swarm ui: ask before actions that cannot be undone, with a danger button and a useConfirm hook
Button gains a danger variant and an autofocus prop; ConfirmDialog gains danger (red confirm, focus on cancel). Destroy, delete linked account, delete claude login, remove schedule and delete flow use it. New useConfirm hook asks from inside a handler (await confirm({...})) without a dialog's state at the call site. It now guards: removing a swarm-wide egress domain, removing a domain for every agent from an agent's popover, removing a stored API-key environment, firing a schedule now, moving an agent's internet to a looser mode, the /clear message, and reload over unsaved instructions (replacing window.confirm). The review try-out's button is now called push a branch for a PR, since it pushes a branch and does not open one, and asks first. loosensInternet is a tested helper.
This commit is contained in:
parent
d9551415bf
commit
c3e24fe2c6
4 changed files with 353 additions and 60 deletions
|
|
@ -15,6 +15,11 @@
|
|||
color: var(--fg);
|
||||
border: none;
|
||||
}
|
||||
.ui-button-danger {
|
||||
background: color-mix(in srgb, var(--red) 14%, var(--bg-elev));
|
||||
color: var(--red);
|
||||
border: 1px solid var(--red);
|
||||
}
|
||||
.ui-button-default {
|
||||
background: var(--bg-elev);
|
||||
color: var(--fg);
|
||||
|
|
|
|||
|
|
@ -1,14 +1,13 @@
|
|||
// <Button> — the shared clickable-action control (form submit today,
|
||||
// any future toolbar/dialog action later). `variant` is `'primary'`
|
||||
// (the one emphasised action on a page, e.g. a form's submit) or
|
||||
// `'default'` (everything else) — two is enough for every real caller
|
||||
// so far; a third reading gets added the day something needs it, not
|
||||
// speculatively.
|
||||
// `'default'` (everything else), or `'danger'` (the action that cannot be
|
||||
// taken back: destroy, delete, a removal that cuts access).
|
||||
import type { ComponentChildren } from "preact";
|
||||
import { Spinner } from "../spinner/Spinner.js";
|
||||
import "./Button.css";
|
||||
|
||||
export type ButtonVariant = "primary" | "default";
|
||||
export type ButtonVariant = "primary" | "default" | "danger";
|
||||
|
||||
export function Button({
|
||||
variant = "default",
|
||||
|
|
@ -18,6 +17,7 @@ export function Button({
|
|||
onClick,
|
||||
describedBy,
|
||||
ariaLabel,
|
||||
autofocus = false,
|
||||
children,
|
||||
}: {
|
||||
variant?: ButtonVariant;
|
||||
|
|
@ -33,6 +33,9 @@ export function Button({
|
|||
/** A name that says what the button acts on, where its visible text repeats
|
||||
* across a list ("remove", "edit"). It should start with the visible text. */
|
||||
ariaLabel?: string;
|
||||
/** Takes the focus when the dialog around it opens, in place of its first
|
||||
* control. A destructive confirm puts it on the safe button. */
|
||||
autofocus?: boolean;
|
||||
children: ComponentChildren;
|
||||
}) {
|
||||
return (
|
||||
|
|
@ -44,6 +47,7 @@ export function Button({
|
|||
onClick={onClick}
|
||||
aria-describedby={describedBy}
|
||||
aria-label={ariaLabel}
|
||||
autofocus={autofocus || undefined}
|
||||
>
|
||||
<span class="ui-button-label">{children}</span>
|
||||
{busy ? (
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ export function ConfirmDialog({
|
|||
confirmLabel = "confirm",
|
||||
cancelLabel = "cancel",
|
||||
confirmDisabled = false,
|
||||
danger = false,
|
||||
children,
|
||||
}: {
|
||||
open: boolean;
|
||||
|
|
@ -33,6 +34,9 @@ export function ConfirmDialog({
|
|||
confirmLabel?: string;
|
||||
cancelLabel?: string;
|
||||
confirmDisabled?: boolean;
|
||||
/** The action cannot be taken back: the confirm button is red and the focus
|
||||
* starts on cancel. */
|
||||
danger?: boolean;
|
||||
children: ComponentChildren;
|
||||
}) {
|
||||
return (
|
||||
|
|
@ -40,9 +44,11 @@ export function ConfirmDialog({
|
|||
<div class="confirm-dialog">
|
||||
<div class="confirm-dialog-body">{children}</div>
|
||||
<div class="confirm-dialog-actions">
|
||||
<Button onClick={onCancel}>{cancelLabel}</Button>
|
||||
<Button onClick={onCancel} autofocus={danger}>
|
||||
{cancelLabel}
|
||||
</Button>
|
||||
<Button
|
||||
variant="primary"
|
||||
variant={danger ? "danger" : "primary"}
|
||||
onClick={onConfirm}
|
||||
disabled={confirmDisabled}
|
||||
>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
// useConfirm — ask before an action, in place, without a dialog's state at every
|
||||
// call site: `const [confirm, dialog] = useConfirm()`, render `{dialog}` once,
|
||||
// and `if (!(await confirm({ ... }))) return;` before the action. Resolves
|
||||
// true on the confirm button and false on cancel, Escape or a click outside.
|
||||
import type { ComponentChildren } from "preact";
|
||||
import { useRef, useState } from "preact/hooks";
|
||||
import { ConfirmDialog } from "./ConfirmDialog.js";
|
||||
|
||||
export interface ConfirmRequest {
|
||||
/** The dialog's accessible name. */
|
||||
label: string;
|
||||
confirmLabel: string;
|
||||
/** The action cannot be taken back: a red confirm button, focus on cancel. */
|
||||
danger?: boolean;
|
||||
/** What will happen, in a sentence or two. */
|
||||
body: ComponentChildren;
|
||||
}
|
||||
|
||||
export function useConfirm(): [
|
||||
(request: ConfirmRequest) => Promise<boolean>,
|
||||
ComponentChildren,
|
||||
] {
|
||||
const [request, setRequest] = useState<ConfirmRequest | null>(null);
|
||||
const resolveRef = useRef<((answer: boolean) => void) | null>(null);
|
||||
|
||||
function confirm(next: ConfirmRequest): Promise<boolean> {
|
||||
// A second request while one is open cancels the first.
|
||||
resolveRef.current?.(false);
|
||||
setRequest(next);
|
||||
return new Promise<boolean>((resolve) => {
|
||||
resolveRef.current = resolve;
|
||||
});
|
||||
}
|
||||
|
||||
function answer(yes: boolean) {
|
||||
resolveRef.current?.(yes);
|
||||
resolveRef.current = null;
|
||||
setRequest(null);
|
||||
}
|
||||
|
||||
const dialog = (
|
||||
<ConfirmDialog
|
||||
open={request !== null}
|
||||
label={request?.label ?? ""}
|
||||
confirmLabel={request?.confirmLabel}
|
||||
danger={request?.danger}
|
||||
onCancel={() => answer(false)}
|
||||
onConfirm={() => answer(true)}
|
||||
>
|
||||
{request?.body}
|
||||
</ConfirmDialog>
|
||||
);
|
||||
return [confirm, dialog];
|
||||
}
|
||||
Loading…
Reference in a new issue