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