Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

9.7 KB
Raw Blame
import { ResourceSectionHeading, StatusIndicator } from '@/components/ResourceLayout'
/*
 * Copyright 2025 BoxLite AI
 * SPDX-License-Identifier: AGPL-3.0
 */

import { cn } from '@/lib/utils'
import { Button } from '@/components/ui/button'
import { forwardRef, type ComponentProps, type ReactNode } from 'react'

/**
 * Shared primitives for the terminal/ASCII design language.
 *
 * These are presentation only — they take plain display values, never domain
 * objects — so a screen can adopt the look without the primitive learning
 * anything about boxes, wallets or plans.
 */

export const BRAND = 'hsl(var(--brand))'

// 5x7 dot-matrix LED glyphs — renders hero numbers as a dot display (telemetry/monitor vibe).
const DM_GLYPHS: Record<string, string[]> = {
  '0': ['01110', '10001', '10011', '10101', '11001', '10001', '01110'],
  '1': ['00100', '01100', '00100', '00100', '00100', '00100', '01110'],
  '2': ['01110', '10001', '00001', '00010', '00100', '01000', '11111'],
  '3': ['11110', '00001', '00001', '01110', '00001', '00001', '11110'],
  '4': ['00010', '00110', '01010', '10010', '11111', '00010', '00010'],
  '5': ['11111', '10000', '11110', '00001', '00001', '10001', '01110'],
  '6': ['00110', '01000', '10000', '11110', '10001', '10001', '01110'],
  '7': ['11111', '00001', '00010', '00100', '01000', '01000', '01000'],
  '8': ['01110', '10001', '10001', '01110', '10001', '10001', '01110'],
  '9': ['01110', '10001', '10001', '01111', '00001', '00010', '01100'],
  '.': ['00', '00', '00', '00', '00', '11', '11'],
  ',': ['00', '00', '00', '00', '00', '11', '10'],
}

const isNumeric = (s: string) => /^[\d.,]+$/.test(s)

function DotMatrix({ text, dot = 4, gap = 1 }: { text: string; dot?: number; gap?: number }) {
  return (
    <div className="flex items-end" style={{ gap: `${dot + gap}px` }}>
      {[...text].map((ch, i) => {
        const rows = DM_GLYPHS[ch]
        if (!rows) return <span key={i} style={{ width: `${dot * 2}px` }} />
        const cols = rows[0].length
        return (
          <div
            key={i}
            className="grid"
            style={{ gridTemplateColumns: `repeat(${cols}, ${dot}px)`, gridAutoRows: `${dot}px`, gap: `${gap}px` }}
          >
            {rows.flatMap((r, y) =>
              [...r].map((c, x) => (
                <span
                  key={`${y}-${x}`}
                  style={{
                    width: `${dot}px`,
                    height: `${dot}px`,
                    borderRadius: '50%',
                    background: c === '1' ? 'currentColor' : 'transparent',
                  }}
                />
              )),
            )}
          </div>
        )
      })}
    </div>
  )
}

// `onClick` turns the card into a filter control: on an inventory screen the
// counts are the fastest way to narrow the list, so making them inert wastes
// the most prominent element on the page. Cards without it stay plain divs.
export function StatCard({
  label,
  value,
  sub,
  live,
  onClick,
  active,
}: {
  label: string
  value: string
  sub: string
  live?: boolean
  onClick?: () => void
  active?: boolean
}) {
  const Tag = onClick ? 'button' : 'div'
  return (
    <Tag
      {...(onClick ? { type: 'button' as const, onClick, 'aria-pressed': active } : {})}
      className={cn(
        'flex flex-col gap-2 border bg-card px-3 py-2.5 text-left transition-transform hover:-translate-y-0.5 sm:gap-[14px] sm:px-[22px] sm:pb-5 sm:pt-[18px]',
        active ? 'border-brand' : 'border-border',
        onClick && !active && 'hover:border-muted-foreground/50',
      )}
    >
      <div className="flex items-start justify-between gap-1">
        <span className="font-sans text-label uppercase leading-tight tracking-[1px] text-muted-foreground sm:whitespace-nowrap sm:text-label sm:tracking-[1.5px]">
          <span style={{ color: BRAND }}>▸</span> {label}
        </span>
        {live && (
          <span className="inline-flex shrink-0 items-center gap-[5px] font-sans text-label tracking-[1px] text-muted-foreground">
            <span
              className="size-[6px] rounded-full"
              style={{ background: BRAND, animation: 'live-pulse 1.6s infinite' }}
            />
            <span className="hidden sm:inline">LIVE</span>
          </span>
        )}
      </div>
      {/* mobile: compact numeric value (the dot-matrix is too tall for a 3-up row) */}
      <div className="font-mono text-section font-semibold leading-none tracking-[-0.5px] sm:hidden">{value}</div>
      {/* desktop: dot-matrix value + sub label */}
      <div className="hidden items-end gap-[10px] sm:flex">
        {isNumeric(value) ? (
          <span className="text-foreground">
            <DotMatrix text={value} />
          </span>
        ) : (
          <div className="font-mono text-[34px] font-semibold leading-none tracking-[-1px]">{value}</div>
        )}
        <span className="mb-[2px] font-sans text-label uppercase tracking-[0.5px] text-muted-foreground">{sub}</span>
      </div>
    </Tag>
  )
}

