import { ActionIcon, Combobox, Group, InputBase, Text, useCombobox } from "@mantine/core"; import { IconCheck, IconChevronDown, IconFolder, IconSearch, IconHistory, IconSortAscendingLetters, IconSortDescendingLetters } from "@tabler/icons-react"; import { useState } from "react"; export interface ProjectOption { value: string; label: string; path?: string; updatedAt?: number; } export function ProjectPicker({ data, value, onChange }: { data: ProjectOption[]; value: string; onChange: (value: string) => void }) { const [search, setSearch] = useState(""); const [sort, setSort] = useState<"recent" | "az" | "za">("recent"); const sortLabel = sort === "recent" ? "Most recent" : sort === "az" ? "A–Z" : "Z–A"; const nextLabel = sort === "recent" ? "A–Z" : sort === "az" ? "Z–A" : "Most recent"; const SortIcon = sort === "recent" ? IconHistory : sort === "az" ? IconSortAscendingLetters : IconSortDescendingLetters; const store = useCombobox({ onDropdownClose: () => { store.resetSelectedOption(); setSearch(""); store.focusTarget(); }, onDropdownOpen: () => { store.focusSearchInput(); }, }); const selected = data.find((item) => item.value === value); const query = search.trim().toLocaleLowerCase(); const ordered = [...data].sort((a, b) => { const rank = (item: ProjectOption) => item.value === "" ? 0 : item.value === "projectless" ? 2 : 1; if (rank(a) !== rank(b)) return rank(a) - rank(b); const alphabet = a.label.localeCompare(b.label, undefined, { sensitivity: "base", numeric: true }) || a.value.localeCompare(b.value); return sort === "recent" ? (b.updatedAt ?? 0) - (a.updatedAt ?? 0) || alphabet : sort === "az" ? alphabet : -alphabet; }); const options = ordered.filter((item) => `${item.label} ${item.path ?? ""}`.toLocaleLowerCase().includes(query)); return { onChange(String(next)); store.closeDropdown(); }} position="bottom-start" width={310} withinPortal> store.toggleDropdown()} leftSection={} rightSection={} className="next-project-trigger"> {selected?.label ?? "All projects"} } rightSectionPointerEvents="all" rightSectionWidth={34} rightSection={ event.preventDefault()} onClick={() => { setSort(sort === "recent" ? "az" : sort === "az" ? "za" : "recent"); store.resetSelectedOption(); }}> } onChange={(event) => { setSearch(event.currentTarget.value); store.resetSelectedOption(); }} /> {options.map((item) => {item.label}{item.value === value && } )} {!options.length && No projects found} ; }