| 1 | "use client"; |
| 2 | |
| 3 | import { ProfileAvatar } from |
| 4 | import { useDebouncedValue } |
| 5 | import { keepPreviousData, useQuery |
| 6 | import { Fragment, useState |
| 7 | |
| 8 | import { |
| 9 | Combobox, |
| 10 | ComboboxChip, |
| 11 | ComboboxChips, |
| 12 | ComboboxChipsInput, |
| 13 | ComboboxContent, |
| 14 | ComboboxEmpty, |
| 15 | ComboboxItem, |
| 16 | ComboboxList, |
| 17 | ComboboxValue, |
| 18 | useComboboxAnchor, |
| 19 | } from "@/components/ui/combobox"; |
| 20 | import { suggestUsers } |
| 21 | |
| 22 | const MAX_ASSIGNEES = 10 |
| 23 | |
| 24 | /** Picks up to ten assignees by searching usernames. Opened empty, it offers the people involved in the repository, and the viewer can assign themselves in one click. */ |
| 25 | export function AssigneePicker({ |
| 26 | username, |
| 27 | repo, |
| 28 | value, |
| 29 | onChange, |
| 30 | viewer, |
| 31 | id, |
| 32 | disabled = false, |
| 33 | }: { |
| 34 | username: string |
| 35 | repo: string |
| 36 | value: string |
| 37 | onChange: (assignees |
| 38 | viewer?: string |
| 39 | id?: string |
| 40 | disabled?: boolean |
| 41 | }) { |
| 42 | const anchor = |
| 43 | const [query, |
| 44 | const [search] |
| 45 | |
| 46 | const { data: |
| 47 | queryKey: ["assignee-suggestions", |
| 48 | placeholderData: keepPreviousData, |
| 49 | staleTime: 30_000, |
| 50 | queryFn: () => |
| 51 | }); |
| 52 | const byName = |
| 53 | |
| 54 | return ( |
| 55 | <div className="flex flex-col gap-1.5 |
| 56 | <Combobox |
| 57 | multiple |
| 58 | items={users.map |
| 59 | // the API matched already |
| 60 | filter={null} |
| 61 | value={value} |
| 62 | onValueChange={(next: |
| 63 | onChange(next.slice |
| 64 | } |
| 65 | inputValue={query} |
| 66 | onInputValueChange={setQuery} |
| 67 | disabled={disabled} |
| 68 | > |
| 69 | <ComboboxChips ref={ |
| 70 | <ComboboxValue> |
| 71 | {(selected: string |
| 72 | <Fragment> |
| 73 | {selected.map(( |
| 74 | <ComboboxChip key={ |
| 75 | ))} |
| 76 | <ComboboxChipsInput |
| 77 | id={id} |
| 78 | placeholder={selected.length |
| 79 | /> |
| 80 | </Fragment> |
| 81 | )} |
| 82 | </ComboboxValue> |
| 83 | </ComboboxChips> |
| 84 | <ComboboxContent anchor={ |
| 85 | <ComboboxEmpty> |
| 86 | {isFetching ? "Searching… |
| 87 | </ComboboxEmpty> |
| 88 | <ComboboxList> |
| 89 | {(name: string |
| 90 | <ComboboxItem key={ |
| 91 | <ProfileAvatar |
| 92 | name={name} |
| 93 | image={byName.get |
| 94 | className="size-5" |
| 95 | fallbackClassName="text-[10px]" |
| 96 | /> |
| 97 | <span className="font-medium |
| 98 | <span className="truncate text-muted-foreground |
| 99 | {byName.get(name |
| 100 | </span> |
| 101 | </ComboboxItem> |
| 102 | )} |
| 103 | </ComboboxList> |
| 104 | </ComboboxContent> |
| 105 | </Combobox> |
| 106 | |
| 107 | {viewer && !value |
| 108 | <button |
| 109 | type="button" |
| 110 | className="w-fit text-xs text-muted-foreground hover:text-primary hover:underline disabled:opacity-50" |
| 111 | disabled={disabled} |
| 112 | onClick={() => |
| 113 | > |
| 114 | Assign yourself |
| 115 | </button> |
| 116 | )} |
| 117 | </div> |
| 118 | ); |
| 119 | } |