-
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.
✨ Provide initial state for column management (#1964)
Related improvements: 1. support restoring columns to initial configuration 2. clean up invalid columns stored in local storage Part-of: #1931 Signed-off-by: Radoslaw Szwajkowski <[email protected]>
- Loading branch information
Showing
6 changed files
with
66 additions
and
20 deletions.
There are no files selected for viewing
44 changes: 37 additions & 7 deletions
44
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 |
---|---|---|
@@ -1,28 +1,58 @@ | ||
import { useLocalStorage } from "@migtools/lib-ui"; | ||
import { ColumnSetting } from "../types"; | ||
import { useEffect } from "react"; | ||
|
||
export interface ColumnState<TColumnKey extends string> { | ||
id: TColumnKey; | ||
label: string; | ||
isVisible: boolean; | ||
isIdentity?: boolean; | ||
} | ||
|
||
export interface IColumnState<TColumnKey extends string> { | ||
columns: ColumnState<TColumnKey>[]; | ||
defaultColumns: ColumnState<TColumnKey>[]; | ||
setColumns: (newColumns: ColumnState<TColumnKey>[]) => void; | ||
} | ||
|
||
interface IColumnStateArgs<TColumnKey extends string> { | ||
initialColumns: ColumnState<TColumnKey>[]; | ||
initialColumns?: Partial<Record<TColumnKey, ColumnSetting>>; | ||
columnsKey: string; | ||
supportedColumns: Record<TColumnKey, string>; | ||
} | ||
|
||
export const useColumnState = <TColumnKey extends string>( | ||
args: IColumnStateArgs<TColumnKey> | ||
): IColumnState<TColumnKey> => { | ||
export const useColumnState = <TColumnKey extends string>({ | ||
initialColumns, | ||
supportedColumns, | ||
columnsKey, | ||
}: IColumnStateArgs<TColumnKey>): IColumnState<TColumnKey> => { | ||
const defaultColumns = ( | ||
Object.entries(supportedColumns) as [TColumnKey, string][] | ||
).map(([id, label]) => ({ | ||
id, | ||
label, | ||
isVisible: initialColumns?.[id]?.isVisible ?? true, | ||
isIdentity: initialColumns?.[id]?.isIdentity, | ||
})); | ||
const [columns, setColumns] = useLocalStorage<ColumnState<TColumnKey>[]>({ | ||
key: args.columnsKey, | ||
defaultValue: args.initialColumns, | ||
key: columnsKey, | ||
defaultValue: defaultColumns, | ||
}); | ||
|
||
return { columns, setColumns }; | ||
useEffect(() => { | ||
const valid = columns.filter(({ id }) => | ||
defaultColumns.find((it) => id === it.id) | ||
); | ||
if (valid.length !== defaultColumns.length) { | ||
setColumns( | ||
defaultColumns.map((it) => ({ | ||
...it, | ||
isVisible: | ||
valid.find(({ id }) => id === it.id)?.isVisible ?? it.isVisible, | ||
})) | ||
); | ||
} | ||
}, [defaultColumns, columns, setColumns]); | ||
|
||
return { columns, setColumns, defaultColumns }; | ||
}; |
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
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