-
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathsidebar-view-popover.tsx
More file actions
321 lines (310 loc) · 8.96 KB
/
Copy pathsidebar-view-popover.tsx
File metadata and controls
321 lines (310 loc) · 8.96 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import {
ArrowDownAZ,
Calendar,
Check,
ChevronsUpDown,
Clock3,
Folder,
FolderGit2,
GripVertical,
ListFilter,
type LucideIcon,
Rows3,
} from "lucide-react";
import { useCallback, useMemo } from "react";
import { Button } from "@/components/ui/button";
import {
CommandEmpty,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command";
import { CommandPopoverContent } from "@/components/ui/command-popover";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { InlineShortcutDisplay } from "@/features/shortcuts/shortcut-display";
import type { RepositoryCreateOption } from "@/lib/api";
import type { SidebarGrouping, SidebarSort } from "@/lib/settings";
import { WorkspaceAvatar } from "./avatar";
interface SidebarSortOption {
value: SidebarSort;
label: string;
icon: LucideIcon;
}
interface SidebarRepoFilterPickerProps {
repositories: RepositoryCreateOption[];
selectedRepoIds: string[];
onRepoFilterChange?: (repoIds: string[]) => void;
}
interface SidebarViewPopoverProps {
repositories: RepositoryCreateOption[];
grouping: SidebarGrouping;
selectedRepoIds: string[];
sort: SidebarSort;
open: boolean;
onOpenChange: (open: boolean) => void;
shortcut?: string | null;
onGroupingChange?: (grouping: SidebarGrouping) => void;
onRepoFilterChange?: (repoIds: string[]) => void;
onSortChange?: (sort: SidebarSort) => void;
}
const SIDEBAR_SORT_OPTIONS: SidebarSortOption[] = [
{ value: "custom", label: "Draggable order", icon: GripVertical },
{ value: "repoName", label: "Repository name", icon: ArrowDownAZ },
{ value: "updatedAt", label: "Last updated", icon: Clock3 },
{ value: "createdAt", label: "Created time", icon: Calendar },
];
function repoFilterLabel(
repositories: RepositoryCreateOption[],
selectedRepoIds: string[],
) {
if (selectedRepoIds.length === 0) return "All repositories";
if (selectedRepoIds.length === 1) {
return (
repositories.find((repo) => repo.id === selectedRepoIds[0])?.name ??
"1 selected"
);
}
return `${selectedRepoIds.length} selected`;
}
function repoCommandValue(repo: RepositoryCreateOption) {
return [repo.name, repo.rootPath, repo.id].filter(Boolean).join(" ");
}
function hasDuplicateRepoName(
repositories: RepositoryCreateOption[],
repo: RepositoryCreateOption,
) {
const repoName = repo.name.trim().toLocaleLowerCase();
return (
repoName.length > 0 &&
repositories.some(
(other) =>
other.id !== repo.id &&
other.name.trim().toLocaleLowerCase() === repoName,
)
);
}
function SidebarRepoFilterPicker({
repositories,
selectedRepoIds,
onRepoFilterChange,
}: SidebarRepoFilterPickerProps) {
const sortedRepositories = useMemo(
() =>
[...repositories].sort((left, right) => {
const byName = left.name.localeCompare(right.name, undefined, {
sensitivity: "base",
});
if (byName !== 0) return byName;
const byRootPath = (left.rootPath ?? "").localeCompare(
right.rootPath ?? "",
undefined,
{ sensitivity: "base" },
);
if (byRootPath !== 0) return byRootPath;
return left.id.localeCompare(right.id);
}),
[repositories],
);
const selectedRepoIdSet = useMemo(
() => new Set(selectedRepoIds),
[selectedRepoIds],
);
const label = repoFilterLabel(sortedRepositories, selectedRepoIds);
const toggleRepository = useCallback(
(repoId: string) => {
const next = new Set(selectedRepoIds);
if (next.has(repoId)) {
next.delete(repoId);
} else {
next.add(repoId);
}
onRepoFilterChange?.(Array.from(next));
},
[onRepoFilterChange, selectedRepoIds],
);
return (
<Popover>
<PopoverTrigger asChild>
<Button
type="button"
variant="outline"
className="h-8 w-full justify-between rounded-md border-border/70 bg-background/40 px-2 text-[13px] font-normal"
>
<span className="truncate">{label}</span>
<ChevronsUpDown className="size-3.5 shrink-0 text-muted-foreground" />
</Button>
</PopoverTrigger>
<CommandPopoverContent
align="start"
sideOffset={4}
className="w-(--radix-popover-trigger-width)"
commandClassName="max-h-[320px]"
>
<CommandInput placeholder="Search repositories" />
<CommandList>
<CommandEmpty>No repositories found.</CommandEmpty>
<CommandItem
value="all repositories"
data-checked={selectedRepoIds.length === 0}
onSelect={() => onRepoFilterChange?.([])}
>
<Folder className="size-4 shrink-0 text-muted-foreground" />
<span className="truncate">All repositories</span>
</CommandItem>
{sortedRepositories.map((repo) => {
const checked = selectedRepoIdSet.has(repo.id);
const showRootPath = Boolean(
repo.rootPath && hasDuplicateRepoName(sortedRepositories, repo),
);
return (
<CommandItem
key={repo.id}
value={repoCommandValue(repo)}
data-checked={checked}
onSelect={() => toggleRepository(repo.id)}
>
<WorkspaceAvatar
repoIconSrc={repo.repoIconSrc}
repoInitials={repo.repoInitials ?? null}
repoName={repo.name}
title={repo.name}
/>
<span className="flex min-w-0 flex-1 flex-col">
<span className="truncate">{repo.name}</span>
{showRootPath ? (
<span className="break-all text-[11px] leading-4 text-muted-foreground">
{repo.rootPath}
</span>
) : null}
</span>
</CommandItem>
);
})}
</CommandList>
</CommandPopoverContent>
</Popover>
);
}
export function SidebarViewPopover({
repositories,
grouping,
selectedRepoIds,
sort,
open,
onOpenChange,
shortcut,
onGroupingChange,
onRepoFilterChange,
onSortChange,
}: SidebarViewPopoverProps) {
return (
<Popover open={open} onOpenChange={onOpenChange}>
<Tooltip>
<TooltipTrigger asChild>
<PopoverTrigger asChild>
<Button
type="button"
aria-label="Filter and sort sidebar"
variant="ghost"
size="icon-xs"
className="text-muted-foreground"
>
<ListFilter className="size-4" strokeWidth={2} />
</Button>
</PopoverTrigger>
</TooltipTrigger>
<TooltipContent
side="top"
sideOffset={4}
className="flex h-[24px] items-center gap-2 rounded-md px-2 text-[12px] leading-none"
>
<span>Filter and sort</span>
{shortcut ? (
<InlineShortcutDisplay
hotkey={shortcut}
className="text-background/60"
/>
) : null}
</TooltipContent>
</Tooltip>
<PopoverContent align="start" className="w-[260px] gap-2 p-2">
<div className="grid gap-1 px-1">
<div className="text-[12px] font-medium text-muted-foreground">
Repository
</div>
<SidebarRepoFilterPicker
repositories={repositories}
selectedRepoIds={selectedRepoIds}
onRepoFilterChange={onRepoFilterChange}
/>
</div>
<div className="h-px bg-border/60" />
<div className="px-1 text-[12px] font-medium text-muted-foreground">
Group by
</div>
<div className="grid gap-0.5">
{[
{ value: "status", label: "Status", icon: Rows3 },
{ value: "repo", label: "Repository", icon: FolderGit2 },
].map((option) => {
const Icon = option.icon;
const checked = grouping === option.value;
return (
<button
key={option.value}
type="button"
role="radio"
aria-checked={checked}
className="flex h-7 w-full cursor-pointer items-center gap-2 rounded-md px-1.5 text-left text-[13px] hover:bg-accent/70 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring/50"
onClick={() =>
onGroupingChange?.(option.value as SidebarGrouping)
}
>
<Icon className="size-3.5 shrink-0 text-muted-foreground" />
<span className="min-w-0 flex-1 truncate">{option.label}</span>
{checked ? (
<Check className="size-3.5" strokeWidth={2.2} />
) : null}
</button>
);
})}
</div>
<div className="h-px bg-border/60" />
<div className="px-1 text-[12px] font-medium text-muted-foreground">
Sort by
</div>
<div className="grid gap-0.5">
{SIDEBAR_SORT_OPTIONS.map((option) => {
const Icon = option.icon;
const checked = sort === option.value;
return (
<button
key={option.value}
type="button"
role="radio"
aria-checked={checked}
className="flex h-7 w-full cursor-pointer items-center gap-2 rounded-md px-1.5 text-left text-[13px] hover:bg-accent/70 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring/50"
onClick={() => onSortChange?.(option.value)}
>
<Icon className="size-3.5 shrink-0 text-muted-foreground" />
<span className="min-w-0 flex-1 truncate">{option.label}</span>
{checked ? (
<Check className="size-3.5" strokeWidth={2.2} />
) : null}
</button>
);
})}
</div>
</PopoverContent>
</Popover>
);
}