-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindicator.tsx
More file actions
389 lines (360 loc) · 13.5 KB
/
Copy pathindicator.tsx
File metadata and controls
389 lines (360 loc) · 13.5 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
* The worktree chip in the chat input (`chat-input-toolbar`).
*
* One control for everything a task's checkout needs:
*
* - **status** — ahead/behind, uncommitted changes and merge readiness for the
* worktree the current task is in;
* - **create** — a new worktree, which becomes the selection for the next task;
* - **switch** — pick which worktree the next task runs in, or re-point a workflow
* that has not started its agents yet.
*
* The selection is *plugin* state, not webview state: core asks the plugin where a task
* should run (`resolve-task-cwd`) at creation time, so the answer has to live on the
* side that will be asked. Picking nothing means a fresh worktree — parallel tasks
* never collide by default, which is the whole point of the feature.
*
* BUILD: `ui/indicator.js` (esbuild ESM; React and `@shofer/plugin-ui` external).
*/
import { useCallback, useEffect, useMemo, useState } from "react"
import {
Popover,
PopoverContent,
PopoverTrigger,
StandardTooltip,
cn,
usePluginTranslation,
useShoferPortal,
} from "@shofer/plugin-ui"
import type { WorktreeStatus } from "../src/worktrees/types.js"
import {
CreateWorktreeDialog,
ask,
useCreationProgress,
useWorktreeList,
useWorktreesFeature,
worktreeLabel,
type PluginUIApi,
type Worktree,
} from "./shared.js"
export default function WorktreeIndicator({ api }: { api: PluginUIApi }): React.JSX.Element | null {
const t = usePluginTranslation()
const portalContainer = useShoferPortal()
const featureOn = useWorktreesFeature(api)
const { listing, refresh } = useWorktreeList(api)
const { steps, reset } = useCreationProgress(api)
const [open, setOpen] = useState(false)
const [modalOpen, setModalOpen] = useState(false)
const [status, setStatus] = useState<WorktreeStatus | null>(null)
const [loading, setLoading] = useState(false)
const [pendingCwd, setPendingCwd] = useState<string | null | undefined>(undefined)
const [actionError, setActionError] = useState<string | null>(null)
const task = api.context.task
// A task is active once the host has pushed any timeline rows for it.
const hasActiveTask = (task?.messageCount ?? 0) > 0
// The host says whether it would still accept a re-point: true before a task starts,
// and for a workflow that is still collecting its parameters (no agents on disk yet).
const canRepoint = task?.cwdMutable !== false
const locked = hasActiveTask && !canRepoint
const worktrees = useMemo(() => listing?.worktrees ?? [], [listing])
const selectable = worktrees.filter((w) => !w.isBare)
const workspaceCurrent = worktrees.find((w) => w.isCurrent)
// Once a task exists, the chip must name the worktree that task is IN — the workspace
// checkout is always the main one, so `isCurrent` would be a lie there.
const selected = useMemo<Worktree | undefined>(() => {
if (hasActiveTask && task?.cwd) return worktrees.find((w) => w.path === task.cwd) ?? workspaceCurrent
if (pendingCwd) return worktrees.find((w) => w.path === pendingCwd) ?? workspaceCurrent
return workspaceCurrent
}, [hasActiveTask, task?.cwd, pendingCwd, worktrees, workspaceCurrent])
// The pending pick lives in the plugin, so re-read it rather than assume this mount
// made it (a second window, or a re-mount after a tab switch, has the same state).
useEffect(() => {
void ask<{ cwd?: string; optedOut?: boolean }>(api, "selection")
.then((selection) => setPendingCwd(selection.optedOut ? null : selection.cwd))
.catch(() => undefined)
}, [api])
const available =
listing !== null &&
listing.isGitRepo &&
!listing.isMultiRoot &&
!listing.isSubfolder &&
listing.gitRootPath !== ""
const disabledTooltip =
listing === null
? ""
: listing.gitRootPath === "" && !listing.isGitRepo
? t("picker.disabledNoFolder")
: listing.isMultiRoot
? t("picker.disabledMultiRoot")
: !listing.isGitRepo
? t("picker.disabledNotGitRepo")
: listing.isSubfolder
? t("picker.disabledSubfolder")
: ""
const handleOpenChange = useCallback(
(isOpen: boolean) => {
// Defensive: the trigger is disabled in this state, but Radix can still open
// via the keyboard.
if (isOpen && !available) return
setOpen(isOpen)
if (!isOpen) return
refresh()
setLoading(true)
void ask<WorktreeStatus>(api, "status", { cwd: selected?.path })
.then(setStatus)
.catch(() => setStatus(null))
.finally(() => setLoading(false))
},
[api, available, refresh, selected?.path],
)
/** Point the next task — or a not-yet-started workflow — at `cwd`. */
const choose = useCallback(
async (cwd: string | null) => {
setActionError(null)
try {
if (hasActiveTask && canRepoint && cwd) {
await ask(api, "set-task-cwd", { cwd, taskId: task?.taskId }, true)
return
}
await ask(api, "select", { cwd })
setPendingCwd(cwd)
} catch (error) {
setActionError(error instanceof Error ? error.message : String(error))
}
},
[api, hasActiveTask, canRepoint, task?.taskId],
)
const handleSelect = useCallback(
(worktree: Worktree) => {
setOpen(false)
if (locked) return
// The workspace checkout is the opt-out: run on the current branch, no worktree.
void choose(worktree.isCurrent ? null : worktree.path)
},
[locked, choose],
)
/**
* A worktree created from the chat input is a place to work, so open a task in it —
* the same thing the built-in control did. On a workflow that has not started, there
* is already a task to move, so re-point that one instead of opening a second.
*/
const handleCreated = useCallback(
async (createdPath: string, branch: string) => {
reset()
refresh()
if (hasActiveTask) {
await choose(createdPath)
return
}
setActionError(null)
try {
await ask(api, "open-task", { cwd: createdPath, name: `worktree: ${branch}` }, true)
} catch (error) {
setActionError(error instanceof Error ? error.message : String(error))
}
},
[api, reset, refresh, choose, hasActiveTask],
)
// The feature is off (Basics config or governance): a deployment replacing
// worktrees must not end up with two branch chips.
if (!featureOn) return null
return (
<>
<Popover open={open} onOpenChange={handleOpenChange}>
<StandardTooltip content={available ? t("picker.tooltip") : disabledTooltip}>
<PopoverTrigger
disabled={!available}
aria-disabled={!available}
className={cn(
"inline-flex items-center gap-1 relative whitespace-nowrap px-1.5 py-1 text-xs",
"bg-transparent border border-[rgba(255,255,255,0.08)] rounded-md text-vscode-foreground",
"transition-all duration-150 focus:outline-none focus-visible:ring-1 focus-visible:ring-vscode-focusBorder focus-visible:ring-inset",
"max-w-[160px]",
available
? "opacity-90 hover:opacity-100 hover:bg-[rgba(255,255,255,0.03)] hover:border-[rgba(255,255,255,0.15)] cursor-pointer"
: "opacity-40 cursor-not-allowed",
)}>
<span className="codicon codicon-git-branch shrink-0" />
<span className="truncate">{selected ? worktreeLabel(selected, t) : t("view.noBranch")}</span>
<span className="codicon codicon-chevron-down shrink-0 opacity-70" />
</PopoverTrigger>
</StandardTooltip>
<PopoverContent
align="start"
sideOffset={4}
container={portalContainer}
className="p-0 overflow-hidden min-w-72 max-w-80">
<div className="flex flex-col w-full">
<div className="px-3 pt-3 pb-2">
<h4 className="text-sm font-semibold m-0 flex items-center gap-2">
<span className="codicon codicon-git-branch" />
{selected ? worktreeLabel(selected, t) : t("view.noBranch")}
</h4>
</div>
{steps.length > 0 && (
<div className="px-3 pb-2">
{steps.map((s) => {
const failed = s.detail?.startsWith("failed")
return (
<div
key={s.step}
className="flex items-center gap-2 py-0.5 text-xs text-vscode-descriptionForeground">
<span
className={cn(
"codicon shrink-0",
s.completed && !failed
? "codicon-check text-vscode-charts-green"
: failed
? "codicon-error text-vscode-errorForeground"
: "codicon-loading codicon-modifier-spin",
)}
/>
<span className="truncate">{t(`step.${s.step}`)}</span>
{s.completed && (
<span
className={cn(
"ml-auto shrink-0",
failed
? "text-vscode-errorForeground"
: "text-vscode-charts-green",
)}>
{s.detail}
</span>
)}
</div>
)
})}
</div>
)}
{loading ? (
<div className="flex items-center justify-center py-6">
<span className="codicon codicon-loading codicon-modifier-spin text-lg" />
</div>
) : status ? (
<div className="max-h-[260px] overflow-y-auto px-3 pb-3 text-sm">
{status.lastCommit && (
<div className="mb-2">
<span className="text-vscode-descriptionForeground">
{t("status.lastCommit")}:
</span>{" "}
<span className="font-mono text-xs">{status.lastCommit.hash}</span>{" "}
<span className="text-vscode-descriptionForeground">
{status.lastCommit.subject}
</span>
<div className="text-xs text-vscode-descriptionForeground mt-0.5">
{status.lastCommit.relativeTime} — {status.lastCommit.author}
</div>
</div>
)}
{!status.isBaseBranch && (
<div className="flex gap-3 mb-2">
{status.commitsAhead > 0 && (
<span className="text-vscode-charts-green">
▲ {status.commitsAhead} {t("status.ahead")}
</span>
)}
{status.commitsBehind > 0 && (
<span className="text-vscode-charts-yellow">
▼ {status.commitsBehind} {t("status.behind")}
</span>
)}
{status.commitsAhead === 0 && status.commitsBehind === 0 && (
<span className="text-vscode-descriptionForeground">
{t("status.upToDate")}
</span>
)}
</div>
)}
{!status.isBaseBranch && status.filesChanged > 0 && (
<div className="mb-1 text-vscode-descriptionForeground">
{status.filesChanged} {t("status.filesChanged")} ({status.insertions}+ /{" "}
{status.deletions}-)
</div>
)}
{status.hasUncommittedChanges && (
<div className="mb-1 text-vscode-charts-yellow">
⚠ {status.uncommittedCount} {t("status.uncommittedChanges")}
</div>
)}
{!status.isBaseBranch && status.mergeReadiness.hasConflicts !== null && (
<div
className={cn(
"mb-1 flex items-center gap-1",
status.mergeReadiness.hasConflicts
? "text-vscode-errorForeground"
: "text-vscode-charts-green",
)}>
{status.mergeReadiness.hasConflicts
? `⚠ ${t("status.conflictsDetected", { count: status.mergeReadiness.conflictedFiles.length })}`
: `✅ ${t("status.safeToMerge")}`}
</div>
)}
</div>
) : (
<div className="px-3 pb-3 text-sm text-vscode-descriptionForeground">
{t("status.noData")}
</div>
)}
{actionError && (
<div className="px-3 pb-2 text-sm text-vscode-errorForeground">{actionError}</div>
)}
{!locked && (
<>
<div className="border-t border-vscode-dropdown-border" />
<button
type="button"
onClick={() => {
setOpen(false)
setModalOpen(true)
}}
className={cn(
"w-full flex items-center gap-2 px-3 py-2 text-sm text-left",
"bg-transparent border-none cursor-pointer",
"text-vscode-foreground hover:bg-vscode-list-hoverBackground",
"focus:outline-none focus-visible:bg-vscode-list-hoverBackground",
)}>
<span className="codicon codicon-add shrink-0" />
<span>{t("picker.createNew")}</span>
</button>
</>
)}
{!locked && selectable.length > 1 && (
<>
<div className="border-t border-vscode-dropdown-border" />
<div className="px-3 pt-2 pb-1 text-[11px] font-semibold text-vscode-descriptionForeground uppercase tracking-wide">
{t("picker.selectWorktree")}
</div>
<div className="max-h-48 overflow-y-auto pb-1">
{selectable.map((worktree) => (
<button
key={worktree.path}
type="button"
onClick={() => handleSelect(worktree)}
className={cn(
"w-full flex items-center gap-2 px-3 py-1.5 text-sm text-left",
"bg-transparent border-none cursor-pointer",
"text-vscode-foreground hover:bg-vscode-list-hoverBackground",
"focus:outline-none focus-visible:bg-vscode-list-hoverBackground",
)}>
<span className="codicon codicon-git-branch shrink-0 opacity-80" />
<span className="truncate flex-1">{worktree.branch || worktree.path}</span>
{selected?.path === worktree.path && (
<span className="codicon codicon-check shrink-0 opacity-80" />
)}
</button>
))}
</div>
</>
)}
</div>
</PopoverContent>
</Popover>
<CreateWorktreeDialog
api={api}
open={modalOpen}
onClose={() => setModalOpen(false)}
onCreated={(createdPath, branch) => void handleCreated(createdPath, branch)}
/>
</>
)
}