Skip to content

fix(site): Make current user first in the list and fix search #7722

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 1 commit into from
May 31, 2023
Merged
Changes from all commits
Commits
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
22 changes: 18 additions & 4 deletions site/src/pages/WorkspacesPage/filter/autocompletes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useQuery } from "@tanstack/react-query"
import { getTemplates, getUsers } from "api/api"
import { WorkspaceStatuses } from "api/typesGenerated"
import { getDisplayWorkspaceStatus } from "utils/workspace"
import { useMe } from "hooks"

type UseAutocompleteOptions<TOption extends BaseOption> = {
id: string
Expand Down Expand Up @@ -50,7 +51,7 @@ const useAutocomplete = <TOption extends BaseOption = BaseOption>({
})
const selectedOption = selectedOptionQuery.data
const searchOptionsQuery = useQuery({
queryKey: [id, "autocomplete", "search"],
queryKey: [id, "autocomplete", "search", query],
queryFn: () => getOptions(query),
enabled,
})
Expand Down Expand Up @@ -114,8 +115,18 @@ export const useUsersAutocomplete = (
value: string | undefined,
onChange: (option: OwnerOption | undefined) => void,
enabled?: boolean,
) =>
useAutocomplete({
) => {
const me = useMe()

const addMeAsFirstOption = (options: OwnerOption[]) => {
options = options.filter((option) => option.value !== me.username)
return [
{ label: me.username, value: me.username, avatarUrl: me.avatar_url },
...options,
]
}

return useAutocomplete({
onChange,
enabled,
value,
Expand All @@ -134,13 +145,16 @@ export const useUsersAutocomplete = (
},
getOptions: async (query) => {
const usersRes = await getUsers({ q: query, limit: 25 })
return usersRes.users.map((user) => ({
let options: OwnerOption[] = usersRes.users.map((user) => ({
label: user.username,
value: user.username,
avatarUrl: user.avatar_url,
}))
options = addMeAsFirstOption(options)
return options
},
})
}

export type UsersAutocomplete = ReturnType<typeof useUsersAutocomplete>

Expand Down