1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/frontend/src/hooks/useConditionallyHiddenColumns.ts
Nuno Góis 1d1219a055
Use new useConditionallyHiddenColumns hook (#2695)
https://linear.app/unleash/issue/2-515/adapt-tables-to-use-the-new-useconditionallyhiddencolumns-hook

Uses the new `useConditionallyHiddenColumns` hook, like mentioned here:
https://github.com/Unleash/unleash/pull/2691
Also includes small fixes for things I caught along the way. See
comments below.
2022-12-16 10:46:04 +01:00

37 lines
1.1 KiB
TypeScript

import { useEffect } from 'react';
interface IConditionallyHiddenColumns {
condition: boolean;
columns: string[];
}
export const useConditionallyHiddenColumns = (
conditionallyHiddenColumns: IConditionallyHiddenColumns[],
setHiddenColumns: (
columns: string[] | ((columns: string[]) => string[])
) => void,
columnsDefinition: unknown[]
) => {
useEffect(() => {
const columnsToHide = conditionallyHiddenColumns
.filter(({ condition }) => condition)
.flatMap(({ columns }) => columns);
const columnsToShow = conditionallyHiddenColumns
.flatMap(({ columns }) => columns)
.filter(column => !columnsToHide.includes(column));
setHiddenColumns(columns => [
...new Set(
[...columns, ...columnsToHide].filter(
column => !columnsToShow.includes(column)
)
),
]);
}, [
...conditionallyHiddenColumns.map(({ condition }) => condition),
setHiddenColumns,
columnsDefinition,
]);
};