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 { useAnimation } from "@alchemy.run/sigil"; |
| 4 | import { stringWidth } from "@alchemy.run/sigil/ansi"; |
| 5 | import type { ReactNode } from "react"; |
| 6 | import { spinnerFramesFor, statusColor, theme, type StatusVariant } from "../../../Util/Theme.ts"; |
| 7 | import { useBorderStyle, useCliEnvironment, useGlyphs } from "./Environment.tsx"; |
| 8 | import { Box, tabsWindow } from "./Layout.tsx"; |
| 9 | import { Text } from "./Typography.tsx"; |
| 10 | |
| 11 | export interface StatusProps { |
| 12 | readonly variant?: StatusVariant; |
| 13 | readonly children?: ReactNode; |
| 14 | readonly detail?: ReactNode; |
| 15 | } |
| 16 | |
| 17 | export function Status({ variant = "info", children, detail }: StatusProps) { |
| 18 | const glyphs = useGlyphs(); |
| 19 | return ( |
| 20 | <Box gap={1} flexWrap="wrap"> |
| 21 | <Text color={statusColor(variant)}>{glyphs[variant]}</Text> |
| 22 | <Text color={variant === "error" ? statusColor(variant) : undefined}>{children}</Text> |
| 23 | {detail === undefined ? null : <Text tone="muted">· {detail}</Text>} |
| 24 | </Box> |
| 25 | ); |
| 26 | } |
| 27 | |
| 28 | export type ToastProps = StatusProps; |
| 29 | |
| 30 | /** |
| 31 | * Compact application notice. Severity is carried by the status glyph and |
| 32 | * its colour alone — the same vocabulary as transcript lines — so a notice |
| 33 | * row never needs a rail of its own. |
| 34 | */ |
| 35 | export function Toast({ variant = "info", children, detail }: ToastProps) { |
| 36 | return ( |
| 37 | <Status variant={variant} detail={detail}> |
| 38 | {children} |
| 39 | </Status> |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | export interface AlertProps extends StatusProps { |
| 44 | readonly title?: ReactNode; |
| 45 | } |
| 46 | |
| 47 | /** Glyph + bold title on one row, body indented beneath it. */ |
| 48 | export function Alert({ variant = "info", title, children, detail }: AlertProps) { |
| 49 | const glyphs = useGlyphs(); |
| 50 | return ( |
| 51 | <Box flexDirection="column"> |
| 52 | <Box gap={1} alignItems="center"> |
| 53 | <Text bold color={statusColor(variant)}> |
| 54 | {glyphs[variant]} |
| 55 | </Text> |
| 56 | {title === undefined ? null : <Text bold>{title}</Text>} |
| 57 | {detail === undefined ? null : <Text tone="muted">· {detail}</Text>} |
| 58 | </Box> |
| 59 | <Box paddingLeft={theme.space.indent}> |
| 60 | <Text tone="muted">{children}</Text> |
| 61 | </Box> |
| 62 | </Box> |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | export interface KeyBarProps { |
| 67 | readonly keys: ReadonlyArray<readonly [key: string, label: string]>; |
| 68 | readonly marginTop?: number; |
| 69 | readonly inline?: boolean; |
| 70 | /** Widget rendered before the key hints. */ |
| 71 | readonly before?: ReactNode; |
| 72 | /** Widget rendered after the key hints. */ |
| 73 | readonly after?: ReactNode; |
| 74 | /** Draw border rails between populated widget/key sections. */ |
| 75 | readonly divider?: boolean; |
| 76 | } |
| 77 | |
| 78 | export function KeyBar({ |
| 79 | keys, |
| 80 | marginTop = 1, |
| 81 | inline = false, |
| 82 | before, |
| 83 | after, |
| 84 | divider = false, |
| 85 | }: KeyBarProps) { |
| 86 | const borderStyle = useBorderStyle(); |
| 87 | const sectionBorder = { |
| 88 | borderStyle, |
| 89 | borderLeft: true, |
| 90 | borderRight: false, |
| 91 | borderTop: false, |
| 92 | borderBottom: false, |
| 93 | borderColor: theme.color.muted, |
| 94 | borderDimColor: true, |
| 95 | marginLeft: 1, |
| 96 | paddingLeft: 1, |
| 97 | } as const; |
| 98 | return ( |
| 99 | <Box |
| 100 | width={inline ? undefined : "100%"} |
| 101 | flexWrap="wrap" |
| 102 | marginTop={marginTop} |
| 103 | paddingLeft={before === undefined ? theme.space.indent : 0} |
| 104 | > |
| 105 | {before === undefined ? null : <Box>{before}</Box>} |
| 106 | <Box |
| 107 | flexWrap="wrap" |
| 108 | {...(divider && before !== undefined |
| 109 | ? sectionBorder |
| 110 | : before === undefined |
| 111 | ? {} |
| 112 | : { marginLeft: 1 })} |
| 113 | > |
| 114 | {keys.map(([key, label], index) => ( |
| 115 | <Box key={`${key}:${label}`}> |
| 116 | {index === 0 ? null : <Text tone="muted"> • </Text>} |
| 117 | <Text> |
| 118 | <Text bold color={theme.color.brand}> |
| 119 | {key} |
| 120 | </Text> |
| 121 | <Text tone="muted"> {label}</Text> |
| 122 | </Text> |
| 123 | </Box> |
| 124 | ))} |
| 125 | </Box> |
| 126 | {after === undefined ? null : ( |
| 127 | <Box |
| 128 | {...(divider |
| 129 | ? sectionBorder |
| 130 | : { |
| 131 | marginLeft: 1, |
| 132 | })} |
| 133 | > |
| 134 | {after} |
| 135 | </Box> |
| 136 | )} |
| 137 | </Box> |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | export const useSpinnerFrame = (): string => { |
| 142 | const { unicode } = useCliEnvironment(); |
| 143 | const frames = spinnerFramesFor(unicode); |
| 144 | const { frame } = useAnimation({ interval: 80 }); |
| 145 | return frames[frame % frames.length] ?? "-"; |
| 146 | }; |
| 147 | |
| 148 | /** |
| 149 | * Spinner-as-status-icon: one animated frame, colorable so it can stand in |
| 150 | * for a status glyph in trees and progress rows. |
| 151 | */ |
| 152 | type SpinnerGlyphProps = { readonly color?: string }; |
| 153 | |
| 154 | export function SpinnerGlyph({ color }: SpinnerGlyphProps) { |
| 155 | return <Text color={color ?? theme.color.info}>{useSpinnerFrame()}</Text>; |
| 156 | } |
| 157 | |
| 158 | type SpinnerProps = { |
| 159 | readonly label: ReactNode; |
| 160 | readonly detail?: ReactNode; |
| 161 | }; |
| 162 | |
| 163 | export function Spinner({ label, detail }: SpinnerProps) { |
| 164 | return ( |
| 165 | <Box gap={1}> |
| 166 | <SpinnerGlyph /> |
| 167 | <Text>{label}</Text> |
| 168 | {detail === undefined ? null : <Text tone="muted">{detail}</Text>} |
| 169 | </Box> |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | type TabsProps = { |
| 174 | readonly tabs: ReadonlyArray<{ |
| 175 | readonly id: string; |
| 176 | readonly label: string; |
| 177 | readonly marked?: boolean; |
| 178 | }>; |
| 179 | readonly active: string; |
| 180 | }; |
| 181 | |
| 182 | export function Tabs({ tabs, active }: TabsProps) { |
| 183 | const glyphs = useGlyphs(); |
| 184 | const { columns } = useCliEnvironment(); |
| 185 | const gap = 1; |
| 186 | const activeIndex = Math.max( |
| 187 | 0, |
| 188 | tabs.findIndex((tab) => tab.id === active), |
| 189 | ); |
| 190 | // chip width = paddingX (2) + optional marker glyph + space + label |
| 191 | const widths = tabs.map( |
| 192 | (tab) => 2 + stringWidth(tab.label) + (tab.marked ? stringWidth(glyphs.selected) + 1 : 0), |
| 193 | ); |
| 194 | const totalWidth = |
| 195 | widths.reduce((sum, width) => sum + width, 0) + Math.max(0, tabs.length - 1) * gap; |
| 196 | const contentWidth = Math.max(1, columns - theme.space.indent); |
| 197 | const { start, end } = |
| 198 | totalWidth <= contentWidth |
| 199 | ? { start: 0, end: tabs.length } |
| 200 | : // reserve an arrow cell + gap on each side so the window stays put |
| 201 | // whether or not the edge arrows render |
| 202 | tabsWindow(widths, activeIndex, Math.max(1, contentWidth - 2 * (1 + gap)), gap); |
| 203 | return ( |
| 204 | <Box |
| 205 | width="100%" |
| 206 | gap={gap} |
| 207 | paddingLeft={theme.space.indent} |
| 208 | marginBottom={1} |
| 209 | aria-role="tablist" |
| 210 | > |
| 211 | {start > 0 ? <Text tone="muted">{glyphs.overflowLeft}</Text> : null} |
| 212 | {tabs.slice(start, end).map((tab) => { |
| 213 | const selected = tab.id === active; |
| 214 | return ( |
| 215 | <Box |
| 216 | key={tab.id} |
| 217 | paddingX={1} |
| 218 | backgroundColor={selected ? theme.paint.interactive : undefined} |
| 219 | aria-role="tab" |
| 220 | aria-label={tab.label} |
| 221 | aria-state={{ selected }} |
| 222 | > |
| 223 | <Text |
| 224 | bold={selected} |
| 225 | color={selected ? theme.color.onAccent : undefined} |
| 226 | dimColor={!selected} |
| 227 | > |
| 228 | {tab.marked ? ( |
| 229 | <Text color={selected ? theme.color.onAccent : theme.color.brand}> |
| 230 | {glyphs.selected}{" "} |
| 231 | </Text> |
| 232 | ) : null} |
| 233 | {tab.label} |
| 234 | </Text> |
| 235 | </Box> |
| 236 | ); |
| 237 | })} |
| 238 | {end < tabs.length ? <Text tone="muted">{glyphs.overflowRight}</Text> : null} |
| 239 | </Box> |
| 240 | ); |
| 241 | } |