|
1 | | -import { useAtomValue } from "jotai"; |
2 | | -import { useEffect, useState } from "react"; |
3 | | -import { Link, useNavigate } from "react-router"; |
| 1 | +import { Icon } from "@iconify-icon/react"; |
| 2 | +import { ChevronsUpDownIcon } from "lucide-react"; |
| 3 | +import { useFilter } from "react-aria-components"; |
4 | 4 |
|
5 | | -import { queryClient } from "@/shared/api/rest/client"; |
6 | 5 | import { constructPath } from "@/shared/api/rest/fetch"; |
7 | | -import { breadcrumbItemStyle } from "@/shared/components/layout/breadcrumb-navigation/style"; |
8 | | -import { |
9 | | - Combobox, |
10 | | - ComboboxContent, |
11 | | - ComboboxList, |
12 | | - ComboboxTrigger, |
13 | | -} from "@/shared/components/ui/combobox"; |
14 | | -import { CommandEmpty, CommandItem } from "@/shared/components/ui/command"; |
15 | | -import { classNames } from "@/shared/utils/common"; |
| 6 | +import { Autocomplete } from "@/shared/components/aria/autocomplete"; |
| 7 | +import { ListBox, ListBoxItem } from "@/shared/components/aria/list-box"; |
| 8 | +import { Popover, PopoverDialog, PopoverTrigger } from "@/shared/components/aria/popover"; |
| 9 | +import { BreadcrumbItem } from "@/shared/components/ui/breadcrumb"; |
16 | 10 |
|
17 | | -import { getBranchesQueryOptions } from "@/entities/branches/domain/get-branches.query"; |
18 | | -import { branchesState } from "@/entities/branches/stores"; |
| 11 | +import { useGetBranches } from "@/entities/branches/domain/get-branches.query"; |
| 12 | + |
| 13 | +interface BreadcrumbBranchSelectorProps { |
| 14 | + currentBranchName: string; |
| 15 | +} |
19 | 16 |
|
20 | 17 | export default function BreadcrumbBranchSelector({ |
21 | | - value, |
22 | | - className, |
| 18 | + currentBranchName, |
23 | 19 | ...props |
24 | | -}: { |
25 | | - value: string; |
26 | | - className?: string; |
27 | | -}) { |
28 | | - const branches = useAtomValue(branchesState); |
29 | | - const navigate = useNavigate(); |
30 | | - const [isOpen, setIsOpen] = useState(false); |
31 | | - |
32 | | - useEffect(() => { |
33 | | - if (isOpen) queryClient.invalidateQueries(getBranchesQueryOptions()); |
34 | | - }, [isOpen]); |
| 20 | +}: BreadcrumbBranchSelectorProps) { |
| 21 | + const { data: branches = [] } = useGetBranches(); |
| 22 | + const { contains } = useFilter({ sensitivity: "base" }); |
35 | 23 |
|
36 | 24 | return ( |
37 | | - <Combobox open={isOpen} onOpenChange={setIsOpen}> |
38 | | - <ComboboxTrigger className={classNames(breadcrumbItemStyle, className)} {...props}> |
39 | | - {value} |
40 | | - </ComboboxTrigger> |
| 25 | + <PopoverTrigger> |
| 26 | + <BreadcrumbItem {...props}> |
| 27 | + <span className="truncate">{currentBranchName}</span> |
| 28 | + <ChevronsUpDownIcon className="ml-2 size-4" /> |
| 29 | + </BreadcrumbItem> |
41 | 30 |
|
42 | | - <ComboboxContent align="start" fitTriggerWidth={false}> |
43 | | - <ComboboxList> |
44 | | - <CommandEmpty>No branch found.</CommandEmpty> |
45 | | - {branches.map((branch) => { |
46 | | - const branchUrl = constructPath(`/branches/${branch.name}`); |
47 | | - return ( |
48 | | - <CommandItem |
49 | | - key={branch.name} |
50 | | - value={branch.name} |
51 | | - onSelect={() => { |
52 | | - setIsOpen(false); |
53 | | - navigate(branchUrl); |
54 | | - }} |
55 | | - asChild |
| 31 | + <Popover className="bg-stone-100/50 backdrop-blur"> |
| 32 | + <PopoverDialog aria-label="Branch selector"> |
| 33 | + {({ close }) => ( |
| 34 | + <Autocomplete filter={contains}> |
| 35 | + <ListBox |
| 36 | + items={branches} |
| 37 | + emptyMessage="No branches found." |
| 38 | + className="p-1" |
| 39 | + onAction={close} |
56 | 40 | > |
57 | | - <Link to={branchUrl}>{branch.name}</Link> |
58 | | - </CommandItem> |
59 | | - ); |
60 | | - })} |
61 | | - </ComboboxList> |
62 | | - </ComboboxContent> |
63 | | - </Combobox> |
| 41 | + {(branch) => ( |
| 42 | + <ListBoxItem |
| 43 | + textValue={branch.name} |
| 44 | + href={constructPath(`/branches/${branch.name}`)} |
| 45 | + > |
| 46 | + <Icon icon="mdi:source-branch" /> {branch.name} |
| 47 | + </ListBoxItem> |
| 48 | + )} |
| 49 | + </ListBox> |
| 50 | + </Autocomplete> |
| 51 | + )} |
| 52 | + </PopoverDialog> |
| 53 | + </Popover> |
| 54 | + </PopoverTrigger> |
64 | 55 | ); |
65 | 56 | } |
0 commit comments