MCPcopy Create free account
hub / github.com/coder/coder / useFilter

Function useFilter

site/src/components/Filter/Filter.tsx:59–103  ·  view source on GitHub ↗
({
	fallbackFilter = "",
	searchParams,
	onSearchParamsChange,
	onUpdate,
}: UseFilterConfig)

Source from the content-addressed store, hash-verified

57export const useFilterParamsKey = "filter";
58
59export const useFilter = ({
60 fallbackFilter = "",
61 searchParams,
62 onSearchParamsChange,
63 onUpdate,
64}: UseFilterConfig): UseFilterResult => {
65 const query = searchParams.get(useFilterParamsKey) ?? fallbackFilter;
66
67 const update = (newValues: string | FilterValues) => {
68 const serialized =
69 typeof newValues === "string" ? newValues : stringifyFilter(newValues);
70 const noUpdateNeeded = query === serialized;
71 if (noUpdateNeeded) {
72 return;
73 }
74
75 /**
76 * @todo 2025-07-15 - We have a slightly nasty bug here, where trying to
77 * update state via immutable state updates causes our code to break.
78 *
79 * In theory, it would be better to make a copy of the search params. We
80 * can then mutate and dispatch the copy instead of the original. Doing
81 * that causes other parts of our existing logic to break, though.
82 * That's a sign that our other code is slightly broken, and only just
83 * happens to work by chance right now.
84 */
85 searchParams.set(useFilterParamsKey, serialized);
86 onSearchParamsChange(searchParams);
87 onUpdate?.(serialized);
88 };
89
90 const { debounced: debounceUpdate, cancelDebounce } = useDebouncedFunction(
91 update,
92 500,
93 );
94
95 return {
96 query,
97 update,
98 debounceUpdate,
99 cancelDebounce,
100 values: parseFilterQuery(query),
101 used: query !== "" && query !== fallbackFilter,
102 };
103};
104
105const parseFilterQuery = (filterQuery: string): FilterValues => {
106 if (filterQuery === "") {

Callers 9

GroupPageFunction · 0.90
ConnectionLogPageFunction · 0.90
UsersPageFunction · 0.90
AuditPageFunction · 0.90
RequestLogsPageFunction · 0.90
AISessionListPageFunction · 0.90
OrganizationMembersPageFunction · 0.90
useWorkspacesFilterFunction · 0.90
useTemplatesFilterFunction · 0.90

Calls 3

useDebouncedFunctionFunction · 0.90
parseFilterQueryFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected