Skip to content

Commit eb3bed4

Browse files
committed
refactor: use DotPath type for type-safe nested key support in useFuzzyFilter
Replace plain `string[]` with a recursive `DotPath<T>` type that generates valid dot-notation paths from the object type, giving compile-time safety for nested keys like "environment.type". https://claude.ai/code/session_01HsDEMADbz1HQmZ4MDD753r
1 parent bd094c9 commit eb3bed4

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

apps/webapp/app/hooks/useFuzzyFilter.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
import { useMemo, useState } from "react";
22
import { matchSorter } from "match-sorter";
33

4+
type DotPath<T, Depth extends unknown[] = []> = Depth["length"] extends 4
5+
? never
6+
: T extends object
7+
? {
8+
[K in keyof T & string]: K | `${K}.${DotPath<T[K], [...Depth, unknown]>}`;
9+
}[keyof T & string]
10+
: never;
11+
412
/**
513
* A hook that provides fuzzy filtering functionality for a list of objects.
614
* Uses match-sorter to perform the filtering across multiple object properties and
715
* consistently order the results by score.
816
*
917
* @param params - The parameters object
1018
* @param params.items - Array of objects to filter
11-
* @param params.keys - Array of object keys to perform the fuzzy search on
19+
* @param params.keys - Array of object keys to perform the fuzzy search on (supports dot-notation for nested properties)
1220
* @returns An object containing:
1321
* - filterText: The current filter text
1422
* - setFilterText: Function to update the filter text
@@ -28,7 +36,7 @@ export function useFuzzyFilter<T extends Object>({
2836
keys,
2937
}: {
3038
items: T[];
31-
keys: string[];
39+
keys: DotPath<T>[];
3240
}) {
3341
const [filterText, setFilterText] = useState("");
3442

0 commit comments

Comments
 (0)