/** @jsxRuntime automatic */
/** @jsxImportSource @alchemy.run/sigil */
import { useAnimation } from "@alchemy.run/sigil";
import { stringWidth } from "@alchemy.run/sigil/ansi";
import type { ReactNode } from "react";
import { spinnerFramesFor, statusColor, theme, type StatusVariant } from "../../../Util/Theme.ts";
import { useBorderStyle, useCliEnvironment, useGlyphs } from "./Environment.tsx";
import { Box, tabsWindow } from "./Layout.tsx";
import { Text } from "./Typography.tsx";
export interface StatusProps {
readonly variant?: StatusVariant;
readonly children?: ReactNode;
readonly detail?: ReactNode;
}
export function Status({ variant = "info", children, detail }: StatusProps) {
const glyphs = useGlyphs();
return (
{glyphs[variant]}
{children}
{detail === undefined ? null : · {detail}}
);
}
export type ToastProps = StatusProps;
/**
* Compact application notice. Severity is carried by the status glyph and
* its colour alone — the same vocabulary as transcript lines — so a notice
* row never needs a rail of its own.
*/
export function Toast({ variant = "info", children, detail }: ToastProps) {
return (
{children}
);
}
export interface AlertProps extends StatusProps {
readonly title?: ReactNode;
}
/** Glyph + bold title on one row, body indented beneath it. */
export function Alert({ variant = "info", title, children, detail }: AlertProps) {
const glyphs = useGlyphs();
return (
{glyphs[variant]}
{title === undefined ? null : {title}}
{detail === undefined ? null : · {detail}}
{children}
);
}
export interface KeyBarProps {
readonly keys: ReadonlyArray;
readonly marginTop?: number;
readonly inline?: boolean;
/** Widget rendered before the key hints. */
readonly before?: ReactNode;
/** Widget rendered after the key hints. */
readonly after?: ReactNode;
/** Draw border rails between populated widget/key sections. */
readonly divider?: boolean;
}
export function KeyBar({
keys,
marginTop = 1,
inline = false,
before,
after,
divider = false,
}: KeyBarProps) {
const borderStyle = useBorderStyle();
const sectionBorder = {
borderStyle,
borderLeft: true,
borderRight: false,
borderTop: false,
borderBottom: false,
borderColor: theme.color.muted,
borderDimColor: true,
marginLeft: 1,
paddingLeft: 1,
} as const;
return (
{before === undefined ? null : {before}}
{keys.map(([key, label], index) => (
{index === 0 ? null : • }
{key}
{label}
))}
{after === undefined ? null : (
{after}
)}
);
}
export const useSpinnerFrame = (): string => {
const { unicode } = useCliEnvironment();
const frames = spinnerFramesFor(unicode);
const { frame } = useAnimation({ interval: 80 });
return frames[frame % frames.length] ?? "-";
};
/**
* Spinner-as-status-icon: one animated frame, colorable so it can stand in
* for a status glyph in trees and progress rows.
*/
type SpinnerGlyphProps = { readonly color?: string };
export function SpinnerGlyph({ color }: SpinnerGlyphProps) {
return {useSpinnerFrame()};
}
type SpinnerProps = {
readonly label: ReactNode;
readonly detail?: ReactNode;
};
export function Spinner({ label, detail }: SpinnerProps) {
return (
{label}
{detail === undefined ? null : {detail}}
);
}
type TabsProps = {
readonly tabs: ReadonlyArray<{
readonly id: string;
readonly label: string;
readonly marked?: boolean;
}>;
readonly active: string;
};
export function Tabs({ tabs, active }: TabsProps) {
const glyphs = useGlyphs();
const { columns } = useCliEnvironment();
const gap = 1;
const activeIndex = Math.max(
0,
tabs.findIndex((tab) => tab.id === active),
);
// chip width = paddingX (2) + optional marker glyph + space + label
const widths = tabs.map(
(tab) => 2 + stringWidth(tab.label) + (tab.marked ? stringWidth(glyphs.selected) + 1 : 0),
);
const totalWidth =
widths.reduce((sum, width) => sum + width, 0) + Math.max(0, tabs.length - 1) * gap;
const contentWidth = Math.max(1, columns - theme.space.indent);
const { start, end } =
totalWidth <= contentWidth
? { start: 0, end: tabs.length }
: // reserve an arrow cell + gap on each side so the window stays put
// whether or not the edge arrows render
tabsWindow(widths, activeIndex, Math.max(1, contentWidth - 2 * (1 + gap)), gap);
return (
{start > 0 ? {glyphs.overflowLeft} : null}
{tabs.slice(start, end).map((tab) => {
const selected = tab.id === active;
return (
{tab.marked ? (
{glyphs.selected}{" "}
) : null}
{tab.label}
);
})}
{end < tabs.length ? {glyphs.overflowRight} : null}
);
}