Skip to content

refactor(site): Improve workspaces filtering #7681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix sync with smart cache
  • Loading branch information
BrunoQuaresma committed May 25, 2023
commit 76bf6ec4515338b6ff8b20301dc1446b49fa58f2
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ const mockAutocomplete = {
selectedOption: undefined,
selectOption: action("selectOption"),
setQuery: action("updateQuery"),
clearSelection: action("clearSelection"),
}

const defaultFilterProps = {
Expand Down
30 changes: 18 additions & 12 deletions site/src/pages/WorkspacesPage/filter/autocompletes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo, useState } from "react"
import { useMemo, useRef, useState } from "react"
import {
BaseOption,
OwnerOption,
Expand Down Expand Up @@ -29,14 +29,26 @@ const useAutocomplete = <TOption extends BaseOption = BaseOption>({
onChange,
enabled,
}: UseAutocompleteOptions<TOption>) => {
const selectedOptionsCacheRef = useRef<Record<string, TOption>>({})
const [query, setQuery] = useState("")
const [selectedOption, setSelectedOption] = useState<TOption>()
const selectedOptionQuery = useQuery({
queryKey: [id, "autocomplete", "selected", value],
queryFn: () => getSelectedOption(),
onSuccess: (option) => setSelectedOption(option ?? undefined),
queryFn: () => {
if (!value) {
return null
}

const cachedOption = selectedOptionsCacheRef.current[value]
if (cachedOption) {
return cachedOption
}

return getSelectedOption()
},
enabled,
keepPreviousData: true,
})
const selectedOption = selectedOptionQuery.data
const searchOptionsQuery = useQuery({
queryKey: [id, "autocomplete", "search"],
queryFn: () => getOptions(query),
Expand Down Expand Up @@ -75,26 +87,20 @@ const useAutocomplete = <TOption extends BaseOption = BaseOption>({

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

if (option.value === selectedOption?.value) {
newSelectedOptionValue = undefined
}

if (onChange) {
onChange(newSelectedOptionValue)
}
setSelectedOption(newSelectedOptionValue)
}
const clearSelection = () => {
setSelectedOption(undefined)
onChange(newSelectedOptionValue)
}

return {
query,
setQuery,
selectedOption,
selectOption,
clearSelection,
searchOptions,
isInitializing: selectedOptionQuery.isInitialLoading,
initialOption: selectedOptionQuery.data,
Expand Down
6 changes: 0 additions & 6 deletions site/src/pages/WorkspacesPage/filter/filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,6 @@ export const Filter = ({
size="small"
onClick={() => {
filter.update("")
autocomplete.templates.clearSelection()
autocomplete.status.clearSelection()

if (autocomplete.users) {
autocomplete.users.clearSelection()
}
}}
>
<CloseOutlined sx={{ fontSize: 14 }} />
Expand Down