The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | /** @jsxRuntime automatic */ |
| 2 | /** @jsxImportSource @alchemy.run/sigil */ |
| 3 | import { useSyncExternalStore } from "@alchemy.run/sigil/react"; |
| 4 | import type { ReactNode } from "react"; |
| 5 | import { theme } from "../../../Util/Theme.ts"; |
| 6 | import { useGlyphs } from "./Environment.tsx"; |
| 7 | import { SpinnerGlyph } from "./Feedback.tsx"; |
| 8 | import { Box, Row, Stack } from "./Layout.tsx"; |
| 9 | import { ProgressBar } from "./ProgressBar.tsx"; |
| 10 | import { Text } from "./Typography.tsx"; |
| 11 | |
| 12 | export interface TaskRowProps { |
| 13 | /** Status glyph. Ignored while `spinning`; defaults to the bullet glyph. */ |
| 14 | readonly icon?: string; |
| 15 | /** Color for the glyph (and the spinner while `spinning`). */ |
| 16 | readonly iconColor?: string; |
| 17 | /** Render an animated spinner frame in the glyph slot. */ |
| 18 | readonly spinning?: boolean; |
| 19 | readonly label: ReactNode; |
| 20 | /** Muted annotation directly after the label. */ |
| 21 | readonly detail?: ReactNode; |
| 22 | /** Indentation in 2-space units. */ |
| 23 | readonly depth?: number; |
| 24 | /** Extra trailing cells (status labels, chips). */ |
| 25 | readonly children?: ReactNode; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * The one status row shape shared by TaskTree and the plan/apply views: |
| 30 | * glyph-or-spinner, bold label, muted detail, then any trailing cells. |
| 31 | */ |
| 32 | export function TaskRow({ |
| 33 | icon, |
| 34 | iconColor, |
| 35 | spinning = false, |
| 36 | label, |
| 37 | detail, |
| 38 | depth = 0, |
| 39 | children, |
| 40 | }: TaskRowProps) { |
| 41 | const glyphs = useGlyphs(); |
| 42 | return ( |
| 43 | <Row gap={1} paddingLeft={depth * theme.space.indent}> |
| 44 | {spinning ? ( |
| 45 | <SpinnerGlyph color={iconColor} /> |
| 46 | ) : ( |
| 47 | <Text color={iconColor}>{icon ?? glyphs.bullet}</Text> |
| 48 | )} |
| 49 | <Text bold={depth === 0}>{label}</Text> |
| 50 | {detail === undefined ? null : <Text tone="muted">· {detail}</Text>} |
| 51 | {children} |
| 52 | </Row> |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | export interface ProgressGroupRow { |
| 57 | readonly id: string; |
| 58 | readonly label: ReactNode; |
| 59 | readonly completed: number; |
| 60 | readonly total: number; |
| 61 | readonly failed?: number; |
| 62 | readonly detail?: ReactNode; |
| 63 | } |
| 64 | |
| 65 | type ProgressGroupProps = { |
| 66 | readonly rows: ReadonlyArray<ProgressGroupRow>; |
| 67 | readonly width?: number; |
| 68 | /** Fixed label column (truncated) so the count cells align across rows. */ |
| 69 | readonly labelWidth?: number; |
| 70 | }; |
| 71 | |
| 72 | export function ProgressGroup({ rows, width = 20, labelWidth }: ProgressGroupProps) { |
| 73 | return ( |
| 74 | <Stack> |
| 75 | {rows.map((row) => { |
| 76 | const complete = row.total > 0 && row.completed >= row.total; |
| 77 | const variant = row.failed ? "error" : complete ? "success" : "info"; |
| 78 | return ( |
| 79 | <Row key={row.id}> |
| 80 | <ProgressBar |
| 81 | value={row.total <= 0 ? 0 : row.completed / row.total} |
| 82 | width={width} |
| 83 | showPercent={false} |
| 84 | variant={variant} |
| 85 | /> |
| 86 | {labelWidth === undefined ? ( |
| 87 | <Text>{row.label}</Text> |
| 88 | ) : ( |
| 89 | <Box width={labelWidth} flexShrink={0}> |
| 90 | <Text wrap="truncate-end">{row.label}</Text> |
| 91 | </Box> |
| 92 | )} |
| 93 | <Text bold tone={variant === "error" ? "danger" : variant}> |
| 94 | {row.completed}/{row.total} |
| 95 | </Text> |
| 96 | {row.failed ? <Text tone="danger">({row.failed} failed)</Text> : null} |
| 97 | {row.detail === undefined ? null : <Text tone="muted">{row.detail}</Text>} |
| 98 | </Row> |
| 99 | ); |
| 100 | })} |
| 101 | </Stack> |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | export class LiveStore<Value> { |
| 106 | private value: Value; |
| 107 | private readonly listeners = new Set<() => void>(); |
| 108 | constructor(initial: Value) { |
| 109 | this.value = initial; |
| 110 | } |
| 111 | readonly subscribe = (listener: () => void) => { |
| 112 | this.listeners.add(listener); |
| 113 | return () => this.listeners.delete(listener); |
| 114 | }; |
| 115 | readonly snapshot = () => this.value; |
| 116 | readonly set = (value: Value) => { |
| 117 | this.value = value; |
| 118 | for (const listener of this.listeners) listener(); |
| 119 | }; |
| 120 | readonly update = (f: (value: Value) => Value) => this.set(f(this.value)); |
| 121 | } |
| 122 | |
| 123 | export const useLiveStore = <Value,>(store: LiveStore<Value>): Value => |
| 124 | useSyncExternalStore(store.subscribe, store.snapshot); |