devsandbox 0.11.1

Manage devcontainer-style sandboxes on docker, podman, or Apple container — many throwaway instances per repo from one TOML config.
---
/**
 * The mini dashboard: a playable window modelled on the real TUI
 * (`src/tui/ui.rs`). The markup is the no-JS state (`STATIC` in
 * `lib/mini-tui.ts`), rendered with the same span helpers the driver uses; a
 * host script brings it to life with `mountMiniTui` (`scripts/mini-tui.ts`),
 * which drives the `data-tui-*` hooks and reports state changes to the host
 * (the landing diagram, a docs tutorial). Every part a host may point at
 * carries `data-tui-region` (tabs, instances, detail, services, ports, inbox,
 * terminal, prompt, hints, help, logs, config, confirm, suspended, status).
 */
import { SquareTerminal } from '@lucide/astro';
import {
  detailLines,
  instanceCells,
  type Line,
  sandboxCells,
  SANDBOX,
  SERVICE,
  STATIC,
  TABS,
  TAB_LABEL,
  totalsLine,
  usedBy,
} from '../lib/mini-tui';
import { SERVICE_CONTAINER } from '../lib/mini-tui-content';
import { VERSION } from '../lib/nav';
import '../styles/mini-tui.css';

interface Props {
  /** Prefix for element ids (`<id>-keys`), unique per widget on a page. */
  id?: string;
  /** Play the autoplay story while on screen (read by `mountMiniTui`). */
  autoplay?: boolean;
  /** Take keys and clicks; off, it's a picture a host drives. */
  interactive?: boolean;
  label?: string;
}

const { id = 'tui', autoplay = false, interactive = true, label = 'Dashboard preview' } = Astro.props;

const unread = STATIC.unread;
const keysId = `${id}-keys`;
const selected = STATIC.instances[STATIC.selected]?.name;
const escape = (t: string) => t.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
/**
 * A Line (spans with colour classes) as one HTML string: the rows are
 * `white-space: pre`, so no formatting whitespace may sneak in between spans.
 */
const spansHtml = (line: Line) =>
  line.map((s) => (s.cls ? `<span class="${s.cls}">${escape(s.text)}</span>` : escape(s.text))).join('');
/** Keycap button, out of the tab order (the widget is one tab stop). */
const cap = (key: string, text: string) => ({ key, text });
/**
 * A dashboard hint (`draw_help`'s per-tab list, trimmed): keycaps, a label, the
 * tabs it shows on (all when unset), an optional live verb; `wide` ones only
 * fit a wider widget than the landing page's (the `?` overlay lists them all).
 */
const dashHints: {
  caps: { key: string; text: string }[];
  text: string;
  tabs?: string;
  verb?: string;
  terms?: boolean;
  wide?: boolean;
}[] = [
  { caps: [cap('ArrowUp', '↑'), cap('ArrowDown', '↓')], text: 'select' },
  { caps: [cap('Enter', 'enter')], text: 'config', tabs: 'instances services' },
  { caps: [cap('o', 'o')], text: 'vscode', tabs: 'instances' },
  { caps: [cap('s', 's')], text: 'stop', tabs: 'instances', verb: 's' },
  { caps: [cap('l', 'l')], text: 'logs', tabs: 'instances', wide: true },
  { caps: [cap('b', 'b')], text: 'build log', tabs: 'instances', wide: true },
  { caps: [cap('p', 'p')], text: 'forward', tabs: 'instances', wide: true },
  { caps: [cap('d', 'd')], text: 'rm', tabs: 'instances', verb: 'd', wide: true },
  { caps: [cap('p', 'p')], text: 'forward', tabs: 'services' },
  { caps: [cap('d', 'd')], text: 'stop forward', tabs: 'ports' },
  { caps: [cap('t', 't')], text: 'term', tabs: 'instances' },
  { caps: [cap(':', ':')], text: 'cmd' },
  { caps: [cap('Tab', 'tab')], text: 'switch', wide: true },
  { caps: [cap('?', '?')], text: 'help' },
  { caps: [cap('[', '['), cap(']', ']')], text: 'terms', terms: true },
  { caps: [cap('x', 'x')], text: 'close term', terms: true },
];
---

