Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

5.9 KB
Raw Blame
/**
 * Confirmation dialog for destructive or overwriting actions (delete, overwrite-on-update,
 * save-to-file, …), deliberately minimal: **no title bar** — just the tone mark, the message and a
 * small Cancel / Confirm pair, so every confirmation in the app is one compact, identical card.
 * `tone` picks the look — danger for deletions, primary for saves and other overwrites. The
 * message and any details (e.g. a version list) go in children; `title` only names the dialog for
 * assistive tech (never rendered).
 *
 * Both button labels are the caller's copy: what "confirm" means is the action's own verb
 * (Delete, Overwrite, Save), and the app's dictionary owns the Cancel beside it.
 *
 * A dialog may add ONE extra choice (`secondaryLabel` + `onSecondary`) between Cancel and Confirm
 * — for prompts whose recommended path is the confirm button while a plain "do it anyway" escape
 * hatch has to stay one click away (the mid-chat thinking-level switch: compact-then-switch /
 * switch anyway / cancel). It stays the same card: a neutral button in the same row, never a
 * second primary. `confirmDisabled` covers the case where that recommended action is temporarily
 * unavailable — the body then says why, and the other choices stay live.
 *
 * A confirmation whose action is a write can also report the refusal of that write (`refusal`):
 * the server's words render inside the card, under its sentence, so the reason sits next to the
 * button that retries it. A toast says the same thing, but it dismisses itself out of the card
 * the user is still looking at.
 */
import type { ReactNode } from "react";
import { Button } from "../../actions/button/button";
import { NoticeStrip } from "../../feedback/notice/notice-strip";
import { ICONS } from "../../icons/icons";
import { Modal } from "../modal/modal";

/**
 * The card's leading mark: a warning triangle for danger, a pencil for confirmations that
 * overwrite or save. The tone is carried by the glyph's ink alone, on the same neutral disc for
 * both — an icon never sits in a tint of its own tone.
 */
function ToneMark({ tone, glyph: own }: { tone: "danger" | "primary"; glyph?: string }) {
  const glyph = own ?? (tone === "danger" ? ICONS.triangleAlert : ICONS.penLine);
  return (
    <span
      aria-hidden
      className={`flex h-9 w-9 shrink-0 items-center justify-center rounded-full bg-line-muted ${
        tone === "danger" ? "text-tone-danger-fg" : "text-tone-neutral-fg"
      }`}
    >
      <svg
        width="18"
        height="18"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        strokeWidth="1.7"
        strokeLinecap="round"
        strokeLinejoin="round"
      >
        <path d={glyph} />
      </svg>
    </span>
  );
}

export interface ConfirmModalProps {
  open: boolean;
  /** Accessible dialog name only — the compact card renders no title bar. */
  title: string;
  onClose: () => void;
  onConfirm: () => void;
  /** Confirm button text: the action's own verb where it has one, else the app's "Confirm". */
  confirmLabel: string;
  /** Cancel button text, from the app's dictionary. */
  cancelLabel: string;
  /** Disables ONLY the confirm button (its action is temporarily unavailable — the body copy is expected to say why); Cancel and the secondary action stay clickable. */
  confirmDisabled?: boolean;
  /** Optional third choice, rendered as a neutral button between Cancel and Confirm; needs `onSecondary` to appear. */
  secondaryLabel?: string;
  /** Handler for the third choice (the dialog does not close itself — the caller decides, exactly like onConfirm). */
  onSecondary?: () => void;
  /** Confirm button variant: danger for deletions, primary for saves and other overwrites. */
  tone?: "danger" | "primary";
  /**
   * The leading mark's drawing (an `ICONS` path) when the tone's default does not say what the
   * action does — a download for an install, say. The tone still picks its ink.
   */
  glyph?: string;
  busy?: boolean;
  /**
   * The words for a write this confirmation asked for and the server refused, rendered under the
   * card's sentence. Absent (not empty) when nothing was refused, so the slot is simply not drawn.
   */
  refusal?: string;
  children: ReactNode;
}

export function ConfirmModal({
  open,
  title,
  onClose,
  onConfirm,
  confirmLabel,
  cancelLabel,
  confirmDisabled = false,
  secondaryLabel,
  onSecondary,
  tone = "danger",
  glyph,
  busy = false,
  refusal,
  children,
}: ConfirmModalProps) {
  return (
    <Modal open={open} title={title} onClose={onClose} headerless widthClass="sm:max-w-sm">
      <div className="flex items-start gap-3">
        <ToneMark tone={tone} {...(glyph !== undefined ? { glyph } : {})} />
        <div className="min-w-0 flex-1 pt-1.5">
          {children}
          {refusal !== undefined && (
            // The card's own words, in the card's own width: a small strip under the sentence so
            // the reason reads as part of the question that was refused, not as page-level news.
            <NoticeStrip
              tone="danger"
              role="alert"
              className="mt-3 rounded-md border px-2.5 py-2 text-xs"
            >
              <span data-slot="body">{refusal}</span>
            </NoticeStrip>
          )}
        </div>
      </div>
      {/* Wraps rather than overflows: three choices with long labels don't fit one row inside
          the bottom-sheet width on a phone (a two-button card never reaches the wrap point). */}
      <div className="mt-4 flex flex-wrap justify-end gap-2">
        <Button size="sm" onClick={onClose} disabled={busy}>
          {cancelLabel}
        </Button>
        {secondaryLabel !== undefined && onSecondary !== undefined && (
          <Button size="sm" onClick={onSecondary} disabled={busy}>
            {secondaryLabel}
          </Button>
        )}
        <Button size="sm" variant={tone} disabled={busy || confirmDisabled} onClick={onConfirm}>
          {confirmLabel}
        </Button>
      </div>
    </Modal>
  );
}