1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-01 01:18:10 +02:00
unleash.unleash/frontend/src/component/feature/FeatureToggleList/useGlobalFeatureSearch.ts
Jaanus Sellin ffe1305934
feat: extract global feature search ()
We need global search for command menu, so extracting into separate
hook.
2024-06-12 13:32:13 +03:00

71 lines
1.7 KiB
TypeScript

import {
encodeQueryParams,
NumberParam,
StringParam,
withDefault,
} from 'use-query-params';
import {
DEFAULT_PAGE_LIMIT,
useFeatureSearch,
} from 'hooks/api/getters/useFeatureSearch/useFeatureSearch';
import {
BooleansStringParam,
FilterItemParam,
} from 'utils/serializeQueryParams';
import { usePersistentTableState } from 'hooks/usePersistentTableState';
import mapValues from 'lodash.mapvalues';
export const useGlobalFeatureSearch = (storageKey = 'features-list-table') => {
const stateConfig = {
offset: withDefault(NumberParam, 0),
limit: withDefault(NumberParam, DEFAULT_PAGE_LIMIT),
query: StringParam,
favoritesFirst: withDefault(BooleansStringParam, true),
sortBy: withDefault(StringParam, 'createdAt'),
sortOrder: withDefault(StringParam, 'desc'),
project: FilterItemParam,
tag: FilterItemParam,
state: FilterItemParam,
segment: FilterItemParam,
createdAt: FilterItemParam,
type: FilterItemParam,
};
const [tableState, setTableState] = usePersistentTableState(
`${storageKey}`,
stateConfig,
);
const {
offset,
limit,
query,
favoritesFirst,
sortBy,
sortOrder,
...filterState
} = tableState;
const {
features = [],
total,
loading,
refetch,
initialLoad,
} = useFeatureSearch(
mapValues(encodeQueryParams(stateConfig, tableState), (value) =>
value ? `${value}` : undefined,
),
);
return {
features,
total,
refetch,
loading,
initialLoad,
tableState,
setTableState,
filterState,
};
};