Skip to content

Commit 76bf6ec

Browse files
committed
Fix sync with smart cache
1 parent 66dec40 commit 76bf6ec

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

site/src/pages/WorkspacesPage/WorkspacesPageView.stories.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ const mockAutocomplete = {
8080
selectedOption: undefined,
8181
selectOption: action("selectOption"),
8282
setQuery: action("updateQuery"),
83-
clearSelection: action("clearSelection"),
8483
}
8584

8685
const defaultFilterProps = {

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useMemo, useState } from "react"
1+
import { useMemo, useRef, useState } from "react"
22
import {
33
BaseOption,
44
OwnerOption,
@@ -29,14 +29,26 @@ const useAutocomplete = <TOption extends BaseOption = BaseOption>({
2929
onChange,
3030
enabled,
3131
}: UseAutocompleteOptions<TOption>) => {
32+
const selectedOptionsCacheRef = useRef<Record<string, TOption>>({})
3233
const [query, setQuery] = useState("")
33-
const [selectedOption, setSelectedOption] = useState<TOption>()
3434
const selectedOptionQuery = useQuery({
3535
queryKey: [id, "autocomplete", "selected", value],
36-
queryFn: () => getSelectedOption(),
37-
onSuccess: (option) => setSelectedOption(option ?? undefined),
36+
queryFn: () => {
37+
if (!value) {
38+
return null
39+
}
40+
41+
const cachedOption = selectedOptionsCacheRef.current[value]
42+
if (cachedOption) {
43+
return cachedOption
44+
}
45+
46+
return getSelectedOption()
47+
},
3848
enabled,
49+
keepPreviousData: true,
3950
})
51+
const selectedOption = selectedOptionQuery.data
4052
const searchOptionsQuery = useQuery({
4153
queryKey: [id, "autocomplete", "search"],
4254
queryFn: () => getOptions(query),
@@ -75,26 +87,20 @@ const useAutocomplete = <TOption extends BaseOption = BaseOption>({
7587

7688
const selectOption = (option: TOption) => {
7789
let newSelectedOptionValue: TOption | undefined = option
90+
selectedOptionsCacheRef.current[option.value] = option
7891

7992
if (option.value === selectedOption?.value) {
8093
newSelectedOptionValue = undefined
8194
}
8295

83-
if (onChange) {
84-
onChange(newSelectedOptionValue)
85-
}
86-
setSelectedOption(newSelectedOptionValue)
87-
}
88-
const clearSelection = () => {
89-
setSelectedOption(undefined)
96+
onChange(newSelectedOptionValue)
9097
}
9198

9299
return {
93100
query,
94101
setQuery,
95102
selectedOption,
96103
selectOption,
97-
clearSelection,
98104
searchOptions,
99105
isInitializing: selectedOptionQuery.isInitialLoading,
100106
initialOption: selectedOptionQuery.data,

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,6 @@ export const Filter = ({
205205
size="small"
206206
onClick={() => {
207207
filter.update("")
208-
autocomplete.templates.clearSelection()
209-
autocomplete.status.clearSelection()
210-
211-
if (autocomplete.users) {
212-
autocomplete.users.clearSelection()
213-
}
214208
}}
215209
>
216210
<CloseOutlined sx={{ fontSize: 14 }} />

0 commit comments

Comments
 (0)