@bridgebrew/project 0.0.0-stage → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,642 @@
1
+ // A git project's Branches screen (research-083): its default branch (the project's own folder) and each
2
+ // branch in its worktree (`.worktrees/<name>`), one line each in the kit's Table: its name, then marks in slots of their
3
+ // own (Active, its commits behind | ahead of the default branch, its uncommitted changes as GitHub's diffstat, its pull
4
+ // request; commits to push or pull show through the Git button), then its actions, right-aligned in one order: the Git
5
+ // button and its menu (`@bridge/session-ui/git-actions`), Activate, and "…" for the rest (Delete, with why it is
6
+ // refused). New branch starts one from the default branch; a selected branch takes a prompt whose session works on it.
7
+ // Activate makes a branch the active one: everything Bridge loads from the project (its plugins, host-level code such as
8
+ // the Bridge UI kit) follows it, without a restart. Data and actions from this plugin's `/branches` routes
9
+ // (../branches.ts, bridgebrew/bridge#557). New, Push, Pull, Create PR and Activate may run on past their route: the
10
+ // row says which runs, then why it failed, and the screen reads the branches again until it ends. Without the GitHub
11
+ // plugin, every git action works and a notice offers to install it for pull requests.
12
+ import * as React from 'react';
13
+ import {
14
+ Badge,
15
+ Button,
16
+ Dialog,
17
+ DialogBody,
18
+ DialogContent,
19
+ DialogDescription,
20
+ DialogFooter,
21
+ DialogHeader,
22
+ DialogTitle,
23
+ DropdownMenu,
24
+ DropdownMenuContent,
25
+ DropdownMenuItem,
26
+ DropdownMenuTrigger,
27
+ EmptyState,
28
+ Icon,
29
+ Input,
30
+ Progress,
31
+ Table,
32
+ TableBody,
33
+ TableCell,
34
+ TableHead,
35
+ TableHeader,
36
+ TableRow,
37
+ cn,
38
+ copyText,
39
+ isRecord,
40
+ readJson,
41
+ useToast,
42
+ } from '@bridge/ui';
43
+ import type { Context } from 'cordis';
44
+ import type {} from '@bridge/ui-renderer/client';
45
+ import { SessionPrompt } from '@bridge/session-ui/prompt';
46
+ import {
47
+ GitActions,
48
+ type BranchesAnswer,
49
+ type BranchesRoute,
50
+ type GitBranch,
51
+ } from '@bridge/session-ui/git-actions';
52
+
53
+ /** This plugin's Branches route of project `node` in workspace `slug`, or one of its actions. */
54
+ export const projectBranchesRoute: BranchesRoute = (slug, node, action) =>
55
+ `/${encodeURIComponent(slug)}/api/project/branches${action ? `/${action}` : ''}?node=${encodeURIComponent(node)}`;
56
+
57
+ /** An action that may run on past its route: running, or how it ended. */
58
+ interface Job {
59
+ action: string;
60
+ running: boolean;
61
+ error?: string;
62
+ }
63
+ interface View extends BranchesAnswer {
64
+ project: { title: string; folder: string };
65
+ /** Whether the workspace runs the GitHub plugin, which pull requests need. */
66
+ github?: 'ready' | 'missing';
67
+ /** Host-level code loaded from the project: why Activate is refused (an installed Bridge), its last build's error. */
68
+ hostLevel?: { refusal?: string; error?: string };
69
+ branches: (GitBranch & { job?: Job })[];
70
+ /** New branches being made, by name. */
71
+ jobs?: (Job & { name: string })[];
72
+ /** A fetch or a GitHub read runs: the next reading shows what it read. */
73
+ refreshing?: boolean;
74
+ }
75
+ /** What each action is called while it runs, and when it failed. */
76
+ const JOB_WORDS: Record<string, [running: string, failed: string]> = {
77
+ new: ['Preparing…', 'Not made'],
78
+ push: ['Pushing…', 'Push failed'],
79
+ pull: ['Pulling…', 'Pull failed'],
80
+ pr: ['Opening PR…', 'PR failed'],
81
+ activate: ['Activating…', 'Activate failed'],
82
+ };
83
+ /** How often the screen reads the branches again while an action runs on. */
84
+ const POLL_MS = 2_000;
85
+
86
+ /** Name, Active, behind | ahead of the default branch, uncommitted changes, pull request, actions: each mark in a slot
87
+ * of its own, so it never moves. Changes takes the width left before the name (it grows to 196px first), so a 1024
88
+ * window still fits every column. */
89
+ const COLUMNS = 'minmax(72px,1fr) 124px minmax(104px,196px) 76px 300px';
90
+
91
+ const plural = (count: number, word: string) => `${count} ${word}${count === 1 ? '' : 's'}`;
92
+ /** A non-zero count's share of the bar's half: at least a sliver, so 1 of 300 still shows. */
93
+ const share = (count: number, scale: number) => (count > 0 ? Math.max(count / scale, 0.08) : 0);
94
+ /** The bar's half, a kit Progress on no track: aria-hidden, the cell's label says it in words. */
95
+ const HALF = 'h-1 bg-transparent';
96
+
97
+ /** Its commits behind | ahead of the default branch `base`, as GitHub's branch list: two numbers split by a thin
98
+ * divider, and a bar under them, behind (muted) growing left from it and ahead (accent) right, each half scaled to
99
+ * `scale` (the largest count on the screen). The default branch itself shows "—". */
100
+ export function VsDefault({
101
+ branch,
102
+ base,
103
+ scale,
104
+ }: {
105
+ branch: Pick<GitBranch, 'isDefault' | 'ahead' | 'behindDefault' | 'unknown'>;
106
+ base: string;
107
+ scale: number;
108
+ }) {
109
+ if (branch.unknown) return null;
110
+ if (branch.isDefault) return <span className="w-full text-center text-muted">—</span>;
111
+ const label = `${plural(branch.behindDefault, 'commit')} behind ${base}, ${branch.ahead} ahead`;
112
+ return (
113
+ <span
114
+ role="img"
115
+ aria-label={label}
116
+ title={label}
117
+ className="grid w-full grid-cols-[1fr_1px_1fr] items-center gap-x-1.5 text-xs tabular-nums"
118
+ data-vs-default
119
+ >
120
+ <span className="text-right text-secondary">{branch.behindDefault}</span>
121
+ <span className="row-span-2 h-full bg-border" data-divider />
122
+ <span className="text-secondary">{branch.ahead}</span>
123
+ <Progress
124
+ aria-hidden
125
+ value={share(branch.behindDefault, scale)}
126
+ size="sm"
127
+ data-behind
128
+ // Mirrored, so it grows left from the divider; muted, as GitHub's behind part.
129
+ className={cn(HALF, '-scale-x-100 [&>[data-slot=progress-bar]]:bg-muted')}
130
+ />
131
+ <Progress
132
+ aria-hidden
133
+ value={share(branch.ahead, scale)}
134
+ size="sm"
135
+ data-ahead
136
+ className={HALF}
137
+ />
138
+ </span>
139
+ );
140
+ }
141
+
142
+ /** GitHub's diffstat blocks: 5, shared between additions (green) and deletions (red) by their lines; under 5 lines,
143
+ * one block a line, the rest grey. */
144
+ export const diffBlocks = (additions: number, deletions: number) => {
145
+ const total = additions + deletions;
146
+ const added = total < 5 ? additions : Math.round((5 * additions) / total);
147
+ const deleted = total < 5 ? deletions : 5 - added;
148
+ return [
149
+ ...Array<string>(added).fill('bg-success'),
150
+ ...Array<string>(deleted).fill('bg-destructive'),
151
+ ...Array<string>(5 - added - deleted).fill('bg-border'),
152
+ ];
153
+ };
154
+
155
+ /** A branch's uncommitted changes as GitHub's diffstat, each part in a fixed slot: its files, `+a −d` in the diff
156
+ * colours (as a session's changed files), then the 5 blocks. Empty when nothing changed. */
157
+ export function DiffStat({ branch }: { branch: Pick<GitBranch, 'diff' | 'unknown'> }) {
158
+ if (branch.unknown) return <span className="text-secondary">Unknown</span>;
159
+ const { files, additions, deletions } = branch.diff;
160
+ if (!files) return null;
161
+ const label = `${plural(files, 'file')} changed, ${plural(additions, 'addition')}, ${plural(deletions, 'deletion')}`;
162
+ return (
163
+ // Narrow (a 1024 window), its files count leaves: the label and the title keep it.
164
+ <span className="@container w-full">
165
+ <span
166
+ role="img"
167
+ aria-label={label}
168
+ title={label}
169
+ data-diff-stat
170
+ className="grid grid-cols-[52px_1fr_auto] items-center gap-x-1.5 text-xs tabular-nums @max-[150px]:grid-cols-[1fr_auto]"
171
+ >
172
+ <span
173
+ data-slot-diff="files"
174
+ className="whitespace-nowrap text-secondary @max-[150px]:hidden"
175
+ >
176
+ {plural(files, 'file')}
177
+ </span>
178
+ <span data-slot-diff="counts" className="truncate font-mono">
179
+ <span className="text-success">+{additions}</span>{' '}
180
+ <span className="text-destructive">−{deletions}</span>
181
+ </span>
182
+ <span data-slot-diff="blocks" className="flex gap-px">
183
+ {diffBlocks(additions, deletions).map((colour, index) => (
184
+ <span
185
+ key={index}
186
+ className={cn('size-2 rounded-[2px] @max-[150px]:size-1.5', colour)}
187
+ />
188
+ ))}
189
+ </span>
190
+ </span>
191
+ </span>
192
+ );
193
+ }
194
+
195
+ /** Why Delete is refused, if it is. */
196
+ const keeps = (branch: GitBranch) =>
197
+ branch.main
198
+ ? 'The project’s own folder is never deleted.'
199
+ : branch.active
200
+ ? 'Active: activate another branch first.'
201
+ : branch.dirty || branch.unpushed
202
+ ? 'Holds work not pushed.'
203
+ : undefined;
204
+
205
+ /** A pull request's state as GitHub shows it: its icon and colour (open green, draft gray, merged purple as the
206
+ * theme's accent, closed red), from the kit's semantic colours. */
207
+ const PR_LOOK = {
208
+ open: { icon: 'git-pull-request-arrow', className: 'text-success', label: 'Open' },
209
+ draft: { icon: 'git-pull-request-draft', className: 'text-muted', label: 'Draft' },
210
+ merged: { icon: 'git-merge', className: 'text-merged', label: 'Merged' },
211
+ closed: { icon: 'git-pull-request-closed', className: 'text-destructive', label: 'Closed' },
212
+ } as const;
213
+
214
+ /** A branch's pull request as GitHub shows it: its state's icon and its number, a pill linking to it (a new tab). */
215
+ export function PrPill({
216
+ url,
217
+ branch,
218
+ }: {
219
+ url: string;
220
+ branch: Pick<GitBranch, 'prState' | 'prNumber'>;
221
+ }) {
222
+ const state = branch.prState ?? 'open';
223
+ const label = `Pull request${branch.prNumber ? ` #${branch.prNumber}` : ''} · ${PR_LOOK[state].label}`;
224
+ return (
225
+ <Button
226
+ size="sm"
227
+ // Not `property`: it mutes every icon inside, and the icon carries the state's colour.
228
+ variant="outline"
229
+ title={label}
230
+ aria-label={label}
231
+ className="rounded-full tabular-nums"
232
+ render={<a href={url} target="_blank" rel="noreferrer" />}
233
+ onClick={(event) => event.stopPropagation()}
234
+ >
235
+ <Icon name={PR_LOOK[state].icon} size={13} className={PR_LOOK[state].className} />
236
+ {branch.prNumber ? `#${branch.prNumber}` : 'PR'}
237
+ </Button>
238
+ );
239
+ }
240
+
241
+ export function Branches({ ctx, node }: { ctx: Context; node: string }) {
242
+ const { toast } = useToast();
243
+ const slug = ctx.workspace.slug;
244
+ const [view, setView] = React.useState<View>();
245
+ const [error, setError] = React.useState('');
246
+ const [title, setTitle] = React.useState('');
247
+ const [naming, setNaming] = React.useState(false);
248
+ const [selected, setSelected] = React.useState<string>();
249
+ const [running, setRunning] = React.useState<{ action: string; name?: string }>();
250
+ const [installing, setInstalling] = React.useState(false);
251
+ const [reads, setReads] = React.useState(0);
252
+ React.useEffect(() => {
253
+ let gone = false;
254
+ fetch(projectBranchesRoute(slug, node), { cache: 'no-store' })
255
+ .then(readJson)
256
+ .then((body: unknown) => {
257
+ if (gone) return;
258
+ // A project that is not a git repository of its own has no branches: that state, in place of the list.
259
+ if (isRecord(body) && body.repository === false && isRecord(body.project))
260
+ return setError(`${String(body.project.title)} is not a git repository`);
261
+ // A node no bridge.yml lists as a project (one made before projects had one): no branches either.
262
+ if (isRecord(body) && body.project === null)
263
+ return setError('No bridge.yml lists this project: Bridge shows no branches for it');
264
+ // The host's own answer: typed once where it is read.
265
+ setView(body as View);
266
+ })
267
+ .catch((cause: unknown) => {
268
+ if (!gone) setError(cause instanceof Error ? cause.message : String(cause));
269
+ });
270
+ return () => {
271
+ gone = true;
272
+ };
273
+ }, [slug, node, reads]);
274
+ // While an action runs on past its route, the branches are read again until it ends.
275
+ const runsOn =
276
+ !!view?.refreshing ||
277
+ !!view?.branches.some((branch) => branch.job?.running) ||
278
+ !!view?.jobs?.some((job) => job.running);
279
+ React.useEffect(() => {
280
+ if (!runsOn) return;
281
+ const timer = setTimeout(() => setReads((count) => count + 1), POLL_MS);
282
+ return () => clearTimeout(timer);
283
+ }, [runsOn, view]);
284
+ /** Installs the GitHub plugin in this workspace (the Plugins screen's own install), then reads the branches again. */
285
+ const installGitHub = async () => {
286
+ setInstalling(true);
287
+ try {
288
+ const res = await fetch(`/_bridge/workspaces/${encodeURIComponent(slug)}/plugins/install`, {
289
+ method: 'POST',
290
+ headers: { 'content-type': 'application/json' },
291
+ body: JSON.stringify({ source: '@bridgebrew/github' }),
292
+ });
293
+ await readJson(res);
294
+ toast({ variant: 'success', title: 'GitHub installed' });
295
+ setReads((count) => count + 1);
296
+ } catch (cause) {
297
+ toast({
298
+ variant: 'error',
299
+ title: 'GitHub is not installed',
300
+ description: cause instanceof Error ? cause.message : String(cause),
301
+ });
302
+ } finally {
303
+ setInstalling(false);
304
+ }
305
+ };
306
+ /** Posts `action`; shows the branches it answers. */
307
+ const run = async (action: string, body: Record<string, unknown>, branch?: string) => {
308
+ setRunning({ action, ...(branch !== undefined && { name: branch }) });
309
+ try {
310
+ // The host's own answer: typed once where it is read.
311
+ const answer = (await readJson(
312
+ await fetch(projectBranchesRoute(slug, node, action), {
313
+ method: 'POST',
314
+ headers: { 'content-type': 'application/json' },
315
+ body: JSON.stringify(body),
316
+ }),
317
+ )) as View & {
318
+ result?: { name?: string; running?: string; left?: { plugin: string; reason: string }[] };
319
+ };
320
+ setView(answer);
321
+ if (action === 'new') {
322
+ setTitle('');
323
+ setNaming(false);
324
+ if (answer.result?.name) setSelected(answer.result.name);
325
+ }
326
+ if (answer.result?.running)
327
+ toast({
328
+ title: `${JOB_WORDS[answer.result.running]?.[0] ?? 'Running…'}`,
329
+ description: 'It runs on: its row says when it ends.',
330
+ });
331
+ if (action === 'activate' && answer.result?.left?.length)
332
+ toast({
333
+ variant: 'error',
334
+ title: 'Some code stays on its branch',
335
+ description: answer.result.left
336
+ .map((each) => `${each.plugin}: ${each.reason}`)
337
+ .join('\n'),
338
+ });
339
+ } catch (cause) {
340
+ toast({
341
+ variant: 'error',
342
+ title: 'Not done',
343
+ description: cause instanceof Error ? cause.message : String(cause),
344
+ });
345
+ } finally {
346
+ setRunning(undefined);
347
+ }
348
+ };
349
+ if (error) return <EmptyState icon={<Icon name="git-branch" size={20} />} title={error} fill />;
350
+ if (!view) return <section aria-label="Screen" className="flex h-full min-h-0 flex-col" />;
351
+ const busy = running !== undefined;
352
+ const spinning = (action: string, branch?: string) =>
353
+ running?.action === action && running.name === branch;
354
+ const current = view.branches.find((branch) => branch.name === selected && !branch.main);
355
+ // Bridge loading the project's code from several branches at once (a restart loads what each place kept): Activate
356
+ // stays offered on each, to bring it all on one.
357
+ const mixed = view.branches.filter((branch) => branch.active).length > 1;
358
+ const base = view.branches.find((branch) => branch.isDefault)?.name ?? 'default';
359
+ const scale = Math.max(
360
+ 1,
361
+ ...view.branches
362
+ .filter((branch) => !branch.isDefault && !branch.unknown)
363
+ .map((branch) => Math.max(branch.ahead, branch.behindDefault)),
364
+ );
365
+ return (
366
+ <section aria-label="Screen" className="flex h-full min-h-0 flex-col" data-branches>
367
+ <div className="flex min-h-0 flex-1 flex-col gap-4 overflow-auto px-6 pb-6 pt-4">
368
+ <header className="flex items-center gap-3">
369
+ <h1 className="flex-1 text-xl font-semibold">Branches</h1>
370
+ <Button variant="primary" disabled={busy} onClick={() => setNaming(true)}>
371
+ <Icon
372
+ name={spinning('new') ? 'loader-circle' : 'git-branch'}
373
+ className={cn(spinning('new') && 'animate-spin motion-reduce:animate-none')}
374
+ />
375
+ {spinning('new') ? 'Preparing…' : 'New branch'}
376
+ </Button>
377
+ </header>
378
+ {view.github === 'missing' && (
379
+ <p className="m-0 flex items-center gap-1.5 rounded-md border border-border px-3 py-2 text-xs text-secondary">
380
+ <Icon name="info" size={12} className="shrink-0" />
381
+ <span className="min-w-0 flex-1 break-words">
382
+ Pull requests need the GitHub plugin: every other action works without it.
383
+ </span>
384
+ <Button size="sm" variant="outline" disabled={installing} onClick={() => void installGitHub()}>
385
+ {installing ? 'Installing…' : 'Install GitHub'}
386
+ </Button>
387
+ </p>
388
+ )}
389
+ {view.jobs
390
+ ?.filter((job) => job.running || job.error)
391
+ .map((job) => (
392
+ <p
393
+ key={job.name}
394
+ role={job.error ? 'alert' : undefined}
395
+ className={cn(
396
+ 'm-0 flex items-start gap-1.5 rounded-md border px-3 py-2 text-xs',
397
+ job.error ? 'border-destructive/30 text-destructive' : 'border-border text-secondary',
398
+ )}
399
+ >
400
+ <Icon
401
+ name={job.error ? 'triangle-alert' : 'loader-circle'}
402
+ size={12}
403
+ className={cn('mt-0.5 shrink-0', !job.error && 'animate-spin motion-reduce:animate-none')}
404
+ />
405
+ <span className="min-w-0 break-words">
406
+ <span className="font-mono">{job.name}</span>:{' '}
407
+ {job.error ? `${JOB_WORDS.new![1]}: ${job.error}` : JOB_WORDS.new![0]}
408
+ </span>
409
+ </p>
410
+ ))}
411
+ {view.hostLevel?.refusal && (
412
+ <p className="m-0 flex items-start gap-1.5 rounded-md border border-border px-3 py-2 text-xs text-secondary">
413
+ <Icon name="info" size={12} className="mt-0.5 shrink-0" />
414
+ <span className="min-w-0 break-words">{view.hostLevel.refusal}</span>
415
+ </p>
416
+ )}
417
+ {view.hostLevel?.error && (
418
+ <p
419
+ role="alert"
420
+ className="m-0 flex items-start gap-1.5 rounded-md border border-destructive/30 px-3 py-2 text-xs text-destructive"
421
+ >
422
+ <Icon name="triangle-alert" size={12} className="mt-0.5 shrink-0" />
423
+ <span className="min-w-0 whitespace-pre-wrap break-words">
424
+ The active branch did not build, Bridge keeps its last build: {view.hostLevel.error}
425
+ </span>
426
+ </p>
427
+ )}
428
+ <Table columns={COLUMNS} aria-label="Branches" className="text-[13px]">
429
+ <TableHeader>
430
+ <TableRow size="sm">
431
+ <TableHead className="px-2">Branch</TableHead>
432
+ <TableHead
433
+ className="justify-center whitespace-nowrap px-2"
434
+ title={`Commits behind and ahead of ${base}`}
435
+ >
436
+ Behind | Ahead
437
+ </TableHead>
438
+ <TableHead className="px-2" title="Uncommitted changes, staged or not, against HEAD">
439
+ Changes
440
+ </TableHead>
441
+ <TableHead className="px-2">PR</TableHead>
442
+ <TableHead className="px-2" />
443
+ </TableRow>
444
+ </TableHeader>
445
+ <TableBody>
446
+ {view.branches.map((branch) => (
447
+ <TableRow
448
+ key={branch.folder}
449
+ data-branch={branch.name}
450
+ aria-selected={branch.name === selected}
451
+ onClick={() => !branch.main && setSelected(branch.name)}
452
+ className={cn(
453
+ !branch.main && 'cursor-pointer',
454
+ branch.name === selected && !branch.main && 'bg-accent-subtle',
455
+ )}
456
+ >
457
+ <TableCell className="min-w-0 gap-2 px-2">
458
+ <Icon name="git-branch" size={13} className="shrink-0 text-muted" />
459
+ <span className="truncate font-mono" title={branch.folder}>
460
+ {branch.name}
461
+ </span>
462
+ {branch.active && (
463
+ <Badge className="h-4 shrink-0 bg-accent-subtle px-1.5 text-[11px] text-accent">
464
+ Active
465
+ </Badge>
466
+ )}
467
+ {branch.job && (branch.job.running || branch.job.error) && (
468
+ <Badge
469
+ title={branch.job.error}
470
+ className={cn(
471
+ 'h-4 shrink-0 gap-1 px-1.5 text-[11px]',
472
+ branch.job.error ? 'bg-destructive/10 text-destructive' : 'bg-muted text-secondary',
473
+ )}
474
+ >
475
+ {branch.job.running && (
476
+ <Icon name="loader-circle" size={10} className="animate-spin motion-reduce:animate-none" />
477
+ )}
478
+ {JOB_WORDS[branch.job.action]?.[branch.job.running ? 0 : 1] ?? branch.job.action}
479
+ </Badge>
480
+ )}
481
+ </TableCell>
482
+ <TableCell className="px-2">
483
+ <VsDefault branch={branch} base={base} scale={scale} />
484
+ </TableCell>
485
+ <TableCell className="px-2">
486
+ <DiffStat branch={branch} />
487
+ </TableCell>
488
+ <TableCell className="px-2">
489
+ {branch.pr && <PrPill url={branch.pr} branch={branch} />}
490
+ </TableCell>
491
+ <TableCell
492
+ className="justify-end gap-1.5 px-2"
493
+ // Its menus and dialogs are portals: their clicks reach the row through React, never selecting it.
494
+ onClick={(event) => event.stopPropagation()}
495
+ >
496
+ <GitActions
497
+ slug={slug}
498
+ node={node}
499
+ branch={branch}
500
+ route={projectBranchesRoute}
501
+ disabled={busy || !!branch.job?.running}
502
+ onAnswer={(answer) =>
503
+ setView((was) => (was ? { ...was, branches: answer.branches } : was))
504
+ }
505
+ />
506
+ <Button
507
+ size="sm"
508
+ variant="outline"
509
+ disabled={busy || !!view.hostLevel?.refusal || (branch.active && !mixed)}
510
+ title={
511
+ view.hostLevel?.refusal ??
512
+ (branch.active && !mixed
513
+ ? 'Bridge loads this project’s code from this branch'
514
+ : 'Bridge loads this project’s code from this branch now')
515
+ }
516
+ onClick={() => void run('activate', { name: branch.name }, branch.name)}
517
+ >
518
+ <Icon
519
+ name={spinning('activate', branch.name) ? 'loader-circle' : 'play'}
520
+ size={12}
521
+ className={cn(
522
+ spinning('activate', branch.name) &&
523
+ 'animate-spin motion-reduce:animate-none',
524
+ )}
525
+ />
526
+ Activate
527
+ </Button>
528
+ <DropdownMenu>
529
+ <DropdownMenuTrigger
530
+ render={
531
+ <Button
532
+ size="icon-sm"
533
+ aria-label={`More actions on ${branch.name}`}
534
+ disabled={busy}
535
+ />
536
+ }
537
+ >
538
+ <Icon
539
+ name={spinning('remove', branch.name) ? 'loader-circle' : 'ellipsis'}
540
+ size={14}
541
+ className={cn(
542
+ spinning('remove', branch.name) &&
543
+ 'animate-spin motion-reduce:animate-none',
544
+ )}
545
+ />
546
+ </DropdownMenuTrigger>
547
+ <DropdownMenuContent align="end">
548
+ <DropdownMenuItem
549
+ onSelect={() =>
550
+ void copyText(branch.folder).then(
551
+ () => toast({ title: 'Folder path copied' }),
552
+ (cause: unknown) =>
553
+ toast({
554
+ variant: 'error',
555
+ title: 'Not copied',
556
+ description: String(cause),
557
+ }),
558
+ )
559
+ }
560
+ >
561
+ <Icon name="copy" size={14} />
562
+ Copy folder path
563
+ </DropdownMenuItem>
564
+ <DropdownMenuItem
565
+ variant="destructive"
566
+ disabled={keeps(branch) !== undefined}
567
+ title={keeps(branch) ?? 'Delete its worktree (the branch stays in git)'}
568
+ onSelect={() => void run('remove', { name: branch.name }, branch.name)}
569
+ >
570
+ <Icon name="trash" size={14} />
571
+ Delete
572
+ </DropdownMenuItem>
573
+ </DropdownMenuContent>
574
+ </DropdownMenu>
575
+ </TableCell>
576
+ </TableRow>
577
+ ))}
578
+ </TableBody>
579
+ </Table>
580
+ {view.branches.length === 1 && (
581
+ <EmptyState icon={<Icon name="git-branch" size={20} />} title="No branch yet">
582
+ <p>
583
+ A branch works in its own folder: its sessions change it without touching main, and it
584
+ ends with a pull request.
585
+ </p>
586
+ </EmptyState>
587
+ )}
588
+ </div>
589
+ {current && (
590
+ <div className="border-t border-border px-6 py-3">
591
+ <p className="mb-2 text-xs text-secondary">
592
+ A new session on <span className="font-mono">{current.name}</span>
593
+ </p>
594
+ <SessionPrompt
595
+ ctx={ctx}
596
+ at={{ slug, node }}
597
+ create={{ branch: current.name, draft: `branches.${current.name}` }}
598
+ onCreated={(id: string) =>
599
+ ctx.shell.setScreen({ plugin: 'sessions', id: 'sessions' }, node, id)
600
+ }
601
+ />
602
+ </div>
603
+ )}
604
+ <Dialog open={naming} onOpenChange={(open) => !busy && setNaming(open)}>
605
+ <DialogContent>
606
+ <form
607
+ onSubmit={(event) => {
608
+ event.preventDefault();
609
+ if (title.trim()) void run('new', { title: title.trim() });
610
+ }}
611
+ >
612
+ <DialogHeader>
613
+ <DialogTitle>New branch</DialogTitle>
614
+ </DialogHeader>
615
+ <DialogBody className="pb-3">
616
+ <DialogDescription>
617
+ Made from the default branch, in its own folder, its dependencies installed.
618
+ </DialogDescription>
619
+ <Input
620
+ aria-label="New branch"
621
+ placeholder="What is it about? (button-density, table-rows…)"
622
+ value={title}
623
+ disabled={busy}
624
+ autoFocus
625
+ className="mt-2"
626
+ onChange={(event) => setTitle(event.currentTarget.value)}
627
+ />
628
+ </DialogBody>
629
+ <DialogFooter className="justify-end gap-2">
630
+ <Button type="button" disabled={busy} onClick={() => setNaming(false)}>
631
+ Cancel
632
+ </Button>
633
+ <Button type="submit" variant="primary" disabled={busy || !title.trim()}>
634
+ {spinning('new') ? 'Preparing…' : 'Create branch'}
635
+ </Button>
636
+ </DialogFooter>
637
+ </form>
638
+ </DialogContent>
639
+ </Dialog>
640
+ </section>
641
+ );
642
+ }