/** Retains the existing section API while sharing its hierarchy with resource details. */
export function SectionTitle({ title, count, right }: { title: string; count?: ReactNode; right?: ReactNode }) {
  return <ResourceSectionHeading title={title} count={count} right={right} />
}

/**
 * Segmented progress bar. Warns at 80% and alerts at 90% of `limit`, so a bar
 * reads as a status at a glance rather than needing its label parsed.
 * `limit` of 0 renders an empty track — callers with no ceiling to show pass 0.
 */
export function SegmentedBar({ used, limit, segments = 40 }: { used: number; limit: number; segments?: number }) {
  const ratio = limit > 0 ? used / limit : 0
  const filled = Math.max(0, Math.min(segments, Math.round(ratio * segments)))
  const color = ratio >= 0.9 ? 'hsl(var(--destructive))' : ratio >= 0.8 ? 'hsl(var(--warning))' : BRAND
  return (
    <div className="flex flex-1 gap-[3px]">
      {Array.from({ length: segments }).map((_, i) => (
        <span
          key={i}
          className="h-1.5 flex-1"
          // Empty track is neutral: a pale tint of the fill colour read as
          // "filled, faintly", so the bar had no visible edge.
          style={{ background: i < filled ? color : 'hsl(var(--border))' }}
        />
      ))}
    </div>
  )
}

/** Label / big value / caption triple used in card headers. */
export function Metric({ label, value, sub }: { label: string; value: ReactNode; sub?: ReactNode }) {
  return (
    <div className="flex flex-col gap-1">
      <span className="font-sans text-label uppercase tracking-[1.5px] text-muted-foreground">
        <span style={{ color: BRAND }}>▸</span> {label}
      </span>
      <span className="font-mono text-page font-semibold leading-none tracking-tight tabular-nums text-foreground">
        {value}
      </span>
      {sub && <span className="font-sans text-meta text-muted-foreground">{sub}</span>}
    </div>
  )
}

/**
 * Semantic names for the shared terminal button styles. Forward native props
 * and refs so the same control also works as a Radix dialog/dropdown trigger.
 */
export const AsciiButton = forwardRef<
  HTMLButtonElement,
  ComponentProps<'button'> & {
    variant?: 'primary' | 'secondary' | 'danger' | 'dangerSolid' | 'quiet' | 'quiet-danger'
    size?: 'default' | 'sm' | 'icon' | 'icon-lg'
  }
>(function AsciiButton({ variant = 'secondary', size = 'default', type = 'button', ...props }, ref) {
  return (
    <Button
      ref={ref}
      type={type}
      variant={
        variant === 'quiet' || variant === 'quiet-danger'
          ? variant
          : variant === 'primary'
            ? 'default'
            : variant === 'dangerSolid'
              ? 'destructive'
              : variant === 'danger'
                ? 'danger'
                : 'outline'
      }
      size={size === 'icon' ? 'icon-sm' : size}
      {...props}
    />
  )
})

/** Selectable chip — used for top-up presets. */
export function AsciiChip({
  selected,
  className,
  children,
  ...props
}: ComponentProps<'button'> & { selected?: boolean }) {
  return (
    <button
      type="button"
      className={cn(
        'border border-border px-4 py-2 font-mono text-body tabular-nums transition-colors focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring disabled:cursor-not-allowed disabled:opacity-40',
        selected
          ? 'bg-selection font-semibold text-selection-foreground hover:[&:not(:disabled)]:bg-selection'
          : 'bg-transparent text-muted-foreground hover:[&:not(:disabled)]:bg-control-hover hover:[&:not(:disabled)]:text-foreground',
        className,
      )}
      {...props}
    >
      {children}
    </button>
  )
}

const STATUS_COLOR = {
  ok: 'hsl(var(--success))',
  warn: 'hsl(var(--warning))',
  bad: 'hsl(var(--destructive))',
  idle: 'hsl(var(--muted-foreground))',
} as const

/** Square swatch + label. Replaces rounded badges, which clash with the square language. */
export function StatusMark({ tone, children }: { tone: keyof typeof STATUS_COLOR; children: ReactNode }) {
  return <StatusIndicator label={children} color={STATUS_COLOR[tone]} />
}

/** Bordered panel — the standard container for a section's content. */
export function Panel({ className, children }: { className?: string; children: ReactNode }) {
  return <div className={cn('border border-border bg-card', className)}>{children}</div>
}

/**
 * A note under a bar or field: plain explanatory text, no leading marker.
 *
 * Body notes stay unadorned, separate from section headings and metric labels.
 * Putting a heading marker on body text made a heading
 * and a caption underneath it look like the same kind of line.
 */
export function PanelNote({ children }: { children: ReactNode }) {
  return <p className="mt-1.5 font-sans text-meta text-muted-foreground">{children}</p>
}