<div
  class="tui-window"
  data-tui
  data-tui-autoplay={String(autoplay)}
  data-tui-interactive={String(interactive)}
  role="group"
  aria-label={label}
  aria-describedby={interactive ? keysId : undefined}
>
  {
    interactive && (
      <p class="visually-hidden" id={keysId}>
        Arrow keys or j and k select an instance, o opens it in VS Code, Escape closes VS Code, t opens a terminal on it,
        s stops or starts it, d removes it (or cancels its build), l shows its logs, b its build log, p forwards a port, Enter
        opens the config explorer, colon opens the
        command prompt where Tab completes and Escape cancels, left and right arrows or 1 to 4 switch tabs, question
        mark shows help. In a terminal, keys go to the shell and Escape returns to the dashboard; square brackets switch
        terminals and x closes one. Tab leaves the preview.
      </p>
    )
  }
  <p class="visually-hidden" aria-live="polite" data-tui-live></p>
  <div class="window-bar" aria-hidden="true">
    <div class="window-dots"><i></i><i></i><i></i></div>
    <div class="window-title"><SquareTerminal size={13} /> devsandbox — ~/sandboxes</div>
    <div class="window-bar-right">tui</div>
  </div>

  <div class="tui-screen">
    <div class="tui-tabs" data-tui-region="tabs">
      {
        TABS.map((t) => (
          <button type="button" tabindex="-1" class="tui-tab" data-tui-tab={t} data-active={t === STATIC.tab ? '' : undefined}>
            {TAB_LABEL[t]}
            {t === 'inbox' && (
              <span class="tui-unread" data-tui-unread hidden={unread === 0}>
                {` (${unread})`}
              </span>
            )}
          </button>
        ))
      }
      <span class="tui-totals" data-tui-totals>{totalsLine(STATIC)}</span>
    </div>

    <div class="tui-body">
      <div class="tui-panel" data-tui-panel="instances">
        <div class="tui-box tui-table" data-tui-region="instances">
          <span class="tui-box-title">Instances</span>
          <div class="tui-tr tui-th">
            <span>TREE</span><span>STATUS</span><span>UPTIME</span><span>FOLDER</span>
          </div>
          <div class="tui-tr tui-row tui-group" data-tui-group={SANDBOX}>
            {sandboxCells(STATIC).map((line) => <span><Fragment set:html={spansHtml(line)} /></span>)}
          </div>
          <div class="tui-rows" data-tui-rows>
            {
              STATIC.instances.map((i) => (
                <div class="tui-tr tui-row" data-tui-row={i.name} data-selected={i.name === selected ? '' : undefined}>
                  {instanceCells(STATIC, i).map((line) => <span><Fragment set:html={spansHtml(line)} /></span>)}
                </div>
              ))
            }
          </div>
        </div>
        <div class="tui-box tui-detail" data-tui-region="detail">
          <span class="tui-box-title">Detail</span>
          <div class="tui-kv" data-tui-detail>
            {detailLines(STATIC).map((line) => <div><Fragment set:html={spansHtml(line)} /></div>)}
          </div>
        </div>
      </div>

      <div class="tui-panel" data-tui-panel="services" hidden>
        <div class="tui-box tui-table" data-tui-region="services">
          <span class="tui-box-title">Services</span>
          <div class="tui-tr tui-th tui-tr--services">
            <span>NAME</span><span>SCOPE</span><span>STATUS</span><span>USED BY</span>
          </div>
          <div class="tui-tr tui-row tui-tr--services" data-tui-row={SERVICE} data-selected>
            <span>{SERVICE}</span>
            <span class="tui-blue">global</span>
            <span class="tui-green">1 running</span>
            <span data-tui-used-by>{usedBy(STATIC)}</span>
          </div>
        </div>
        <div class="tui-box tui-detail">
          <span class="tui-box-title">Detail</span>
          <div class="tui-kv">
            <div>{SERVICE_CONTAINER}{'  '}<span class="tui-green">running</span></div>
            <div><span class="tui-dim">ports: </span>-</div>
            <div><span class="tui-dim">env: </span>1</div>
          </div>
        </div>
      </div>

      <div class="tui-panel" data-tui-panel="ports" hidden>
        {/* `draw_ports_empty` / `draw_ports_table`: one box, the empty line or the table. */}
        <div class="tui-box tui-table tui-empty" data-tui-ports data-tui-region="ports">
          <span class="tui-box-title">Ports</span>
          <span class="tui-dim" data-tui-ports-empty
            >no forwards — press p on an instance or service, or :port &lt;instance&gt; &lt;port&gt;</span
          >
          <div class="tui-tr tui-th tui-tr--ports" data-tui-ports-head hidden>
            <span>LOCAL</span><span>TARGET</span><span>PROCESS</span><span>STATE</span><span>CONNS</span>
          </div>
          <div class="tui-rows" data-tui-port-rows></div>
        </div>
        {/* Like the real Ports tab: an empty Detail keeps the terminal split aligned. */}
        <div class="tui-box tui-detail tui-detail--terms">
          <span class="tui-box-title">Detail</span>
        </div>
      </div>

      <div class="tui-panel" data-tui-panel="inbox" hidden>
        <div class="tui-box tui-table" data-tui-region="inbox">
          <span class="tui-box-title">Inbox</span>
          <div class="tui-tr tui-th tui-tr--inbox">
            <span>TIME</span><span>LEVEL</span><span>INSTANCE</span><span>MESSAGE</span>
          </div>
          <span class="tui-dim tui-inbox-empty" data-tui-inbox-empty hidden={STATIC.notified}>
            no notifications — containers send them with <code>devsbd notify "…"</code>
          </span>
          <div class="tui-tr tui-row tui-tr--inbox" data-tui-row="notify-1" data-tui-notified data-selected data-unread>
            <span class="tui-dim">just now</span>
            <span class="tui-dim">info</span>
            <span>web-3</span>
            <span>tests pass, ready for review</span>
          </div>
        </div>
        <div class="tui-box tui-detail">
          <span class="tui-box-title">Detail</span>
          <div class="tui-kv" data-tui-notified>
            <div><span class="tui-dim">Oct 2 14:32</span>{'  '}<span class="tui-dim">info</span>{'  '}web-3</div>
            <div>tests pass, ready for review</div>
          </div>
        </div>
      </div>

      {/* The integrated terminal (`draw_terminal_panel` in `src/tui/ui.rs`): tab strip as the title. */}
      <div class="tui-box tui-term" data-tui-term data-tui-region="terminal" hidden>
        <span class="tui-box-title tui-term-tabs" data-tui-term-tabs></span>
        <div class="tui-term-screen" data-tui-term-screen></div>
      </div>

      {/* `l`: the logs modal (`Modal::Logs`, drawn by `draw_text_modal`); `b`'s build log uses it too. */}
      <div class="tui-view" data-tui-view="logs" data-tui-region="logs" hidden>
        <div class="tui-box tui-pane" data-focused>
          <span class="tui-box-title" data-tui-logs-title></span>
          <div class="tui-view-text" data-tui-logs-body></div>
        </div>
      </div>

      {/* `enter`: the config explorer (`draw_config_modal`), config | inspect. */}
      <div class="tui-view tui-view--split" data-tui-view="config" data-tui-region="config" hidden>
        <div class="tui-box tui-pane" data-focused>
          <span class="tui-box-title" data-tui-config-title></span>
          <div class="tui-view-text" data-tui-config-body></div>
        </div>
        <div class="tui-box tui-pane">
          <span class="tui-box-title" data-tui-inspect-title></span>
          <div class="tui-view-text" data-tui-inspect-body></div>
        </div>
      </div>

      {/* The `?` overlay: the real HELP_BODY's keys that work here, plus where this preview differs. */}
      <div class="tui-modal" data-tui-help-overlay data-tui-region="help" hidden>
        <span class="tui-box-title">help — keys</span>
        <dl>
          <dt>↑/k ↓/j</dt><dd>move selection</dd>
          <dt>tab 1-4</dt><dd>switch · jump to tab</dd>
          <dt>enter, e</dt><dd>config explorer</dd>
          <dt>:</dt><dd>command prompt</dd>
          <dt>r</dt><dd>run / rename</dd>
          <dt>o</dt><dd>open in VS Code</dd>
          <dt>s</dt><dd>stop / start</dd>
          <dt>l</dt><dd>logs (Instances)</dd>
          <dt>b</dt><dd>build log (Instances)</dd>
          <dt>p</dt><dd>forward a port</dd>
          <dt>d</dt><dd>rm / cancel build (Instances)</dd>
          <dt>d</dt><dd>stop forward (Ports)</dd>
          <dt>t</dt><dd>open terminal</dd>
          <dt>[ / ]</dt><dd>prev / next terminal</dd>
          <dt>x</dt><dd>close terminal</dd>
          <dt>ctrl-]</dt><dd>terminal ⇄ dashboard</dd>
        </dl>
        <p class="tui-dim">
          In this preview ←/→ switch tabs (Tab leaves it; click a tab instead), esc
          closes VS Code or leaves a terminal, and the <span class="tui-accent">▾ web</span> row is picked with a click.
          In the <span class="tui-accent">:</span> prompt Tab completes; in the config explorer{' '}
          <span class="tui-accent">t</span> toggles original / resolved.
        </p>
        <p class="tui-dim">esc or ? to close</p>
      </div>

      {/* A yes/no question over the dashboard (`draw_confirm_modal`): rm's branch question, cancelling a build. */}
      <div class="tui-modal tui-confirm" data-tui-confirm data-tui-region="confirm" role="alertdialog" hidden>
        <div data-tui-confirm-question></div>
        <div class="tui-dim">{'[y] yes  [n] no'}</div>
      </div>
    </div>

    {/* One hint bar per mode, like `draw_help` in `src/tui/ui.rs`; the prompt takes its place. */}
    <div class="tui-help" data-tui-help="dashboard" data-tui-region="hints">
      {
        dashHints.map((h) => (
          <span
            class:list={['tui-hint', { 'tui-hint--wide': h.wide }]}
            data-tui-for={h.tabs}
            data-tui-terms-hint={h.terms ? '' : undefined}
            hidden={(h.tabs !== undefined && !h.tabs.split(' ').includes(STATIC.tab)) || h.terms}
          >
            {h.caps.map((c) => (
              <button type="button" tabindex="-1" class="tui-key" data-key={c.key}>
                {c.text}
              </button>
            ))}
            <span data-tui-verb={h.verb}>{h.text}</span>
          </span>
        ))
      }
    </div>
    <div class="tui-help" data-tui-help="terminal" data-tui-region="hints" hidden>
      <span class="tui-hint">
        <button type="button" tabindex="-1" class="tui-key" data-key="Escape">esc</button> / ctrl-] back to dashboard · all
        other keys go to the shell
      </span>
    </div>
    <div class="tui-help" data-tui-help="exited" data-tui-region="hints" hidden>
      <span class="tui-hint">
        terminal exited — <button type="button" tabindex="-1" class="tui-key" data-key="Escape">esc</button> back ·
        <button type="button" tabindex="-1" class="tui-key" data-key="x">x</button> closes
      </span>
    </div>
    <div class="tui-help" data-tui-help="logs" data-tui-region="hints" hidden>
      <span class="tui-hint">
        <button type="button" tabindex="-1" class="tui-key" data-key="ArrowUp">↑</button><button
          type="button"
          tabindex="-1"
          class="tui-key"
          data-key="ArrowDown">↓</button
        > scroll
      </span>
      <span class="tui-hint">
        <button type="button" tabindex="-1" class="tui-key" data-key="g">g</button><button
          type="button"
          tabindex="-1"
          class="tui-key"
          data-key="G">G</button
        > top / bottom
      </span>
      <span class="tui-hint"><button type="button" tabindex="-1" class="tui-key" data-key="Escape">esc</button> close</span>
    </div>
    <div class="tui-help" data-tui-help="config" data-tui-region="hints" hidden>
      <span class="tui-hint"><button type="button" tabindex="-1" class="tui-key" data-key="t">t</button> toggle</span>
      <span class="tui-hint">
        <button type="button" tabindex="-1" class="tui-key" data-key="ArrowUp">↑</button><button
          type="button"
          tabindex="-1"
          class="tui-key"
          data-key="ArrowDown">↓</button
        > scroll
      </span>
      <span class="tui-hint"><button type="button" tabindex="-1" class="tui-key" data-key="Escape">esc</button> close</span>
    </div>
    {/* `ConfirmModal::keys_hint`: rm's question (n keeps the branch, esc aborts), then a build's cancel. */}
    <div class="tui-help" data-tui-help="confirm-rm" data-tui-region="hints" hidden>
      <span class="tui-hint"><button type="button" tabindex="-1" class="tui-key" data-key="y">y</button>/enter yes</span>
      <span class="tui-hint"><button type="button" tabindex="-1" class="tui-key" data-key="n">n</button> no</span>
      <span class="tui-hint"><button type="button" tabindex="-1" class="tui-key" data-key="Escape">esc</button> cancel</span>
    </div>
    <div class="tui-help" data-tui-help="confirm-cancel" data-tui-region="hints" hidden>
      <span class="tui-hint"><button type="button" tabindex="-1" class="tui-key" data-key="y">y</button>/enter yes</span>
      <span class="tui-hint">
        <button type="button" tabindex="-1" class="tui-key" data-key="n">n</button>/<button
          type="button"
          tabindex="-1"
          class="tui-key"
          data-key="Escape">esc</button
        > no
      </span>
    </div>
    <div class="tui-help" data-tui-help="help" data-tui-region="hints" hidden>
      <span class="tui-hint">
        <button type="button" tabindex="-1" class="tui-key" data-key="Escape">esc</button> / ? close
      </span>
    </div>
    {/* `draw_prompt`: `: <input>` with the caret, then candidates (active one accented) or a red error. */}
    <div class="tui-prompt" data-tui-help="prompt" data-tui-region="prompt" hidden>
      <div class="tui-prompt-line"><span class="tui-accent">: </span><span data-tui-prompt-input></span><span class="tui-cursor"></span></div>
      <div class="tui-prompt-hint" data-tui-prompt-hint></div>
    </div>

    {/* `exec`, the one suspending command (`run_suspended` in `src/tui/mod.rs`): the dashboard is gone until a key. */}
    <div class="tui-suspended" data-tui-suspended data-tui-region="suspended" hidden>
      <div class="tui-dim">you@laptop:~/sandboxes$ devsandbox</div>
      <div data-tui-suspended-lines></div>
      <div class="tui-dim" data-tui-suspended-return>press any key to return</div>
    </div>
  </div>

  <div class="terminal-status" aria-hidden="true">
    <span><span class="status-indicator"></span> DASHBOARD PREVIEW</span>
    <span class="tui-hint-slot" data-tui-hint data-tui-region="status" hidden>CLICK TO TRY IT</span>
    <span>DEVSANDBOX v{VERSION}</span>
  </div>
</div>