Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

worktree-setup-card.tsx

378 lines12.1 KB

1import {
2 worktreeSetupStageLabel,
3 type WorktreeSetupSnapshot,
4 type WorktreeSetupStage,
5} from "@t3tools/contracts";
6import { worktreeSetupAgentStarted } from "@t3tools/client-runtime/worktree-setup";
7import { formatDuration } from "@t3tools/shared/orchestrationTiming";
8import { useState } from "react";
9import { Platform, Pressable, ScrollView, View } from "react-native";
10import { useSafeAreaInsets } from "react-native-safe-area-context";
11import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
12import { AppText as Text } from "../../components/ui/AppText";
13import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol";
14import { WorktreeSetupSheet } from "./worktree-setup-sheet";
15import { ShimmeringWorkContent } from "./thread-work-log";
16import { useVisibleSecondClock } from "./use-visible-second-clock";
17​
18export interface WorktreeSetupCardProps {
19 snapshot: WorktreeSetupSnapshot;
20 turnStarted: boolean;
21 turnStartedAt: string | null;
22 working: boolean;
23 onCancel: () => void;
24 onWorkLocally: (() => void) | null;
26​
27function elapsed(start: string | null, end: string | null, now: number) {
28 const duration = (end ? Date.parse(end) : now) - (start ? Date.parse(start) : NaN);
29 return Number.isFinite(duration) ? formatDuration(Math.max(0, duration)) : null;
31​
32const icons: Record<WorktreeSetupStage["status"], AppSymbolName> = {
33 pending: "circle",
34 running: "clock",
35 done: "checkmark",
36 skipped: "minus",
37 failed: "xmark",
38 warning: "exclamationmark.triangle",
39};
40​
41export function WorktreeSetupCard(props: WorktreeSetupCardProps) {
42 const { snapshot, turnStarted, turnStartedAt, working } = props;
43 const handedOff = turnStarted && worktreeSetupAgentStarted(snapshot);
44 const running = snapshot.phase === "running";
45 const backgroundSetup = handedOff && running;
46 const scriptName = snapshot.setupScript?.name ?? "Setup script";
47 const [detailsOpen, setDetailsOpen] = useState(false);
48 const now = useVisibleSecondClock(running || working);
49 const failed =
50 snapshot.phase === "failed" || snapshot.stages.some((stage) => stage.status === "failed");
51 const label =
52 handedOff && working
53 ? `Working for ${elapsed(turnStartedAt, null, now) ?? "0s"}`
54 : running
55 ? handedOff
56 ? "Setup continues…"
57 : "Setting up worktree…"
58 : snapshot.phase === "cancelled"
59 ? "Worktree setup cancelled"
60 : snapshot.phase === "failed"
61 ? "Worktree setup failed"
62 : failed
63 ? "Setup script failed"
64 : "Worktree ready";
65​
66 return (
67 <View accessibilityLabel="Worktree setup" className="py-1">
68 <View className="min-h-11 flex-row items-center gap-2 border-b border-border px-1">
69 <HeaderLabel
70 label={label}
71 active={(running || working) && !detailsOpen}
72 failed={failed && !working}
73 />
74 {!handedOff ? (
75 <Text
76 className="text-2xs text-foreground-secondary"
77 style={{ fontVariant: ["tabular-nums"] }}
78 >
79 {elapsed(snapshot.startedAt, snapshot.endedAt, now)}
80 </Text>
81 ) : null}
82 <Pressable
83 accessibilityRole="button"
84 accessibilityLabel={
85 backgroundSetup
86 ? `${scriptName} is still running. Show setup progress.`
87 : "Worktree setup details"
88 }
89 accessibilityState={{ expanded: detailsOpen }}
90 onPress={() => setDetailsOpen(true)}
91 className="min-h-11 max-w-[55%] justify-center px-2"
92 >
93 <View
94 className={
95 backgroundSetup
96 ? "min-w-0 flex-row items-center gap-1 rounded-full border border-border px-2 py-1"
97 : "flex-row items-center gap-1"
98 }
99 >
100 {backgroundSetup ? (
101 <AppActivityIndicator
102 size={Platform.OS === "android" ? 12 : "small"}
103 colorClassName="accent-icon-muted"
104 style={{
105 width: 12,
106 height: 12,
107 transform: Platform.OS === "android" ? undefined : [{ scale: 0.65 }],
108 }}
109 />
110 ) : failed ? (
111 <SymbolView
112 name="exclamationmark.circle"
113 size={12}
114 tintColorClassName="accent-danger-foreground"
115 />
116 ) : null}
117 <Text numberOfLines={1} className="shrink text-2xs text-foreground-secondary">
118 {backgroundSetup ? scriptName : "Details"}
119 </Text>
120 {!backgroundSetup ? (
121 <SymbolView name="chevron.right" size={10} tintColorClassName="accent-icon-muted" />
122 ) : null}
123 </View>
124 </Pressable>
125 </View>
126 {!handedOff ? (
127 <View className="pt-1 pb-2">
128 {snapshot.stages
129 .filter((stage) => stage.id !== "agent")
130 .map((stage) => (
131 <StageRow
132 key={stage.id}
133 stage={stage}
134 scriptName={snapshot.setupScript?.name ?? null}
135 now={now}
136 compact
137 animate={!detailsOpen}
138 />
139 ))}
140 </View>
141 ) : null}
142 {detailsOpen ? (
143 <SetupDetailsSheet {...props} now={now} onClose={() => setDetailsOpen(false)} />
144 ) : null}
145 </View>
146 );
148​
149export function WorktreeWorkingHeader({ startedAt }: { startedAt: string }) {
150 const now = useVisibleSecondClock(true);
151 return (
152 <View className="py-1">
153 <View className="min-h-11 flex-row items-center border-b border-border px-1">
154 <HeaderLabel label={`Working for ${elapsed(startedAt, null, now) ?? "0s"}`} active />
155 </View>
156 </View>
157 );
159​
160function HeaderLabel({
161 label,
162 active,
163 failed = false,
164}: {
165 label: string;
166 active: boolean;
167 failed?: boolean;
168}) {
169 return active ? (
170 <ShimmeringWorkContent
171 icon="clock"
172 iconSubtleColor="transparent"
173 label={label}
174 showIcon={false}
175 />
176 ) : (
177 <Text
178 numberOfLines={1}
179 className={
180 failed
181 ? "flex-1 text-sm text-danger-foreground"
182 : "flex-1 text-sm text-foreground-secondary"
183 }
184 style={{ fontVariant: ["tabular-nums"] }}
185 >
186 {label}
187 </Text>
188 );
190​
191function SetupDetailsSheet({
192 snapshot,
193 turnStarted,
194 onCancel,
195 onWorkLocally,
196 onClose,
197 now,
198}: WorktreeSetupCardProps & { onClose: () => void; now: number }) {
199 const insets = useSafeAreaInsets();
200 const [bodyHeight, setBodyHeight] = useState(0);
201 const canCancel = snapshot.phase === "running" && !turnStarted;
202 return (
203 <WorktreeSetupSheet height={bodyHeight} onClose={onClose}>
204 <ScrollView
205 contentInsetAdjustmentBehavior="automatic"
206 bounces={false}
207 onContentSizeChange={(_width, height) => setBodyHeight(height)}
208 contentContainerStyle={{
209 paddingHorizontal: 20,
210 paddingTop: 12,
211 paddingBottom: Math.max(20, insets.bottom),
212 }}
213 >
214 {snapshot.stages
215 .filter((stage) => stage.id !== "agent")
216 .map((stage) => (
217 <View key={stage.id}>
218 <StageRow stage={stage} scriptName={snapshot.setupScript?.name ?? null} now={now} />
219 {stage.id === "setup-script" &&
220 (stage.status === "running" || stage.status === "failed" || stage.tail.length > 0) ? (
221 <OutputTail lines={stage.tail} failed={stage.status === "failed"} />
222 ) : null}
223 </View>
224 ))}
225 {snapshot.phase === "failed" && snapshot.error ? (
226 <Text selectable className="ml-8 mt-2 text-xs text-danger-foreground">
227 {snapshot.error}
228 </Text>
229 ) : null}
230 {canCancel ? (
231 <View className="mt-3 flex-row items-center justify-end gap-4 border-t border-border pt-1">
232 <Pressable
233 accessibilityRole="button"
234 accessibilityLabel="Cancel worktree setup"
235 onPress={() => {
236 onClose();
237 onCancel();
238 }}
239 className="min-h-11 justify-center px-2"
240 >
241 <Text className="text-sm text-danger-foreground">Cancel setup</Text>
242 </Pressable>
243 {onWorkLocally ? (
244 <Pressable
245 accessibilityRole="button"
246 onPress={() => {
247 onClose();
248 onWorkLocally();
249 }}
250 className="min-h-11 justify-center px-2"
251 >
252 <Text className="text-sm text-foreground">Work locally</Text>
253 </Pressable>
254 ) : null}
255 </View>
256 ) : null}
257 </ScrollView>
258 </WorktreeSetupSheet>
259 );
261​
262function StageRow({
263 stage,
264 scriptName,
265 now,
266 compact = false,
267 animate = true,
268}: {
269 stage: WorktreeSetupStage;
270 scriptName: string | null;
271 now: number;
272 compact?: boolean;
273 animate?: boolean;
274}) {
275 const label =
276 stage.id === "setup-script"
277 ? (scriptName ?? worktreeSetupStageLabel(stage.id))
278 : worktreeSetupStageLabel(stage.id);
279 const detail =
280 stage.status === "pending"
281 ? null
282 : stage.status === "skipped"
283 ? (stage.detail ?? "skipped")
284 : stage.id === "checkout" && stage.status === "running" && stage.percent !== null
285 ? `${stage.percent}%`
286 : stage.detail;
287 return (
288 <View
289 accessibilityLabel={`${label}, ${stage.status}`}
290 className={compact ? "min-h-8 flex-row items-center" : "min-h-11 flex-row items-center"}
291 style={{ columnGap: 8, opacity: stage.status === "pending" ? 0.4 : 1 }}
292 >
293 <View className="w-6 items-center">
294 {stage.status === "running" && animate ? (
295 <AppActivityIndicator
296 size="small"
297 colorClassName="accent-icon-muted"
298 style={{ transform: [{ scale: 0.75 }] }}
299 />
300 ) : (
301 <SymbolView
302 name={icons[stage.status]}
303 size={14}
304 tintColorClassName={
305 stage.status === "failed"
306 ? "accent-danger-foreground"
307 : stage.status === "warning"
308 ? "accent-warning-foreground"
309 : "accent-icon-muted"
310 }
311 />
312 )}
313 </View>
314 {stage.status === "running" && animate ? (
315 <ShimmeringWorkContent
316 icon="clock"
317 iconSubtleColor="transparent"
318 label={label}
319 showIcon={false}
320 />
321 ) : (
322 <Text
323 numberOfLines={1}
324 className={
325 stage.status === "failed"
326 ? "flex-1 text-sm text-danger-foreground"
327 : "flex-1 text-sm text-foreground-secondary"
328 }
329 >
330 {label}
331 </Text>
332 )}
333 {detail ? (
334 <Text numberOfLines={1} className="max-w-[25%] shrink text-2xs text-foreground-secondary">
335 {detail}
336 </Text>
337 ) : null}
338 {stage.status !== "pending" && stage.status !== "skipped" ? (
339 <Text
340 className="text-2xs text-foreground-secondary"
341 style={{ fontVariant: ["tabular-nums"] }}
342 >
343 {elapsed(stage.startedAt, stage.endedAt, now)}
344 </Text>
345 ) : null}
346 </View>
347 );
349​
350const OUTPUT_TAIL_SLOTS = [0, 1, 2, 3] as const;
351​
352function OutputTail({ lines, failed }: { lines: ReadonlyArray<string>; failed: boolean }) {
353 return (
354 <View
355 accessibilityLabel="Setup script output"
356 className={
357 failed
358 ? "mb-2 ml-8 rounded-md border border-danger-border bg-danger px-3 py-2"
359 : "mb-2 ml-8 rounded-md border border-border bg-card-alt px-3 py-2"
360 }
361 >
362 {OUTPUT_TAIL_SLOTS.map((slot) => (
363 <Text
364 key={slot}
365 selectable
366 numberOfLines={1}
367 className={
368 failed
369 ? "text-2xs leading-5 text-danger-foreground ios:font-[family-name:Menlo] android:font-mono"
370 : "text-2xs leading-5 text-foreground-secondary ios:font-[family-name:Menlo] android:font-mono"
371 }
372 >
373 {lines[lines.length - OUTPUT_TAIL_SLOTS.length + slot] || "\u00a0"}
374 </Text>
375 ))}
376 </View>
377 );