/** @jsxRuntime automatic */ /** @jsxImportSource @alchemy.run/sigil */ import type { JSX } from "react"; import { theme } from "../../../Util/Theme.ts"; import { Box, Gutter, Pointer, SectionHeading, Spinner, Text, useBorderStyle, useGlyphs, } from "../ui/index.ts"; export interface ProfileProviderDisplay { readonly name: string; readonly method: string; readonly status: "ready" | "configured" | "reauth" | "reconfigure" | "error"; readonly lines: ReadonlyArray; } export interface ProfileListDisplay { readonly name: string; readonly active: boolean; readonly providers: ReadonlyArray<{ readonly name: string; readonly method: string; }>; } /** Provider credential status → glyph + color + label, shared with the dashboard. */ export const providerStatusStyle = { ready: { color: theme.color.success, glyph: "success", label: "ready", }, configured: { color: theme.color.warning, glyph: "warning", label: "configured", }, reauth: { color: theme.color.warning, glyph: "refresh", label: "needs re-login", }, reconfigure: { color: theme.color.warning, glyph: "edit", label: "needs setup", }, error: { color: theme.color.danger, glyph: "error", label: "error", }, } as const; /** * Styling for the account-edit flow's row states, shared between the * `profile edit` cycle prompt and the dashboard's edit screen. `keep` is * the neutral state for connected providers, `skip` for unconnected ones. */ export const editStateStyle = { keep: { icon: "selected", variant: "success", label: undefined, }, skip: { icon: "unselected", variant: "neutral", label: undefined, }, add: { icon: "add", variant: "success", label: "add", }, reconfigure: { icon: "edit", variant: "warning", label: "reconfigure", }, remove: { icon: "error", variant: "error", label: "remove", }, } as const; export type EditState = keyof typeof editStateStyle; const columnWidth = (cells: ReadonlyArray): number => Math.max(0, ...cells.map((cell) => cell.length)) + 2; /** `cloudflare (oauth) · aws (sso)` with dim methods and separators. */ type ProviderSummaryProps = { readonly providers: ReadonlyArray<{ name: string; method: string }>; }; function ProviderSummary({ providers }: ProviderSummaryProps): JSX.Element { return providers.length === 0 ? ( no providers ) : ( {providers.map((provider, i) => ( {i === 0 ? null : · } {provider.name} ({provider.method}) ))} ); } function ProfileList({ profiles, }: { readonly profiles: ReadonlyArray; }): JSX.Element { const glyphs = useGlyphs(); const borderStyle = useBorderStyle(); const nameWidth = columnWidth(profiles.map((profile) => profile.name)); return ( Profiles {profiles.length === 0 ? ( {"No profiles configured. Run `alchemy profile` to create one."} ) : ( profiles.map((profile) => ( {profile.active ? glyphs.selected : " "} {profile.name} )) )} ); } /** Options every provider block shares; the dashboard threads them through. */ export interface ProviderBlockOptions { /** Muted hint appended to rows with `status: "reauth"`. */ readonly reauthHint?: string; /** Provider whose detail rows are temporarily replaced by refresh status. */ readonly refreshingProvider?: string; /** Provider currently focused by an interactive parent view. */ readonly focusedProvider?: string; /** * Reserve the list-cursor column (see `Pointer`) so an interactive parent * can mark the focused provider without shifting the table. */ readonly focusColumn?: boolean; } /** Column widths computed over every provider so windowed blocks stay aligned. */ export const providerColumnWidths = ( providers: ReadonlyArray, ): { readonly nameWidth: number; readonly methodWidth: number } => ({ nameWidth: columnWidth(providers.map((provider) => provider.name)), methodWidth: columnWidth(providers.map((provider) => provider.method)), }); /** * Rows a `ProviderBlock` occupies: one blank spacer row above every block * but the first, the header row, then the detail rows (at least one, so the * refresh spinner fits inside the same reserved height). The dashboard * windows providers by this number, so keep it in step with the layout. */ export const providerBlockHeight = (provider: ProfileProviderDisplay, first: boolean): number => (first ? 0 : 1) + 1 + Math.max(provider.lines.length, 1); /** * Detail lines arrive as `key: value` strings (plus free-form diagnostic * messages). Split the former so keys render as a muted, aligned column and * values start together; anything that does not fit the shape is shown as-is. */ const splitDetail = ( line: string, ): { readonly key: string; readonly value: string } | undefined => { const match = /^([A-Za-z0-9_.-]+): (.*)$/.exec(line); return match === null ? undefined : { key: match[1]!, value: match[2]! }; }; /** * One provider in the table: `[cursor] name method status` on the header * row, its details indented beneath. Blocks are separated by a blank row * rather than a rule, and the focused block is marked by the cursor glyph and * a brand-coloured name — never by a rail. */ export function ProviderBlock({ provider, first, nameWidth, methodWidth, reauthHint, refreshingProvider, focusedProvider, focusColumn = false, }: ProviderBlockOptions & { readonly provider: ProfileProviderDisplay; readonly first: boolean; readonly nameWidth: number; readonly methodWidth: number; }): JSX.Element { const glyphs = useGlyphs(); const status = providerStatusStyle[provider.status]; const focused = focusColumn && provider.name === focusedProvider; const details = provider.lines.map((line) => ({ line, parts: splitDetail(line), })); const keyWidth = columnWidth( details.flatMap(({ parts }) => (parts === undefined ? [] : [parts.key])), ); // Details sit under the name, past the cursor column when there is one. const detailIndent = (focusColumn ? 2 : 0) + theme.space.indent; return ( {focusColumn ? ( <> ) : null} {provider.name} {provider.method} {glyphs[status.glyph]} {status.label} {reauthHint !== undefined && provider.status === "reauth" && (focusedProvider === undefined || provider.name === focusedProvider) ? ( — {reauthHint} ) : null} {provider.name === refreshingProvider ? ( ) : ( details.map(({ line, parts }, lineIndex) => ( {parts === undefined ? ( {line} ) : ( <> {parts.key} {parts.value} )} )) )} ); } /** * Provider table body shared by `profile show` and the dashboard's detail * pane, so the two render identically. The dashboard passes `reauthHint` to * advertise its `r` keybinding on rows that need a re-login. */ export function ProfileDetailsBody({ providers, ...options }: ProviderBlockOptions & { readonly providers: ReadonlyArray; }): JSX.Element { const { nameWidth, methodWidth } = providerColumnWidths(providers); return ( {providers.length === 0 ? ( No providers configured. ) : ( providers.map((provider, index) => ( )) )} ); } function ProfileDetails({ profile, providers, active, }: { readonly profile: string; readonly providers: ReadonlyArray; readonly active: boolean; }): JSX.Element { const borderStyle = useBorderStyle(); return ( Profile {profile} ); } function ProfileNotice({ profile, message, }: { readonly profile: string; readonly message: string; }): JSX.Element { const glyphs = useGlyphs(); const borderStyle = useBorderStyle(); return ( Profile {profile} {glyphs.warning} {message} ); } function CurrentProfile({ name, source, }: { readonly name: string; readonly source: string; }): JSX.Element { const glyphs = useGlyphs(); return ( {glyphs.selected} {name}{" "} ({source}) ); } /** * View builders consumed by `Interaction.output.print` and the interactive profile app. */ export const profileListNode = (profiles: ReadonlyArray): JSX.Element => ( ); export const profileDetailsNode = ( profile: string, providers: ReadonlyArray, active: boolean, ): JSX.Element => ; export const profileNoticeNode = (profile: string, message: string): JSX.Element => ( ); export const currentProfileNode = (name: string, source: string): JSX.Element => ( );