@@ -12,38 +12,39 @@ import { getDisplayWorkspaceStatus } from "utils/workspace"
12
12
13
13
type UseAutocompleteOptions < TOption extends BaseOption > = {
14
14
id : string
15
- initialQuery ? : string
15
+ value : string | undefined
16
16
// Using null because of react-query
17
17
// https://tanstack.com/query/v4/docs/react/guides/migrating-to-react-query-4#undefined-is-an-illegal-cache-value-for-successful-queries
18
- getInitialOption : ( ) => Promise < TOption | null >
18
+ getSelectedOption : ( ) => Promise < TOption | null >
19
19
getOptions : ( query : string ) => Promise < TOption [ ] >
20
20
onChange : ( option : TOption | undefined ) => void
21
21
enabled ?: boolean
22
22
}
23
23
24
24
const useAutocomplete = < TOption extends BaseOption = BaseOption > ( {
25
25
id,
26
- getInitialOption,
26
+ value,
27
+ getSelectedOption,
27
28
getOptions,
28
29
onChange,
29
30
enabled,
30
31
} : UseAutocompleteOptions < TOption > ) => {
31
32
const [ query , setQuery ] = useState ( "" )
32
33
const [ selectedOption , setSelectedOption ] = useState < TOption > ( )
33
- const initialOptionQuery = useQuery ( {
34
- queryKey : [ id , "autocomplete" , "initial" ] ,
35
- queryFn : ( ) => getInitialOption ( ) ,
34
+ const selectedOptionQuery = useQuery ( {
35
+ queryKey : [ id , "autocomplete" , "selected" , value ] ,
36
+ queryFn : ( ) => getSelectedOption ( ) ,
36
37
onSuccess : ( option ) => setSelectedOption ( option ?? undefined ) ,
37
38
enabled,
38
39
} )
39
40
const searchOptionsQuery = useQuery ( {
40
- queryKey : [ id , "autoComplete " , "search" ] ,
41
+ queryKey : [ id , "autocomplete " , "search" ] ,
41
42
queryFn : ( ) => getOptions ( query ) ,
42
43
enabled,
43
44
} )
44
45
const searchOptions = useMemo ( ( ) => {
45
46
const isDataLoaded =
46
- searchOptionsQuery . isFetched && initialOptionQuery . isFetched
47
+ searchOptionsQuery . isFetched && selectedOptionQuery . isFetched
47
48
48
49
if ( ! isDataLoaded ) {
49
50
return undefined
@@ -55,13 +56,8 @@ const useAutocomplete = <TOption extends BaseOption = BaseOption>({
55
56
return options
56
57
}
57
58
58
- // We will add the initial option on the top of the options
59
- // 1 - remove the initial option from the search options if it exists
60
- // 2 - add the initial option on the top
61
59
options = options . filter ( ( option ) => option . value !== selectedOption . value )
62
60
options . unshift ( selectedOption )
63
-
64
- // Filter data based o search query
65
61
options = options . filter (
66
62
( option ) =>
67
63
option . label . toLowerCase ( ) . includes ( query . toLowerCase ( ) ) ||
@@ -70,7 +66,7 @@ const useAutocomplete = <TOption extends BaseOption = BaseOption>({
70
66
71
67
return options
72
68
} , [
73
- initialOptionQuery . isFetched ,
69
+ selectedOptionQuery . isFetched ,
74
70
query ,
75
71
searchOptionsQuery . data ,
76
72
searchOptionsQuery . isFetched ,
@@ -99,24 +95,27 @@ const useAutocomplete = <TOption extends BaseOption = BaseOption>({
99
95
selectedOption,
100
96
selectOption,
101
97
clearSelection,
102
- isInitializing : initialOptionQuery . isInitialLoading ,
103
- initialOption : initialOptionQuery . data ,
104
- isSearching : searchOptionsQuery . isFetching ,
105
98
searchOptions,
99
+ isInitializing : selectedOptionQuery . isInitialLoading ,
100
+ initialOption : selectedOptionQuery . data ,
101
+ isSearching : searchOptionsQuery . isFetching ,
106
102
}
107
103
}
108
104
109
105
export const useUsersAutocomplete = (
110
- initialOptionValue : string | undefined ,
106
+ value : string | undefined ,
111
107
onChange : ( option : OwnerOption | undefined ) => void ,
112
108
enabled ?: boolean ,
113
109
) =>
114
110
useAutocomplete ( {
111
+ onChange,
112
+ enabled,
113
+ value,
115
114
id : "owner" ,
116
- getInitialOption : async ( ) => {
117
- const usersRes = await getUsers ( { q : initialOptionValue , limit : 1 } )
115
+ getSelectedOption : async ( ) => {
116
+ const usersRes = await getUsers ( { q : value , limit : 1 } )
118
117
const firstUser = usersRes . users . at ( 0 )
119
- if ( firstUser && firstUser . username === initialOptionValue ) {
118
+ if ( firstUser && firstUser . username === value ) {
120
119
return {
121
120
label : firstUser . username ,
122
121
value : firstUser . username ,
@@ -133,24 +132,22 @@ export const useUsersAutocomplete = (
133
132
avatarUrl : user . avatar_url ,
134
133
} ) )
135
134
} ,
136
- onChange,
137
- enabled,
138
135
} )
139
136
140
137
export type UsersAutocomplete = ReturnType < typeof useUsersAutocomplete >
141
138
142
139
export const useTemplatesAutocomplete = (
143
140
orgId : string ,
144
- initialOptionValue : string | undefined ,
141
+ value : string | undefined ,
145
142
onChange : ( option : TemplateOption | undefined ) => void ,
146
143
) => {
147
144
return useAutocomplete ( {
145
+ onChange,
146
+ value,
148
147
id : "template" ,
149
- getInitialOption : async ( ) => {
148
+ getSelectedOption : async ( ) => {
150
149
const templates = await getTemplates ( orgId )
151
- const template = templates . find (
152
- ( template ) => template . name === initialOptionValue ,
153
- )
150
+ const template = templates . find ( ( template ) => template . name === value )
154
151
if ( template ) {
155
152
return {
156
153
label :
@@ -177,14 +174,13 @@ export const useTemplatesAutocomplete = (
177
174
icon : template . icon ,
178
175
} ) )
179
176
} ,
180
- onChange,
181
177
} )
182
178
}
183
179
184
180
export type TemplatesAutocomplete = ReturnType < typeof useTemplatesAutocomplete >
185
181
186
182
export const useStatusAutocomplete = (
187
- initialOptionValue : string | undefined ,
183
+ value : string | undefined ,
188
184
onChange : ( option : StatusOption | undefined ) => void ,
189
185
) => {
190
186
const statusOptions = WorkspaceStatuses . map ( ( status ) => {
@@ -196,12 +192,12 @@ export const useStatusAutocomplete = (
196
192
} as StatusOption
197
193
} )
198
194
return useAutocomplete ( {
195
+ onChange,
196
+ value,
199
197
id : "status" ,
200
- getInitialOption : async ( ) =>
201
- statusOptions . find ( ( option ) => option . value === initialOptionValue ) ??
202
- null ,
198
+ getSelectedOption : async ( ) =>
199
+ statusOptions . find ( ( option ) => option . value === value ) ?? null ,
203
200
getOptions : async ( ) => statusOptions ,
204
- onChange,
205
201
} )
206
202
}
207
203
0 commit comments