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 refetch on error
  • Loading branch information
BrunoQuaresma committed May 25, 2023
commit d6a7c6f20a6748e63f9cab4ab98eb50957501cdc
19 changes: 14 additions & 5 deletions site/src/pages/WorkspacesPage/Filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,28 @@ type UseAutocompleteOptions<TOption extends BaseOption> = {
getInitialOption: () => Promise<TOption | null>
getOptions: (query: string) => Promise<TOption[]>
onChange: (option: TOption | undefined) => void
enabled?: boolean
}

const useAutocomplete = <TOption extends BaseOption = BaseOption>({
id,
getInitialOption,
getOptions,
onChange,
enabled,
}: UseAutocompleteOptions<TOption>) => {
const [query, setQuery] = useState("")
const [selectedOption, setSelectedOption] = useState<TOption>()
const initialOptionQuery = useQuery({
queryKey: [id, "autocomplete", "initial"],
queryFn: () => getInitialOption(),
onSuccess: (option) => setSelectedOption(option ?? undefined),
enabled,
})
const searchOptionsQuery = useQuery({
queryKey: [id, "autoComplete", "search"],
queryFn: () => getOptions(query),
enabled,
})
const searchOptions = useMemo(() => {
const isDataLoaded =
Expand Down Expand Up @@ -202,6 +206,7 @@ const useAutocomplete = <TOption extends BaseOption = BaseOption>({
export const useUsersAutocomplete = (
initialOptionValue: string | undefined,
onChange: (option: OwnerOption | undefined) => void,
enabled?: boolean,
) =>
useAutocomplete({
id: "owner",
Expand All @@ -226,6 +231,7 @@ export const useUsersAutocomplete = (
}))
},
onChange,
enabled,
})

type UsersAutocomplete = ReturnType<typeof useUsersAutocomplete>
Expand Down Expand Up @@ -320,7 +326,7 @@ export const Filter = ({
}: {
filter: UseFilterResult
autocomplete: {
users: UsersAutocomplete
users?: UsersAutocomplete
templates: TemplatesAutocomplete
status: StatusAutocomplete
}
Expand All @@ -329,13 +335,13 @@ export const Filter = ({
const isIinitializingFilters =
autocomplete.status.isInitializing ||
autocomplete.templates.isInitializing ||
autocomplete.users.isInitializing
(autocomplete.users && autocomplete.users.isInitializing)

if (isIinitializingFilters) {
return (
<Box display="flex" sx={{ gap: 1, mb: 2 }}>
<FilterSkeleton width="100%" />
<FilterSkeleton width="200px" />
{autocomplete.users && <FilterSkeleton width="200px" />}
<FilterSkeleton width="200px" />
<FilterSkeleton width="200px" />
</Box>
Expand Down Expand Up @@ -375,9 +381,12 @@ export const Filter = ({
size="small"
onClick={() => {
filter.update("")
autocomplete.users.clearSelection()
autocomplete.templates.clearSelection()
autocomplete.status.clearSelection()

if (autocomplete.users) {
autocomplete.users.clearSelection()
}
}}
>
<CloseOutlined sx={{ fontSize: 14 }} />
Expand All @@ -387,7 +396,7 @@ export const Filter = ({
),
}}
/>
<OwnerFilter autocomplete={autocomplete.users} />
{autocomplete.users && <OwnerFilter autocomplete={autocomplete.users} />}
<TemplatesFilter autocomplete={autocomplete.templates} />
<StatusFilter autocomplete={autocomplete.status} />
</Box>
Expand Down
7 changes: 5 additions & 2 deletions site/src/pages/WorkspacesPage/WorkspacesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
useTemplatesAutocomplete,
useUsersAutocomplete,
} from "./Filter"
import { useOrganizationId } from "hooks"
import { useOrganizationId, usePermissions } from "hooks"

const WorkspacesPage: FC = () => {
const orgId = useOrganizationId()
Expand All @@ -21,9 +21,12 @@ const WorkspacesPage: FC = () => {
query: filter.query,
})
const updateWorkspace = useWorkspaceUpdate(queryKey)
const permissions = usePermissions()
const canFilterByUser = permissions.viewDeploymentValues
const usersAutocomplete = useUsersAutocomplete(
filter.values.owner,
(option) => filter.update({ ...filter.values, owner: option?.value }),
canFilterByUser,
)
const templatesAutocomplete = useTemplatesAutocomplete(
orgId,
Expand All @@ -50,7 +53,7 @@ const WorkspacesPage: FC = () => {
filterProps={{
filter,
autocomplete: {
users: usersAutocomplete,
users: canFilterByUser ? usersAutocomplete : undefined,
templates: templatesAutocomplete,
status: statusAutocomplete,
},
Expand Down
12 changes: 9 additions & 3 deletions site/src/pages/WorkspacesPage/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
WorkspacesResponse,
} from "api/typesGenerated"
import { displayError } from "components/GlobalSnackbar/utils"
import { useState } from "react"
import { useTranslation } from "react-i18next"

type UseWorkspacesDataParams = {
Expand All @@ -26,17 +27,22 @@ export const useWorkspacesData = ({
query,
}: UseWorkspacesDataParams) => {
const queryKey = ["workspaces", query, page]
const [shouldRefetch, setShouldRefetch] = useState(true)
const result = useQuery({
queryKey,
queryFn: () =>
getWorkspaces({
q: query,
limit: limit,
// If the page is <= 0, just use offset 0. This usually happens
// if the page is not provided.
offset: page <= 0 ? 0 : (page - 1) * limit,
}),
refetchInterval: 5_000,
onSuccess: () => {
setShouldRefetch(true)
},
onError: () => {
setShouldRefetch(false)
},
refetchInterval: shouldRefetch ? 5_000 : undefined,
})

return {
Expand Down