Elffuss BraidHub

kikocis/continue public

2ff6e1c400a1 · 2.7 KiBRawPermalink
1import {
2 BuildingOfficeIcon,
3 ChevronUpDownIcon,
4} from "@heroicons/react/24/outline";
5import { UserCircleIcon } from "@heroicons/react/24/solid";
6import { useContext } from "react";
7import {
8 Listbox,
9 ListboxButton,
10 ListboxOption,
11 ListboxOptions,
12} from "../../components/ui/Listbox";
13import { useAuth } from "../../context/Auth";
14import { IdeMessengerContext } from "../../context/IdeMessenger";
15import { useAppDispatch, useAppSelector } from "../../redux/hooks";
16import {
17 selectCurrentOrg,
18 setSelectedOrgId,
19} from "../../redux/slices/profiles/slice";
20
21interface ScopeSelectProps {
22 onSelect?: () => void;
23}
24export function ScopeSelect({ onSelect }: ScopeSelectProps) {
25 const { organizations } = useAuth();
26 const ideMessenger = useContext(IdeMessengerContext);
27 const selectedOrgId = useAppSelector(
28 (state) => state.profiles.selectedOrganizationId,
29 );
30 const currentOrg = useAppSelector(selectCurrentOrg);
31 const dispatch = useAppDispatch();
32
33 const handleChange = (newValue: string) => {
34 // optimisitic update
35 dispatch(setSelectedOrgId(newValue));
36 ideMessenger.post("didChangeSelectedOrg", {
37 id: newValue,
38 });
39 onSelect?.();
40 };
41
42 const CurScopeEntityFallBackIcon = selectedOrgId
43 ? BuildingOfficeIcon
44 : UserCircleIcon;
45
46 const selectedDisplay = currentOrg ?? {
47 name: "Personal",
48 iconUrl: null,
49 };
50
51 return (
52 <Listbox value={selectedOrgId} onChange={handleChange}>
53 <div className="relative">
54 <ListboxButton className="hover:bg-list-active hover:text-list-active-foreground min-w-[140px] justify-between px-4 py-2 sm:min-w-[200px]">
55 <div className="flex items-center gap-2">
56 {selectedDisplay?.iconUrl ? (
57 <img src={selectedDisplay.iconUrl} alt="" className="h-5 w-5" />
58 ) : (
59 <CurScopeEntityFallBackIcon className="h-5 w-5" />
60 )}
61 <span className="truncate">
62 {selectedDisplay?.name || "Select Organization"}
63 </span>
64 </div>
65 <ChevronUpDownIcon className="h-5 w-5" aria-hidden="true" />
66 </ListboxButton>
67
68 <ListboxOptions className="z-[1000] min-w-[140px] pt-0.5 sm:min-w-[200px]">
69 {organizations.map((org) => (
70 <ListboxOption key={org.id} value={org.id} className="py-2">
71 <div className="flex items-center gap-2">
72 {org.iconUrl ? (
73 <img src={org.iconUrl} alt="" className="h-5 w-5" />
74 ) : (
75 <BuildingOfficeIcon className="h-5 w-5" />
76 )}
77 <span>{org.name}</span>
78 </div>
79 </ListboxOption>
80 ))}
81 </ListboxOptions>
82 </div>
83 </Listbox>
84 );
85}