|
1
|
import { |
|
2
|
QueryKey, |
|
3
|
keepPreviousData, |
|
4
|
useInfiniteQuery, |
|
5
|
useQuery, |
|
6
|
} from "@tanstack/react-query" |
|
7
|
import { useDebouncedSearch } from "./use-debounced-search" |
|
8
|
|
|
9
|
type ComboboxExternalData = { |
|
10
|
offset: number |
|
11
|
limit: number |
|
12
|
count: number |
|
13
|
} |
|
14
|
|
|
15
|
type ComboboxQueryParams = { |
|
16
|
q?: string |
|
17
|
offset?: number |
|
18
|
limit?: number |
|
19
|
} |
|
20
|
|
|
21
|
export const useComboboxData = < |
|
22
|
TResponse extends ComboboxExternalData, |
|
23
|
TParams extends ComboboxQueryParams |
|
24
|
>({ |
|
25
|
queryKey, |
|
26
|
queryFn, |
|
27
|
getOptions, |
|
28
|
defaultValue, |
|
29
|
defaultValueKey, |
|
30
|
pageSize = 10, |
|
31
|
enabled = true, |
|
32
|
}: { |
|
33
|
queryKey: QueryKey |
|
34
|
queryFn: (params: TParams) => Promise<TResponse> |
|
35
|
getOptions: (data: TResponse) => { label: string; value: string }[] |
|
36
|
defaultValueKey?: keyof TParams |
|
37
|
defaultValue?: string | string[] |
|
38
|
pageSize?: number |
|
39
|
enabled?: boolean |
|
40
|
}) => { |
|
41
|
const { searchValue, onSearchValueChange, query } = useDebouncedSearch() |
|
42
|
|
|
43
|
const queryInitialDataBy = defaultValueKey || "id" |
|
44
|
const { data: initialData } = useQuery({ |
|
45
|
queryKey: queryKey, |
|
46
|
queryFn: async () => { |
|
47
|
return queryFn({ |
|
48
|
[queryInitialDataBy]: defaultValue, |
|
49
|
limit: Array.isArray(defaultValue) ? defaultValue.length : 1, |
|
50
|
} as TParams) |
|
51
|
}, |
|
52
|
enabled: !!defaultValue, |
|
53
|
}) |
|
54
|
|
|
55
|
const { data, ...rest } = useInfiniteQuery({ |
|
56
|
queryKey: [...queryKey, query], |
|
57
|
queryFn: async ({ pageParam = 0 }) => { |
|
58
|
return await queryFn({ |
|
59
|
q: query, |
|
60
|
limit: pageSize, |
|
61
|
offset: pageParam, |
|
62
|
} as TParams) |
|
63
|
}, |
|
64
|
initialPageParam: 0, |
|
65
|
getNextPageParam: (lastPage) => { |
|
66
|
const moreItemsExist = lastPage.count > lastPage.offset + lastPage.limit |
|
67
|
return moreItemsExist ? lastPage.offset + lastPage.limit : undefined |
|
68
|
}, |
|
69
|
placeholderData: keepPreviousData, |
|
70
|
enabled, |
|
71
|
}) |
|
72
|
|
|
73
|
const options = data?.pages.flatMap((page) => getOptions(page)) ?? [] |
|
74
|
const defaultOptions = initialData ? getOptions(initialData) : [] |
|
75
|
|
|
76
|
/** |
|
77
|
* If there are no options and the query is empty, then the combobox should be disabled, |
|
78
|
* as there is no data to search for. |
|
79
|
*/ |
|
80
|
const disabled = !rest.isPending && !options.length && !searchValue |
|
81
|
|
|
82
|
// make sure that the default value is included in the options |
|
83
|
if (defaultValue && defaultOptions.length && !searchValue) { |
|
84
|
defaultOptions.forEach((option) => { |
|
85
|
if (!options.find((o) => o.value === option.value)) { |
|
86
|
options.unshift(option) |
|
87
|
} |
|
88
|
}) |
|
89
|
} |
|
90
|
|
|
91
|
return { |
|
92
|
options, |
|
93
|
searchValue, |
|
94
|
onSearchValueChange, |
|
95
|
disabled, |
|
96
|
...rest, |
|
97
|
} |
|
98
|
} |