Open History to see the last commit
1import { FolderPlusIcon, SearchIcon, SquarePenIcon, XIcon } from "lucide-react";
2import {
3 type ComponentProps,
4 type KeyboardEvent as ReactKeyboardEvent,
5 type MouseEvent as ReactMouseEvent,
6 type ReactNode,
7 type RefObject,
8} from "react";
9
10import { cn } from "~/lib/utils";
11import { Button } from "../ui/button";
12import { SidebarInput, SidebarMenuButton } from "../ui/sidebar";
13import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
14
15export interface SidebarThreadHeaderProps {
16 hasProjects: boolean;
17 projectScope: ReactNode;
18 onNewProject: () => void;
19 onNewThread: (event: ReactMouseEvent) => void;
20 newThreadDisabled: boolean;
21 newThreadShortcutLabel: string | null | undefined;
22 newThreadInProjectShortcutLabel: string | null | undefined;
23 showNewThreadInProjectHint: boolean;
24 searchInputRef: RefObject<HTMLInputElement | null>;
25 searchQuery: string;
26 onSearchQueryChange: (value: string) => void;
27 onSearchKeyDown: (event: ReactKeyboardEvent<HTMLInputElement>) => void;
28 isSearching: boolean;
29 searchResultCount: number;
30 activeSearchResultIndex: number;
31 onClearSearch: () => void;
32}
33
34export function SidebarThreadHeader({
35 hasProjects,
36 projectScope,
37 onNewProject,
38 onNewThread,
39 newThreadDisabled,
40 newThreadShortcutLabel,
41 newThreadInProjectShortcutLabel,
42 showNewThreadInProjectHint,
43 searchInputRef,
44 searchQuery,
45 onSearchQueryChange,
46 onSearchKeyDown,
47 isSearching,
48 searchResultCount,
49 activeSearchResultIndex,
50 onClearSearch,
51}: SidebarThreadHeaderProps) {
52 const resultsVisible = isSearching && searchResultCount > 0;
53 // Shrinking results can remove the active option; never leave aria-activedescendant pointing at it.
54 const activeResultExists = resultsVisible && activeSearchResultIndex < searchResultCount;
55 const newThreadLabel = newThreadShortcutLabel
56 ? `New thread (${newThreadShortcutLabel})`
57 : "New thread";
58
59 return (
60 <div className="flex items-center gap-1">
61 <div className="flex h-8 min-w-0 flex-1 items-center gap-2 rounded-md px-2 py-1.5 text-sm font-medium text-sidebar-muted-foreground hover:bg-sidebar-row-hover hover:text-sidebar-foreground focus-within:bg-sidebar-row-hover focus-within:text-sidebar-foreground focus-within:ring-1 focus-within:ring-inset focus-within:ring-ring/70">
62 <SearchIcon className="size-4 shrink-0 text-(--sidebar-icon-color)" />
63 <SidebarInput
64 ref={searchInputRef}
65 nativeInput
66 type="search"
67 value={searchQuery}
68 onChange={(event) => onSearchQueryChange(event.currentTarget.value)}
69 onKeyDown={onSearchKeyDown}
70 placeholder="Search"
71 aria-label="Search threads"
72 role="combobox"
73 aria-autocomplete="list"
74 aria-expanded={resultsVisible}
75 aria-controls={resultsVisible ? "sidebar-thread-search-results" : undefined}
76 aria-activedescendant={
77 activeResultExists
78 ? `sidebar-thread-search-result-${activeSearchResultIndex}`
79 : undefined
80 }
81 className="min-w-0 flex-1"
82 />
83 {isSearching ? (
84 <Button
85 type="button"
86 size="icon-micro"
87 variant="ghost-muted"
88 className="shrink-0"
89 aria-label="Clear thread search"
90 onClick={() => {
91 onClearSearch();
92 searchInputRef.current?.focus();
93 }}
94 >
95 <XIcon className="size-3" />
96 </Button>
97 ) : null}
98 </div>
99 <div className="flex shrink-0 items-center">
100 {hasProjects ? (
101 <>
102 {projectScope}
103 <SidebarHeaderIconButton label="Add project" onClick={onNewProject}>
104 <FolderPlusIcon />
105 </SidebarHeaderIconButton>
106 </>
107 ) : null}
108 <SidebarHeaderIconButton
109 label="New thread"
110 tooltip={
111 showNewThreadInProjectHint ? (
112 <span className="flex flex-col gap-0.5">
113 <span>{newThreadLabel}</span>
114 <span className="text-muted-foreground">
115 New thread in current project: Shift+click
116 {newThreadInProjectShortcutLabel ? ` (${newThreadInProjectShortcutLabel})` : ""}
117 </span>
118 </span>
119 ) : (
120 newThreadLabel
121 )
122 }
123 disabled={newThreadDisabled}
124 onClick={onNewThread}
125 >
126 <SquarePenIcon />
127 </SidebarHeaderIconButton>
128 </div>
129 </div>
130 );
131}
132
133export function SidebarHeaderIconButton({
134 label,
135 tooltip = label,
136 className,
137 children,
138 ...rest
139}: {
140 label: string;
141 tooltip?: ReactNode;
142 className?: string | undefined;
143 children?: ReactNode;
144} & Omit<
145 ComponentProps<typeof SidebarMenuButton>,
146 "children" | "className" | "tooltip" | "isActive" | "aria-label"
147>) {
148 return (
149 <Tooltip>
150 <TooltipTrigger
151 render={
152 <SidebarMenuButton
153 size="icon"
154 type="button"
155 aria-label={label}
156 {...rest}
157 className={cn("relative size-7 shrink-0", className)}
158 />
159 }
160 >
161 {children}
162 <span
163 aria-hidden
164 className="pointer-events-none absolute left-1/2 top-1/2 size-[max(100%,3rem)] -translate-1/2 pointer-fine:hidden"
165 />
166 </TooltipTrigger>
167 <TooltipPopup side="top">{tooltip}</TooltipPopup>
168 </Tooltip>
169 );
170}