/** * The sidebar header: one row holding search, project scope and new thread. * * Search owns the row's text and spans it. Project scope collapses to an icon * that sits with new-project and new-thread as a segmented group at the end. * The scope icon swaps to the project favicon while a project is selected, * so the header still names the scope after the row that showed it is gone. * * The scope picker itself is passed in: its combobox state lives with the rest * of the sidebar's scope logic. `searchFieldRef` lands on the search field so * the picker's popup can anchor to that width rather than to its 28px trigger. */ import { FolderPlusIcon, SearchIcon, SquarePenIcon, XIcon } from "lucide-react"; import { type ComponentProps, type KeyboardEvent as ReactKeyboardEvent, type MouseEvent as ReactMouseEvent, type ReactNode, type RefObject, } from "react"; import { cn } from "~/lib/utils"; import { useCompactSidebarEnabled } from "../../hooks/useSettings"; import { Button } from "../ui/button"; import { Input } from "../ui/input"; import { SidebarMenuButton, useSidebar } from "../ui/sidebar"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; export interface SidebarThreadHeaderProps { /** Lands on the search field so a popup can anchor to its width. */ searchFieldRef?: RefObject; /** Without projects there is nothing to scope, so those controls stay out. */ hasProjects: boolean; /** The project scope combobox, rendered as the first icon of the group. */ projectScope: ReactNode; onNewProject: () => void; /** Receives the click so Shift+click can skip the project picker. */ onNewThread: (event: ReactMouseEvent) => void; newThreadDisabled: boolean; newThreadShortcutLabel: string | null | undefined; newThreadInProjectShortcutLabel: string | null | undefined; /** Shift+click only matters once there is more than one project to pick. */ showNewThreadInProjectHint: boolean; searchInputRef: RefObject; searchQuery: string; onSearchQueryChange: (value: string) => void; onSearchKeyDown: (event: ReactKeyboardEvent) => void; isSearching: boolean; searchResultCount: number; activeSearchResultIndex: number; onClearSearch: () => void; } export function SidebarThreadHeader({ searchFieldRef, hasProjects, projectScope, onNewProject, onNewThread, newThreadDisabled, newThreadShortcutLabel, newThreadInProjectShortcutLabel, showNewThreadInProjectHint, searchInputRef, searchQuery, onSearchQueryChange, onSearchKeyDown, isSearching, searchResultCount, activeSearchResultIndex, onClearSearch, }: SidebarThreadHeaderProps) { const compactEnabled = useCompactSidebarEnabled(); const { state, isMobile, setOpen } = useSidebar(); const compact = compactEnabled && state === "collapsed" && !isMobile; const resultsVisible = isSearching && searchResultCount > 0; // Results shrink as the query narrows, so the active index can outrun the // list; pointing aria-activedescendant at a removed option strands the // screen reader on nothing. const activeResultExists = resultsVisible && activeSearchResultIndex < searchResultCount; const newThreadLabel = newThreadShortcutLabel ? `New thread (${newThreadShortcutLabel})` : "New thread"; return (
{compact ? ( { setOpen(true); requestAnimationFrame(() => searchInputRef.current?.focus()); }} > ) : null}
onSearchQueryChange(event.currentTarget.value)} onKeyDown={onSearchKeyDown} placeholder="Search" aria-label="Search threads" role="combobox" aria-autocomplete="list" aria-expanded={resultsVisible} aria-controls={resultsVisible ? "sidebar-thread-search-results" : undefined} aria-activedescendant={ activeResultExists ? `sidebar-thread-search-result-${activeSearchResultIndex}` : undefined } className="min-w-0 flex-1 [&_[data-slot=input]]:h-auto [&_[data-slot=input]]:p-0 [&_[data-slot=input]]:leading-normal [&_[data-slot=input]]:text-sm [&_[data-slot=input]]:font-medium [&_[data-slot=input]]:text-sidebar-foreground [&_[data-slot=input]]:placeholder:text-[var(--sidebar-icon-color)]" /> {isSearching ? ( ) : null}
{/* Segmented well: the icons read as one control instead of three loose buttons competing with the search field beside them. */}
{hasProjects ? ( <> {projectScope} ) : null} {newThreadLabel} New thread in current project: Shift+click {newThreadInProjectShortcutLabel ? ` (${newThreadInProjectShortcutLabel})` : ""} ) : ( newThreadLabel ) } disabled={newThreadDisabled} onClick={onNewThread} >
); } /** * Icon button with a tooltip, sized for the header's segmented pair. Spreads * unknown props through so it can serve as a popup trigger's render target, * which injects its own handlers, ref and aria state. */ export function SidebarHeaderIconButton({ label, tooltip = label, className, children, ...rest }: { /** Accessible name; also the tooltip unless `tooltip` says more. */ label: string; tooltip?: ReactNode; className?: string | undefined; children?: ReactNode; } & Omit< ComponentProps, "children" | "className" | "tooltip" | "isActive" | "aria-label" >) { return ( } > {children} {/* Coarse-pointer hit area, matching the rest of the sidebar chrome. */} {tooltip} ); }