Watch
0
0
Fork
You've already forked hyperhive
0

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:
iris 2026-11-19 18:42:34 +01:00 • committed by mara
commit c3e24fe2c6
2 changed files with 353 additions and 60 deletions

View file

@ -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}
>

View file

@ -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];
}