Skip to content

Commit 66dec40

Browse files
committed
Fix on search change
1 parent 88b4bb7 commit 66dec40

File tree

2 files changed

+36
-37
lines changed

2 files changed

+36
-37
lines changed

site/src/pages/WorkspacesPage/filter/autocompletes.ts

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,39 @@ import { getDisplayWorkspaceStatus } from "utils/workspace"
1212

1313
type UseAutocompleteOptions<TOption extends BaseOption> = {
1414
id: string
15-
initialQuery?: string
15+
value: string | undefined
1616
// Using null because of react-query
1717
// 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>
1919
getOptions: (query: string) => Promise<TOption[]>
2020
onChange: (option: TOption | undefined) => void
2121
enabled?: boolean
2222
}
2323

2424
const useAutocomplete = <TOption extends BaseOption = BaseOption>({
2525
id,
26-
getInitialOption,
26+
value,
27+
getSelectedOption,
2728
getOptions,
2829
onChange,
2930
enabled,
3031
}: UseAutocompleteOptions<TOption>) => {
3132
const [query, setQuery] = useState("")
3233
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(),
3637
onSuccess: (option) => setSelectedOption(option ?? undefined),
3738
enabled,
3839
})
3940
const searchOptionsQuery = useQuery({
40-
queryKey: [id, "autoComplete", "search"],
41+
queryKey: [id, "autocomplete", "search"],
4142
queryFn: () => getOptions(query),
4243
enabled,
4344
})
4445
const searchOptions = useMemo(() => {
4546
const isDataLoaded =
46-
searchOptionsQuery.isFetched && initialOptionQuery.isFetched
47+
searchOptionsQuery.isFetched && selectedOptionQuery.isFetched
4748

4849
if (!isDataLoaded) {
4950
return undefined
@@ -55,13 +56,8 @@ const useAutocomplete = <TOption extends BaseOption = BaseOption>({
5556
return options
5657
}
5758

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
6159
options = options.filter((option) => option.value !== selectedOption.value)
6260
options.unshift(selectedOption)
63-
64-
// Filter data based o search query
6561
options = options.filter(
6662
(option) =>
6763
option.label.toLowerCase().includes(query.toLowerCase()) ||
@@ -70,7 +66,7 @@ const useAutocomplete = <TOption extends BaseOption = BaseOption>({
7066

7167
return options
7268
}, [
73-
initialOptionQuery.isFetched,
69+
selectedOptionQuery.isFetched,
7470
query,
7571
searchOptionsQuery.data,
7672
searchOptionsQuery.isFetched,
@@ -99,24 +95,27 @@ const useAutocomplete = <TOption extends BaseOption = BaseOption>({
9995
selectedOption,
10096
selectOption,
10197
clearSelection,
102-
isInitializing: initialOptionQuery.isInitialLoading,
103-
initialOption: initialOptionQuery.data,
104-
isSearching: searchOptionsQuery.isFetching,
10598
searchOptions,
99+
isInitializing: selectedOptionQuery.isInitialLoading,
100+
initialOption: selectedOptionQuery.data,
101+
isSearching: searchOptionsQuery.isFetching,
106102
}
107103
}
108104

109105
export const useUsersAutocomplete = (
110-
initialOptionValue: string | undefined,
106+
value: string | undefined,
111107
onChange: (option: OwnerOption | undefined) => void,
112108
enabled?: boolean,
113109
) =>
114110
useAutocomplete({
111+
onChange,
112+
enabled,
113+
value,
115114
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 })
118117
const firstUser = usersRes.users.at(0)
119-
if (firstUser && firstUser.username === initialOptionValue) {
118+
if (firstUser && firstUser.username === value) {
120119
return {
121120
label: firstUser.username,
122121
value: firstUser.username,
@@ -133,24 +132,22 @@ export const useUsersAutocomplete = (
133132
avatarUrl: user.avatar_url,
134133
}))
135134
},
136-
onChange,
137-
enabled,
138135
})
139136

140137
export type UsersAutocomplete = ReturnType<typeof useUsersAutocomplete>
141138

142139
export const useTemplatesAutocomplete = (
143140
orgId: string,
144-
initialOptionValue: string | undefined,
141+
value: string | undefined,
145142
onChange: (option: TemplateOption | undefined) => void,
146143
) => {
147144
return useAutocomplete({
145+
onChange,
146+
value,
148147
id: "template",
149-
getInitialOption: async () => {
148+
getSelectedOption: async () => {
150149
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)
154151
if (template) {
155152
return {
156153
label:
@@ -177,14 +174,13 @@ export const useTemplatesAutocomplete = (
177174
icon: template.icon,
178175
}))
179176
},
180-
onChange,
181177
})
182178
}
183179

184180
export type TemplatesAutocomplete = ReturnType<typeof useTemplatesAutocomplete>
185181

186182
export const useStatusAutocomplete = (
187-
initialOptionValue: string | undefined,
183+
value: string | undefined,
188184
onChange: (option: StatusOption | undefined) => void,
189185
) => {
190186
const statusOptions = WorkspaceStatuses.map((status) => {
@@ -196,12 +192,12 @@ export const useStatusAutocomplete = (
196192
} as StatusOption
197193
})
198194
return useAutocomplete({
195+
onChange,
196+
value,
199197
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,
203200
getOptions: async () => statusOptions,
204-
onChange,
205201
})
206202
}
207203

site/src/pages/WorkspacesPage/filter/filter.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ const FilterSkeleton = (props: SkeletonProps) => {
114114
sx={{
115115
bgcolor: (theme) => theme.palette.background.paperLight,
116116
borderRadius: "6px",
117+
...props.sx,
117118
}}
118119
/>
119120
)
@@ -148,9 +149,11 @@ export const Filter = ({
148149
return (
149150
<Box display="flex" sx={{ gap: 1, mb: 2 }}>
150151
<FilterSkeleton width="100%" />
151-
{autocomplete.users && <FilterSkeleton width="200px" />}
152-
<FilterSkeleton width="200px" />
153-
<FilterSkeleton width="200px" />
152+
{autocomplete.users && (
153+
<FilterSkeleton width="200px" sx={{ flexShrink: 0 }} />
154+
)}
155+
<FilterSkeleton width="200px" sx={{ flexShrink: 0 }} />
156+
<FilterSkeleton width="200px" sx={{ flexShrink: 0 }} />
154157
</Box>
155158
)
156159
}

0 commit comments

Comments
 (0)