|
| 1 | +import { SettingsIcon } from "lucide-react"; |
| 2 | +import { |
| 3 | + useMemo, |
| 4 | + useReducer, |
| 5 | + useRef, |
| 6 | + type KeyboardEvent as ReactKeyboardEvent, |
| 7 | + type MouseEvent as ReactMouseEvent, |
| 8 | + type ReactNode, |
| 9 | +} from "react"; |
| 10 | + |
| 11 | +import { useEnvironmentMachines, usePrimaryEnvironmentId } from "~/state/environments"; |
| 12 | +import { |
| 13 | + projectGroupsSpanEnvironments, |
| 14 | + type SidebarProjectSnapshot, |
| 15 | +} from "~/sidebarProjectGrouping"; |
| 16 | +import { ProjectEnvironmentBadge } from "./ProjectEnvironmentBadge"; |
| 17 | +import { ProjectFavicon } from "./ProjectFavicon"; |
| 18 | +import { filterProjectPickerItems, reduceProjectPickerMenuState } from "./ProjectPicker.logic"; |
| 19 | +import { Button } from "./ui/button"; |
| 20 | +import { |
| 21 | + Combobox, |
| 22 | + ComboboxEmpty, |
| 23 | + ComboboxItem, |
| 24 | + ComboboxList, |
| 25 | + ComboboxPopup, |
| 26 | + ComboboxSearchInput, |
| 27 | + useComboboxFilter, |
| 28 | +} from "./ui/combobox"; |
| 29 | + |
| 30 | +export interface ProjectPickerItem { |
| 31 | + readonly value: string; |
| 32 | + readonly label: string; |
| 33 | + /** Null for rows that are not a project, such as "All projects". */ |
| 34 | + readonly project: SidebarProjectSnapshot | null; |
| 35 | + /** Leading icon for rows without a project. */ |
| 36 | + readonly icon?: ReactNode; |
| 37 | + readonly hideWhileSearching?: boolean; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Searchable project list behind the sidebar's project filter and the draft |
| 42 | + * screen's project selector. The caller renders the `ComboboxTrigger` as |
| 43 | + * `trigger`, since each surface has its own button. |
| 44 | + */ |
| 45 | +export function ProjectPicker(props: { |
| 46 | + readonly items: readonly ProjectPickerItem[]; |
| 47 | + readonly selectedValue: string | null; |
| 48 | + readonly onSelect: (item: ProjectPickerItem) => void; |
| 49 | + readonly trigger: ReactNode; |
| 50 | + readonly align?: "start" | "center" | "end"; |
| 51 | + readonly anchor?: React.RefObject<Element | null>; |
| 52 | + /** Adds a settings button and the context-menu gesture to project rows. */ |
| 53 | + readonly onProjectSettings?: (project: SidebarProjectSnapshot) => void; |
| 54 | +}) { |
| 55 | + const { items, onProjectSettings } = props; |
| 56 | + const primaryEnvironmentId = usePrimaryEnvironmentId(); |
| 57 | + const environmentMachineById = useEnvironmentMachines(); |
| 58 | + // Same-named projects on two machines are only told apart by where they |
| 59 | + // live, so rows on another machine carry its icon once the catalog spans |
| 60 | + // more than one environment; a single-machine catalog stays as it was. |
| 61 | + const showProjectEnvironments = useMemo( |
| 62 | + () => |
| 63 | + projectGroupsSpanEnvironments( |
| 64 | + items.flatMap((item) => (item.project === null ? [] : [item.project])), |
| 65 | + ), |
| 66 | + [items], |
| 67 | + ); |
| 68 | + const [menuState, dispatchMenu] = useReducer(reduceProjectPickerMenuState, { |
| 69 | + open: false, |
| 70 | + query: "", |
| 71 | + }); |
| 72 | + const filter = useComboboxFilter(); |
| 73 | + // Filtering derives from the same React state that controls the input, so |
| 74 | + // the visible query and the visible list can never desync. |
| 75 | + const filteredItems = useMemo( |
| 76 | + () => |
| 77 | + filterProjectPickerItems({ |
| 78 | + items, |
| 79 | + query: menuState.query, |
| 80 | + matches: (item, query) => filter.contains(item, query, (candidate) => candidate.label), |
| 81 | + }), |
| 82 | + [filter, items, menuState.query], |
| 83 | + ); |
| 84 | + const selectedItem = items.find((item) => item.value === props.selectedValue) ?? null; |
| 85 | + // Safari can send a click after Ctrl+click opens settings. Ignore that one |
| 86 | + // selection, then clear the guard when the picker opens again. |
| 87 | + const suppressNextChangeRef = useRef(false); |
| 88 | + const highlightedValueRef = useRef<string | null>(null); |
| 89 | + const openSettings = ( |
| 90 | + event: ReactMouseEvent<HTMLElement> | ReactKeyboardEvent<HTMLInputElement>, |
| 91 | + project: SidebarProjectSnapshot, |
| 92 | + ) => { |
| 93 | + if (!onProjectSettings) return; |
| 94 | + event.preventDefault(); |
| 95 | + event.stopPropagation(); |
| 96 | + suppressNextChangeRef.current = true; |
| 97 | + dispatchMenu({ type: "project-settings-opened" }); |
| 98 | + onProjectSettings(project); |
| 99 | + }; |
| 100 | + |
| 101 | + return ( |
| 102 | + <Combobox |
| 103 | + items={items} |
| 104 | + filteredItems={filteredItems} |
| 105 | + autoHighlight |
| 106 | + itemToStringLabel={(item) => item.label} |
| 107 | + isItemEqualToValue={(a, b) => a.value === b.value} |
| 108 | + open={menuState.open} |
| 109 | + onOpenChange={(open) => { |
| 110 | + if (open) suppressNextChangeRef.current = false; |
| 111 | + dispatchMenu({ type: "open-changed", open }); |
| 112 | + }} |
| 113 | + onItemHighlighted={(item) => { |
| 114 | + highlightedValueRef.current = item?.value ?? null; |
| 115 | + }} |
| 116 | + value={selectedItem} |
| 117 | + onValueChange={(item) => { |
| 118 | + if (suppressNextChangeRef.current) { |
| 119 | + suppressNextChangeRef.current = false; |
| 120 | + return; |
| 121 | + } |
| 122 | + if (!item) return; |
| 123 | + dispatchMenu({ type: "open-changed", open: false }); |
| 124 | + props.onSelect(item); |
| 125 | + }} |
| 126 | + > |
| 127 | + {props.trigger} |
| 128 | + <ComboboxPopup |
| 129 | + align={props.align ?? "start"} |
| 130 | + anchor={props.anchor} |
| 131 | + // At least as wide as its anchor, growing to fit project names up to |
| 132 | + // a cap, past which the rows truncate. |
| 133 | + className="max-w-[min(18rem,var(--available-width))] overflow-hidden" |
| 134 | + > |
| 135 | + <ComboboxSearchInput |
| 136 | + aria-label="Search projects" |
| 137 | + placeholder="Search projects..." |
| 138 | + value={menuState.query} |
| 139 | + onKeyDown={(event) => { |
| 140 | + if ( |
| 141 | + event.defaultPrevented || |
| 142 | + event.nativeEvent.isComposing || |
| 143 | + event.ctrlKey || |
| 144 | + event.altKey || |
| 145 | + event.metaKey || |
| 146 | + (event.key !== "ContextMenu" && !(event.shiftKey && event.key === "F10")) |
| 147 | + ) { |
| 148 | + return; |
| 149 | + } |
| 150 | + // Combobox items use virtual focus: keyboard events stay on this |
| 151 | + // input, not on the highlighted option. |
| 152 | + const project = items.find( |
| 153 | + (item) => item.value === highlightedValueRef.current, |
| 154 | + )?.project; |
| 155 | + if (project) openSettings(event, project); |
| 156 | + }} |
| 157 | + onChange={(event) => dispatchMenu({ type: "query-changed", query: event.target.value })} |
| 158 | + /> |
| 159 | + <ComboboxEmpty>No matching projects.</ComboboxEmpty> |
| 160 | + <ComboboxList> |
| 161 | + {(item: ProjectPickerItem) => { |
| 162 | + const project = item.project; |
| 163 | + return ( |
| 164 | + <ComboboxItem |
| 165 | + key={item.value} |
| 166 | + hideIndicator |
| 167 | + value={item} |
| 168 | + onContextMenu={(event) => { |
| 169 | + if (project) openSettings(event, project); |
| 170 | + }} |
| 171 | + > |
| 172 | + {project ? ( |
| 173 | + <ProjectFavicon project={project} className="size-4 shrink-0" /> |
| 174 | + ) : ( |
| 175 | + item.icon |
| 176 | + )} |
| 177 | + <span className="min-w-0 flex-1 truncate text-sm">{item.label}</span> |
| 178 | + {project && showProjectEnvironments ? ( |
| 179 | + <ProjectEnvironmentBadge |
| 180 | + group={project} |
| 181 | + primaryEnvironmentId={primaryEnvironmentId} |
| 182 | + machineByEnvironmentId={environmentMachineById} |
| 183 | + /> |
| 184 | + ) : null} |
| 185 | + {project && onProjectSettings ? ( |
| 186 | + <Button |
| 187 | + size="icon-xs" |
| 188 | + variant="ghost-muted" |
| 189 | + tabIndex={-1} |
| 190 | + aria-hidden="true" |
| 191 | + title={`Project settings for ${project.displayName}`} |
| 192 | + className="ml-auto" |
| 193 | + onPointerDown={(event) => event.stopPropagation()} |
| 194 | + onClick={(event) => openSettings(event, project)} |
| 195 | + > |
| 196 | + <SettingsIcon className="size-3.5" /> |
| 197 | + </Button> |
| 198 | + ) : null} |
| 199 | + </ComboboxItem> |
| 200 | + ); |
| 201 | + }} |
| 202 | + </ComboboxList> |
| 203 | + </ComboboxPopup> |
| 204 | + </Combobox> |
| 205 | + ); |
| 206 | +} |
0 commit comments