-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate col mgmt modal with table hooks
- Loading branch information
1 parent
af5cbe1
commit 316bb70
Showing
8 changed files
with
277 additions
and
167 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
client/src/app/hooks/table-controls/column/useColumnState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { usePersistentState } from "@app/hooks/usePersistentState"; | ||
import { IFeaturePersistenceArgs } from "../types"; | ||
|
||
export interface ColumnState<TColumnKey extends string> { | ||
id: TColumnKey; | ||
label: string; | ||
isVisible: boolean; | ||
order: number; | ||
width?: number; | ||
} | ||
|
||
export interface IColumnState<TColumnKey extends string> { | ||
columns: ColumnState<TColumnKey>[]; | ||
setColumns: (newColumns: ColumnState<TColumnKey>[]) => void; | ||
} | ||
|
||
interface IColumnStateArgs<TColumnKey extends string> { | ||
initialColumns: ColumnState<TColumnKey>[]; | ||
} | ||
|
||
export const useColumnState = < | ||
TColumnKey extends string, | ||
TPersistenceKeyPrefix extends string = string, | ||
>( | ||
args: IColumnStateArgs<TColumnKey> & | ||
IFeaturePersistenceArgs<TPersistenceKeyPrefix> | ||
): IColumnState<TColumnKey> => { | ||
const { persistTo = "state", persistenceKeyPrefix, initialColumns } = args; | ||
|
||
const [columns, setColumns] = usePersistentState< | ||
ColumnState<TColumnKey>[], | ||
TPersistenceKeyPrefix, | ||
"columns" | ||
>({ | ||
defaultValue: initialColumns, | ||
persistenceKeyPrefix, | ||
...(persistTo === "urlParams" | ||
? { | ||
persistTo, | ||
keys: ["columns"], | ||
serialize: (columnsObj) => { | ||
return { columns: JSON.stringify(columnsObj) }; | ||
}, | ||
deserialize: ({ columns: columnsStr }) => { | ||
try { | ||
return JSON.parse(columnsStr || "[]"); | ||
} catch (e) { | ||
return initialColumns; // Fallback to initial columns on parse failure | ||
} | ||
}, | ||
} | ||
: persistTo === "localStorage" || persistTo === "sessionStorage" | ||
? { | ||
persistTo, | ||
key: "columns", | ||
} | ||
: { persistTo }), | ||
}); | ||
return { columns, setColumns }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.