refactor: make DataTableProvider framework-agnostic by requiring tableIdentifier (#24513)

* refactor: make DataTableProvider framework-agnostic by requiring tableIdentifier

- Remove Next.js usePathname dependency from DataTableProvider
- Make tableIdentifier a required prop instead of optional
- Update all usages to provide explicit tableIdentifier values
- This makes DataTableProvider usable in non-Next.js contexts

Co-Authored-By: [email protected] <[email protected]>

* refactor: use usePathname at usage sites instead of hardcoding tableIdentifier

- Add validation in DataTableProvider for empty/nullish tableIdentifier
- Use usePathname() in Next.js apps to pass pathname as tableIdentifier
- Use descriptive identifiers for non-Next.js package components
- This keeps DataTableProvider framework-agnostic while allowing Next.js apps to use pathname

Co-Authored-By: [email protected] <[email protected]>

* use pathname instead of hard-coded identifiers

* change type of tableIdentifier

* simplify

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
Eunjae LeeandDevin AI authored and GitHub committed 2025-10-23 15:32:16 +02:00
1 parent 4ac8a51bc2
commit 09ddbe886f
14 files changed
+67 -42

No files matched your search

@@ -1,11 +1,8 @@
"use client";
import type { SortingState, OnChangeFn, VisibilityState, ColumnSizingState } from "@tanstack/react-table";
// eslint-disable-next-line no-restricted-imports
import debounce from "lodash/debounce";
// eslint-disable-next-line no-restricted-imports
import isEqual from "lodash/isEqual";
import { usePathname } from "next/navigation";
import { useQueryState } from "nuqs";
import { useState, createContext, useCallback, useEffect, useRef, useMemo } from "react";
@@ -79,9 +76,9 @@ export type DataTableContextType = {
export const DataTableContext = createContext<DataTableContextType | null>(null);
interface DataTableProviderProps {
useSegments?: UseSegments;
tableIdentifier?: string;
tableIdentifier: string;
children: React.ReactNode;
useSegments?: UseSegments;
ctaContainerClassName?: string;
defaultPageSize?: number;
segments?: FilterSegmentOutput[];
@@ -91,7 +88,7 @@ interface DataTableProviderProps {
}
export function DataTableProvider({
tableIdentifier: _tableIdentifier,
tableIdentifier,
children,
useSegments = useSegmentsNoop,
defaultPageSize = DEFAULT_PAGE_SIZE,
@@ -101,10 +98,8 @@ export function DataTableProvider({
preferredSegmentId,
systemSegments,
}: DataTableProviderProps) {
const pathname = usePathname() as string | null;
const tableIdentifier = _tableIdentifier ?? pathname ?? undefined;
if (!tableIdentifier) {
throw new Error("tableIdentifier is required");
if (!tableIdentifier.trim()) {
throw new Error("tableIdentifier is required and cannot be empty");
}
const filterToOpen = useRef<string | undefined>(undefined);
+6 -5
View File
@@ -145,8 +145,11 @@ function UserTable() {
// ... other table options
});
const pathname = usePathname();
const tableIdentifier = "hard-coded idenfidier" // or pathname;
return (
<DataTableProvider tableIdentifier="user-table">
<DataTableProvider tableIdentifier={tableIdentifier}>
<DataTableWrapper
table={table}
paginationMode="standard"
@@ -179,7 +182,7 @@ The context provider that manages all table state including filters, sorting, pa
```tsx
interface DataTableProviderProps {
tableIdentifier?: string; // Unique identifier for the table
tableIdentifier: string; // Unique identifier for the table (throws if empty)
children: React.ReactNode;
useSegments?: UseSegments; // Custom segment hook
defaultPageSize?: number; // Default: 10
@@ -530,7 +533,6 @@ Segments allow users to save and share filter configurations. There are two type
```tsx
<DataTableProvider
tableIdentifier="users"
useSegments={useSegments} // Required to enable segments
>
{/* Your table content */}
@@ -564,7 +566,6 @@ const systemSegments: SystemFilterSegment[] = [
<DataTableProvider
systemSegments={systemSegments}
tableIdentifier="user-table"
>
{/* ... */}
</DataTableProvider>
@@ -1210,7 +1211,7 @@ function UserTableContainer() {
const { data, isPending } = useUsers(filters);
return (
<DataTableProvider tableIdentifier="users">
<DataTableProvider>
<UserTable data={data} isPending={isPending} />
</DataTableProvider>
);