* feat: improve text filters (WIP) * move function to bottom * apply some styles * fix selection of TextFilterOptions * rename value to operand * remove unused file * merge filters/filters into filters/utils * fix regression of not putting url params correctly * move makeWhereClause to filters/utils * fix negative, empty, and not empty operators * fix initial filtering from search state (url) * fix type errors * do not send an empty array to query * update yarn.lock * i18n for text filter operators * extract logic as useColumnFilters() * add missing import * fix type error * revert yarn.lock * use i18n * insensitive text match * move data-table to @calcom/features * fix type errors * fix type errors * fix type errors * feat: support DataTable filters for Insights Routing WIP * remove unused filters * remove additionalFilters and fix types * clean up filter components * support icons for ActiveFilters * support filters on json * fix filter ui * fix type error and clean up * revert changes * revert change * clean up * revert change * fix compatibility with insights booking page * remove unused params * fix type errors * update yarn.lock * fix field filter and adjust ui * chore: update yarn.lock * fix text filter * add Clear Filters button * add more test data * feat: support numeric filter WIP * feat: support numeric filter * feat: move People to an external filter * fixing query states * rename state to activeFilters * add useDataTable * fix ResponseCellValue bug and enable resizing for RoutingFormResponsesTable * fix response values * truncate attribute text * seed booking attendees * fix accessor (no actual change though) * add a gap * rename * use pathname as default identifier for DataTable * fix type error * support text filter for assignment reason * Apply suggestions from code review Co-authored-by: Alex van Andel <me@alexvanandel.com> * rename fields to headers * use safeParse * fix type error * push yarn.lock with faker * fix MemberList --------- Co-authored-by: Udit Takkar <udit222001@gmail.com> Co-authored-by: Alex van Andel <me@alexvanandel.com> Co-authored-by: sean-brydon <sean@cal.com>
74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
import type { Table, ColumnSizingState } from "@tanstack/react-table";
|
|
// eslint-disable-next-line no-restricted-imports
|
|
import debounce from "lodash/debounce";
|
|
import { useState, useCallback, useEffect } from "react";
|
|
|
|
type UsePersistentColumnResizingProps<TData> = {
|
|
enabled: boolean;
|
|
table: Table<TData>;
|
|
identifier?: string;
|
|
};
|
|
|
|
function getLocalStorageKey(identifier: string) {
|
|
return `data-table-column-sizing-${identifier}`;
|
|
}
|
|
|
|
function loadColumnSizing(identifier: string) {
|
|
try {
|
|
return JSON.parse(localStorage.getItem(getLocalStorageKey(identifier)) || "{}");
|
|
} catch (error) {
|
|
return {};
|
|
}
|
|
return {};
|
|
}
|
|
|
|
function saveColumnSizing(identifier: string, columnSizing: ColumnSizingState) {
|
|
localStorage.setItem(getLocalStorageKey(identifier), JSON.stringify(columnSizing));
|
|
}
|
|
|
|
const debouncedSaveColumnSizing = debounce(saveColumnSizing, 1000);
|
|
|
|
export function usePersistentColumnResizing<TData>({
|
|
enabled,
|
|
table,
|
|
identifier,
|
|
}: UsePersistentColumnResizingProps<TData>) {
|
|
const [_, setColumnSizing] = useState<ColumnSizingState>({});
|
|
|
|
const onColumnSizingChange = useCallback(
|
|
(updater: ColumnSizingState | ((old: ColumnSizingState) => ColumnSizingState)) => {
|
|
// `!identifier` is checked already in the `useEffect` hook,
|
|
// but TS doesn't know that, and this won't happen.
|
|
if (!identifier) return;
|
|
|
|
table.setState((oldTableState) => {
|
|
const newColumnSizing = typeof updater === "function" ? updater(oldTableState.columnSizing) : updater;
|
|
debouncedSaveColumnSizing(identifier, newColumnSizing);
|
|
setColumnSizing(newColumnSizing);
|
|
|
|
return {
|
|
...oldTableState,
|
|
columnSizing: newColumnSizing,
|
|
};
|
|
});
|
|
},
|
|
[identifier, table]
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (!enabled || !identifier) return;
|
|
|
|
const newColumnSizing = loadColumnSizing(identifier);
|
|
setColumnSizing(newColumnSizing);
|
|
table.setState((old) => ({
|
|
...old,
|
|
columnSizing: newColumnSizing,
|
|
}));
|
|
table.setOptions((prev) => ({
|
|
...prev,
|
|
columnResizeMode: "onChange",
|
|
onColumnSizingChange,
|
|
}));
|
|
}, [enabled, identifier, table, onColumnSizingChange]);
|
|
}
|