The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** @jsxRuntime automatic */ |
| 2 | /** @jsxImportSource @alchemy.run/sigil */ |
| 3 | import type { JSX } from "react"; |
| 4 | import { theme } from "../../../Util/Theme.ts"; |
| 5 | import { |
| 6 | Box, |
| 7 | Gutter, |
| 8 | Pointer, |
| 9 | SectionHeading, |
| 10 | Spinner, |
| 11 | Text, |
| 12 | useBorderStyle, |
| 13 | useGlyphs, |
| 14 | } from "../ui/index.ts"; |
| 15 | |
| 16 | export interface ProfileProviderDisplay { |
| 17 | readonly name: string; |
| 18 | readonly method: string; |
| 19 | readonly status: "ready" | "configured" | "reauth" | "reconfigure" | "error"; |
| 20 | readonly lines: ReadonlyArray<string>; |
| 21 | } |
| 22 | |
| 23 | export interface ProfileListDisplay { |
| 24 | readonly name: string; |
| 25 | readonly active: boolean; |
| 26 | readonly providers: ReadonlyArray<{ |
| 27 | readonly name: string; |
| 28 | readonly method: string; |
| 29 | }>; |
| 30 | } |
| 31 | |
| 32 | /** Provider credential status → glyph + color + label, shared with the dashboard. */ |
| 33 | export const providerStatusStyle = { |
| 34 | ready: { |
| 35 | color: theme.color.success, |
| 36 | glyph: "success", |
| 37 | label: "ready", |
| 38 | }, |
| 39 | configured: { |
| 40 | color: theme.color.warning, |
| 41 | glyph: "warning", |
| 42 | label: "configured", |
| 43 | }, |
| 44 | reauth: { |
| 45 | color: theme.color.warning, |
| 46 | glyph: "refresh", |
| 47 | label: "needs re-login", |
| 48 | }, |
| 49 | reconfigure: { |
| 50 | color: theme.color.warning, |
| 51 | glyph: "edit", |
| 52 | label: "needs setup", |
| 53 | }, |
| 54 | error: { |
| 55 | color: theme.color.danger, |
| 56 | glyph: "error", |
| 57 | label: "error", |
| 58 | }, |
| 59 | } as const; |
| 60 | |
| 61 | /** |
| 62 | * Styling for the account-edit flow's row states, shared between the |
| 63 | * `profile edit` cycle prompt and the dashboard's edit screen. `keep` is |
| 64 | * the neutral state for connected providers, `skip` for unconnected ones. |
| 65 | */ |
| 66 | export const editStateStyle = { |
| 67 | keep: { |
| 68 | icon: "selected", |
| 69 | variant: "success", |
| 70 | label: undefined, |
| 71 | }, |
| 72 | skip: { |
| 73 | icon: "unselected", |
| 74 | variant: "neutral", |
| 75 | label: undefined, |
| 76 | }, |
| 77 | add: { |
| 78 | icon: "add", |
| 79 | variant: "success", |
| 80 | label: "add", |
| 81 | }, |
| 82 | reconfigure: { |
| 83 | icon: "edit", |
| 84 | variant: "warning", |
| 85 | label: "reconfigure", |
| 86 | }, |
| 87 | remove: { |
| 88 | icon: "error", |
| 89 | variant: "error", |
| 90 | label: "remove", |
| 91 | }, |
| 92 | } as const; |
| 93 | |
| 94 | export type EditState = keyof typeof editStateStyle; |
| 95 | |
| 96 | const columnWidth = (cells: ReadonlyArray<string>): number => |
| 97 | Math.max(0, ...cells.map((cell) => cell.length)) + 2; |
| 98 | |
| 99 | /** `cloudflare (oauth) · aws (sso)` with dim methods and separators. */ |
| 100 | type ProviderSummaryProps = { |
| 101 | readonly providers: ReadonlyArray<{ name: string; method: string }>; |
| 102 | }; |
| 103 | |
| 104 | function ProviderSummary({ providers }: ProviderSummaryProps): JSX.Element { |
| 105 | return providers.length === 0 ? ( |
| 106 | <Text tone="muted">no providers</Text> |
| 107 | ) : ( |
| 108 | <Text> |
| 109 | {providers.map((provider, i) => ( |
| 110 | <Text key={provider.name}> |
| 111 | {i === 0 ? null : <Text tone="muted"> · </Text>} |
| 112 | {provider.name} |
| 113 | <Text tone="muted"> ({provider.method})</Text> |
| 114 | </Text> |
| 115 | ))} |
| 116 | </Text> |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | function ProfileList({ |
| 121 | profiles, |
| 122 | }: { |
| 123 | readonly profiles: ReadonlyArray<ProfileListDisplay>; |
| 124 | }): JSX.Element { |
| 125 | const glyphs = useGlyphs(); |
| 126 | const borderStyle = useBorderStyle(); |
| 127 | const nameWidth = columnWidth(profiles.map((profile) => profile.name)); |
| 128 | return ( |
| 129 | <Box flexDirection="column"> |
| 130 | <Box |
| 131 | marginBottom={1} |
| 132 | borderStyle={borderStyle} |
| 133 | borderBottom |
| 134 | borderTop={false} |
| 135 | borderLeft={false} |
| 136 | borderRight={false} |
| 137 | borderColor={theme.color.muted} |
| 138 | borderDimColor |
| 139 | > |
| 140 | <SectionHeading annotation={`${profiles.length}`}>Profiles</SectionHeading> |
| 141 | </Box> |
| 142 | {profiles.length === 0 ? ( |
| 143 | <Gutter> |
| 144 | <Text tone="muted">{"No profiles configured. Run `alchemy profile` to create one."}</Text> |
| 145 | </Gutter> |
| 146 | ) : ( |
| 147 | profiles.map((profile) => ( |
| 148 | <Gutter key={profile.name}> |
| 149 | <Box flexDirection="row"> |
| 150 | <Text tone="brand">{profile.active ? glyphs.selected : " "}</Text> |
| 151 | <Text> </Text> |
| 152 | <Box width={nameWidth} flexShrink={0}> |
| 153 | <Text bold={profile.active}>{profile.name}</Text> |
| 154 | </Box> |
| 155 | <ProviderSummary providers={profile.providers} /> |
| 156 | </Box> |
| 157 | </Gutter> |
| 158 | )) |
| 159 | )} |
| 160 | </Box> |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | /** Options every provider block shares; the dashboard threads them through. */ |
| 165 | export interface ProviderBlockOptions { |
| 166 | /** Muted hint appended to rows with `status: "reauth"`. */ |
| 167 | readonly reauthHint?: string; |
| 168 | /** Provider whose detail rows are temporarily replaced by refresh status. */ |
| 169 | readonly refreshingProvider?: string; |
| 170 | /** Provider currently focused by an interactive parent view. */ |
| 171 | readonly focusedProvider?: string; |
| 172 | /** |
| 173 | * Reserve the list-cursor column (see `Pointer`) so an interactive parent |
| 174 | * can mark the focused provider without shifting the table. |
| 175 | */ |
| 176 | readonly focusColumn?: boolean; |
| 177 | } |
| 178 | |
| 179 | /** Column widths computed over every provider so windowed blocks stay aligned. */ |
| 180 | export const providerColumnWidths = ( |
| 181 | providers: ReadonlyArray<ProfileProviderDisplay>, |
| 182 | ): { readonly nameWidth: number; readonly methodWidth: number } => ({ |
| 183 | nameWidth: columnWidth(providers.map((provider) => provider.name)), |
| 184 | methodWidth: columnWidth(providers.map((provider) => provider.method)), |
| 185 | }); |
| 186 | |
| 187 | /** |
| 188 | * Rows a `ProviderBlock` occupies: one blank spacer row above every block |
| 189 | * but the first, the header row, then the detail rows (at least one, so the |
| 190 | * refresh spinner fits inside the same reserved height). The dashboard |
| 191 | * windows providers by this number, so keep it in step with the layout. |
| 192 | */ |
| 193 | export const providerBlockHeight = (provider: ProfileProviderDisplay, first: boolean): number => |
| 194 | (first ? 0 : 1) + 1 + Math.max(provider.lines.length, 1); |
| 195 | |
| 196 | /** |
| 197 | * Detail lines arrive as `key: value` strings (plus free-form diagnostic |
| 198 | * messages). Split the former so keys render as a muted, aligned column and |
| 199 | * values start together; anything that does not fit the shape is shown as-is. |
| 200 | */ |
| 201 | const splitDetail = ( |
| 202 | line: string, |
| 203 | ): { readonly key: string; readonly value: string } | undefined => { |
| 204 | const match = /^([A-Za-z0-9_.-]+): (.*)$/.exec(line); |
| 205 | return match === null ? undefined : { key: match[1]!, value: match[2]! }; |
| 206 | }; |
| 207 | |
| 208 | /** |
| 209 | * One provider in the table: `[cursor] name method status` on the header |
| 210 | * row, its details indented beneath. Blocks are separated by a blank row |
| 211 | * rather than a rule, and the focused block is marked by the cursor glyph and |
| 212 | * a brand-coloured name — never by a rail. |
| 213 | */ |
| 214 | export function ProviderBlock({ |
| 215 | provider, |
| 216 | first, |
| 217 | nameWidth, |
| 218 | methodWidth, |
| 219 | reauthHint, |
| 220 | refreshingProvider, |
| 221 | focusedProvider, |
| 222 | focusColumn = false, |
| 223 | }: ProviderBlockOptions & { |
| 224 | readonly provider: ProfileProviderDisplay; |
| 225 | readonly first: boolean; |
| 226 | readonly nameWidth: number; |
| 227 | readonly methodWidth: number; |
| 228 | }): JSX.Element { |
| 229 | const glyphs = useGlyphs(); |
| 230 | const status = providerStatusStyle[provider.status]; |
| 231 | const focused = focusColumn && provider.name === focusedProvider; |
| 232 | const details = provider.lines.map((line) => ({ |
| 233 | line, |
| 234 | parts: splitDetail(line), |
| 235 | })); |
| 236 | const keyWidth = columnWidth( |
| 237 | details.flatMap(({ parts }) => (parts === undefined ? [] : [parts.key])), |
| 238 | ); |
| 239 | // Details sit under the name, past the cursor column when there is one. |
| 240 | const detailIndent = (focusColumn ? 2 : 0) + theme.space.indent; |
| 241 | return ( |
| 242 | <Box flexDirection="column" paddingTop={first ? 0 : 1}> |
| 243 | <Gutter> |
| 244 | <Box flexDirection="row"> |
| 245 | {focusColumn ? ( |
| 246 | <> |
| 247 | <Pointer focused={focused} /> |
| 248 | <Text> </Text> |
| 249 | </> |
| 250 | ) : null} |
| 251 | <Box width={nameWidth} flexShrink={0}> |
| 252 | <Text bold color={focused ? theme.paint.focus : undefined}> |
| 253 | {provider.name} |
| 254 | </Text> |
| 255 | </Box> |
| 256 | <Box width={methodWidth} flexShrink={0}> |
| 257 | <Text tone="muted">{provider.method}</Text> |
| 258 | </Box> |
| 259 | <Text color={status.color}> |
| 260 | {glyphs[status.glyph]} {status.label} |
| 261 | </Text> |
| 262 | {reauthHint !== undefined && |
| 263 | provider.status === "reauth" && |
| 264 | (focusedProvider === undefined || provider.name === focusedProvider) ? ( |
| 265 | <Text tone="muted"> — {reauthHint}</Text> |
| 266 | ) : null} |
| 267 | </Box> |
| 268 | </Gutter> |
| 269 | <Box flexDirection="column" minHeight={Math.max(provider.lines.length, 1)}> |
| 270 | {provider.name === refreshingProvider ? ( |
| 271 | <Gutter> |
| 272 | <Box paddingLeft={detailIndent}> |
| 273 | <Spinner |
| 274 | label={`refreshing ${provider.method.toLowerCase() === "oauth" ? "OAuth" : provider.method} credentials…`} |
| 275 | /> |
| 276 | </Box> |
| 277 | </Gutter> |
| 278 | ) : ( |
| 279 | details.map(({ line, parts }, lineIndex) => ( |
| 280 | <Gutter key={`${provider.name}-${lineIndex}`}> |
| 281 | <Box paddingLeft={detailIndent}> |
| 282 | {parts === undefined ? ( |
| 283 | <Text>{line}</Text> |
| 284 | ) : ( |
| 285 | <> |
| 286 | <Box width={keyWidth} flexShrink={0}> |
| 287 | <Text tone="muted">{parts.key}</Text> |
| 288 | </Box> |
| 289 | <Text>{parts.value}</Text> |
| 290 | </> |
| 291 | )} |
| 292 | </Box> |
| 293 | </Gutter> |
| 294 | )) |
| 295 | )} |
| 296 | </Box> |
| 297 | </Box> |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Provider table body shared by `profile show` and the dashboard's detail |
| 303 | * pane, so the two render identically. The dashboard passes `reauthHint` to |
| 304 | * advertise its `r` keybinding on rows that need a re-login. |
| 305 | */ |
| 306 | export function ProfileDetailsBody({ |
| 307 | providers, |
| 308 | ...options |
| 309 | }: ProviderBlockOptions & { |
| 310 | readonly providers: ReadonlyArray<ProfileProviderDisplay>; |
| 311 | }): JSX.Element { |
| 312 | const { nameWidth, methodWidth } = providerColumnWidths(providers); |
| 313 | return ( |
| 314 | <Box flexDirection="column"> |
| 315 | {providers.length === 0 ? ( |
| 316 | <Gutter> |
| 317 | <Text tone="muted">No providers configured.</Text> |
| 318 | </Gutter> |
| 319 | ) : ( |
| 320 | providers.map((provider, index) => ( |
| 321 | <ProviderBlock |
| 322 | key={provider.name} |
| 323 | provider={provider} |
| 324 | first={index === 0} |
| 325 | nameWidth={nameWidth} |
| 326 | methodWidth={methodWidth} |
| 327 | {...options} |
| 328 | /> |
| 329 | )) |
| 330 | )} |
| 331 | </Box> |
| 332 | ); |
| 333 | } |
| 334 | |
| 335 | function ProfileDetails({ |
| 336 | profile, |
| 337 | providers, |
| 338 | active, |
| 339 | }: { |
| 340 | readonly profile: string; |
| 341 | readonly providers: ReadonlyArray<ProfileProviderDisplay>; |
| 342 | readonly active: boolean; |
| 343 | }): JSX.Element { |
| 344 | const borderStyle = useBorderStyle(); |
| 345 | return ( |
| 346 | <Box flexDirection="column"> |
| 347 | <Box |
| 348 | marginBottom={1} |
| 349 | borderStyle={borderStyle} |
| 350 | borderBottom |
| 351 | borderTop={false} |
| 352 | borderLeft={false} |
| 353 | borderRight={false} |
| 354 | borderColor={theme.color.muted} |
| 355 | borderDimColor |
| 356 | > |
| 357 | <SectionHeading annotation={active ? "active" : undefined}> |
| 358 | Profile {profile} |
| 359 | </SectionHeading> |
| 360 | </Box> |
| 361 | <ProfileDetailsBody providers={providers} /> |
| 362 | </Box> |
| 363 | ); |
| 364 | } |
| 365 | |
| 366 | function ProfileNotice({ |
| 367 | profile, |
| 368 | message, |
| 369 | }: { |
| 370 | readonly profile: string; |
| 371 | readonly message: string; |
| 372 | }): JSX.Element { |
| 373 | const glyphs = useGlyphs(); |
| 374 | const borderStyle = useBorderStyle(); |
| 375 | return ( |
| 376 | <Box flexDirection="column"> |
| 377 | <Box |
| 378 | marginBottom={1} |
| 379 | borderStyle={borderStyle} |
| 380 | borderBottom |
| 381 | borderTop={false} |
| 382 | borderLeft={false} |
| 383 | borderRight={false} |
| 384 | borderColor={theme.color.muted} |
| 385 | borderDimColor |
| 386 | > |
| 387 | <SectionHeading>Profile {profile}</SectionHeading> |
| 388 | </Box> |
| 389 | <Gutter> |
| 390 | <Text color={theme.color.warning}> |
| 391 | {glyphs.warning} {message} |
| 392 | </Text> |
| 393 | </Gutter> |
| 394 | </Box> |
| 395 | ); |
| 396 | } |
| 397 | |
| 398 | function CurrentProfile({ |
| 399 | name, |
| 400 | source, |
| 401 | }: { |
| 402 | readonly name: string; |
| 403 | readonly source: string; |
| 404 | }): JSX.Element { |
| 405 | const glyphs = useGlyphs(); |
| 406 | return ( |
| 407 | <Text> |
| 408 | <Text tone="brand">{glyphs.selected}</Text> <Text bold>{name}</Text>{" "} |
| 409 | <Text tone="muted">({source})</Text> |
| 410 | </Text> |
| 411 | ); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * View builders consumed by `Interaction.output.print` and the interactive profile app. |
| 416 | */ |
| 417 | export const profileListNode = (profiles: ReadonlyArray<ProfileListDisplay>): JSX.Element => ( |
| 418 | <ProfileList profiles={profiles} /> |
| 419 | ); |
| 420 | |
| 421 | export const profileDetailsNode = ( |
| 422 | profile: string, |
| 423 | providers: ReadonlyArray<ProfileProviderDisplay>, |
| 424 | active: boolean, |
| 425 | ): JSX.Element => <ProfileDetails profile={profile} providers={providers} active={active} />; |
| 426 | |
| 427 | export const profileNoticeNode = (profile: string, message: string): JSX.Element => ( |
| 428 | <ProfileNotice profile={profile} message={message} /> |
| 429 | ); |
| 430 | |
| 431 | export const currentProfileNode = (name: string, source: string): JSX.Element => ( |
| 432 | <CurrentProfile name={name} source={source} /> |
| 433 | ); |