2022-12-14 16:22:38 +01:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
|
|
|
|
interface IConditionallyHiddenColumns {
|
|
|
|
condition: boolean;
|
|
|
|
columns: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useConditionallyHiddenColumns = (
|
|
|
|
conditionallyHiddenColumns: IConditionallyHiddenColumns[],
|
2022-12-16 10:46:04 +01:00
|
|
|
setHiddenColumns: (
|
2023-10-02 14:25:46 +02:00
|
|
|
columns: string[] | ((columns: string[]) => string[]),
|
2022-12-16 10:46:04 +01:00
|
|
|
) => void,
|
2023-10-02 14:25:46 +02:00
|
|
|
columnsDefinition: unknown[],
|
2022-12-14 16:22:38 +01:00
|
|
|
) => {
|
|
|
|
useEffect(() => {
|
2022-12-16 10:46:04 +01:00
|
|
|
const columnsToHide = conditionallyHiddenColumns
|
2022-12-14 16:22:38 +01:00
|
|
|
.filter(({ condition }) => condition)
|
2022-12-16 10:46:04 +01:00
|
|
|
.flatMap(({ columns }) => columns);
|
|
|
|
|
|
|
|
const columnsToShow = conditionallyHiddenColumns
|
|
|
|
.flatMap(({ columns }) => columns)
|
2023-10-02 14:25:46 +02:00
|
|
|
.filter((column) => !columnsToHide.includes(column));
|
2022-12-14 16:22:38 +01:00
|
|
|
|
2023-10-02 14:25:46 +02:00
|
|
|
setHiddenColumns((columns) => [
|
2022-12-16 10:46:04 +01:00
|
|
|
...new Set(
|
|
|
|
[...columns, ...columnsToHide].filter(
|
2023-10-02 14:25:46 +02:00
|
|
|
(column) => !columnsToShow.includes(column),
|
|
|
|
),
|
2022-12-16 10:46:04 +01:00
|
|
|
),
|
|
|
|
]);
|
2022-12-14 16:22:38 +01:00
|
|
|
}, [
|
|
|
|
...conditionallyHiddenColumns.map(({ condition }) => condition),
|
|
|
|
setHiddenColumns,
|
|
|
|
columnsDefinition,
|
|
|
|
]);
|
|
|
|
};
|