Skip to content

DangerZone

The last part of a project's settings: what cannot easily be undone, one row each, its consequence in one sentence and its own confirmation. It sits apart, marked with the danger line, so nobody reaches it by accident.

import { FormRow, ConfirmDialog } from '@sypnia/ui/react'

On this page
  1. On a desktop
  2. On a phone
  3. Skeleton
  4. When deleting fails
  5. For someone who is not an owner
  6. When to use it
  7. For everyone
  8. Props

On a desktop

Transfer asks where and says what changes; pause can be resumed from the toast; delete asks to type the name.

Danger zone

Transfer the project

Move northwind-shop to another organization. Its members there decide who keeps access.

Pause the project

Its API stops answering and nothing is billed. Data and backups are kept. Resume at any time.

Delete the project

Deletes its databases, files, keys and every backup. This cannot be undone.

Code
import { useState } from 'react';
import {
  Alert, Button, ConfirmDialog, Field, FormRow, KeyValue, Panel, Select, Stack, Toaster, toast, useSypnia,
} from '@sypnia/ui/react';

// The last part of a project's settings: what cannot be easily undone, each with its own row, its consequence in one
// sentence and its own confirmation. It sits apart from the rest, with the danger line, so nobody reaches it by accident.
const en = {
  title: 'Danger zone',
  transfer: 'Transfer the project', transferDesc: 'Move northwind-shop to another organization. Its members there decide who keeps access.', transferBtn: 'Transfer',
  transferQ: 'Transfer northwind-shop?', transferTo: 'New organization', transferBody: 'What changes when it moves:', transferAction: 'Transfer project',
  billing: 'Billing', billingV: 'moves to the new organization from today', members: 'Members', membersV: 'keep access until the new owners change it', urls: 'URLs and keys', urlsV: 'stay the same',
  transferred: (o: string) => `northwind-shop now belongs to ${o}`,
  pause: 'Pause the project', pauseDesc: 'Its API stops answering and nothing is billed. Data and backups are kept. Resume at any time.', pauseBtn: 'Pause project',
  pauseQ: 'Pause northwind-shop?', pauseBody: 'Apps that use it get an error until you resume it. Nothing is deleted.', paused: 'northwind-shop is paused', resume: 'Resume', resumed: 'northwind-shop is running again',
  del: 'Delete the project', delDesc: 'Deletes its databases, files, keys and every backup. This cannot be undone.', delBtn: 'Delete project',
  delQ: 'Delete northwind-shop for good?', delBody: '3 databases, 41 GB of files, 6 API keys and 30 days of backups are deleted now. Apps using it stop working.', delAction: 'Delete project', deleted: 'northwind-shop was deleted',
  failed: 'The project could not be deleted', failedBody: 'A deploy is still running on it. Wait for it to finish, then try again. Nothing was deleted.',
  owner: 'Only owners can do this', ownerDesc: 'Ask an owner of Northwind: Ana Ruiz or Luis Ortega.',
};
const es: typeof en = {
  title: 'Zona de peligro',
  transfer: 'Transferir el proyecto', transferDesc: 'Mueve northwind-shop a otra organización. Sus miembros allá deciden quién conserva el acceso.', transferBtn: 'Transferir',
  transferQ: '¿Transferir northwind-shop?', transferTo: 'Organización nueva', transferBody: 'Lo que cambia al moverlo:', transferAction: 'Transferir proyecto',
  billing: 'Facturación', billingV: 'pasa a la organización nueva desde hoy', members: 'Miembros', membersV: 'conservan el acceso hasta que los nuevos dueños lo cambien', urls: 'URLs y llaves', urlsV: 'siguen iguales',
  transferred: (o: string) => `northwind-shop ahora es de ${o}`,
  pause: 'Pausar el proyecto', pauseDesc: 'Su API deja de responder y no se cobra nada. Los datos y las copias se guardan. Reanúdalo cuando quieras.', pauseBtn: 'Pausar proyecto',
  pauseQ: '¿Pausar northwind-shop?', pauseBody: 'Las apps que lo usan reciben un error hasta que lo reanudes. No se borra nada.', paused: 'northwind-shop está en pausa', resume: 'Reanudar', resumed: 'northwind-shop funciona de nuevo',
  del: 'Borrar el proyecto', delDesc: 'Borra sus bases de datos, archivos, llaves y todas las copias. No se puede deshacer.', delBtn: 'Borrar proyecto',
  delQ: '¿Borrar northwind-shop para siempre?', delBody: 'Se borran ya 3 bases de datos, 41 GB de archivos, 6 llaves de API y 30 días de copias. Las apps que lo usan dejan de funcionar.', delAction: 'Borrar proyecto', deleted: 'Se borró northwind-shop',
  failed: 'No se pudo borrar el proyecto', failedBody: 'Todavía hay un despliegue corriendo en él. Espera a que termine e intenta de nuevo. No se borró nada.',
  owner: 'Solo los dueños pueden hacer esto', ownerDesc: 'Pídeselo a un dueño de Northwind: Ana Ruiz o Luis Ortega.',
};
const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));
const orgs = [{ value: 'acme', label: 'Acme Retail' }, { value: 'lumen', label: 'Lumen Labs' }];

export default function DangerZone({ state = 'ready' }: { state?: 'ready' | 'loading' | 'error' | 'not-owner' }) {
  const { locale } = useSypnia();
  const c = locale.startsWith('es') ? es : en;
  const [open, setOpen] = useState<'transfer' | 'pause' | 'delete' | null>(null);
  const [busy, setBusy] = useState(false);
  const [org, setOrg] = useState('acme');
  const [paused, setPaused] = useState(false);
  const [failed, setFailed] = useState(false);
  const loading = state === 'loading', locked = state === 'not-owner';
  const run = async (after: () => void) => { setBusy(true); await wait(1100); setBusy(false); setOpen(null); after(); };
  return (
    <>
      <Panel title={c.title}>
        <Stack gap={0}>
          {locked && <Alert tone="info" title={c.owner}>{c.ownerDesc}</Alert>}
          {failed && <Alert tone="fail" title={c.failed} onDismiss={() => setFailed(false)}>{c.failedBody}</Alert>}
          <FormRow label={c.transfer} description={c.transferDesc} loading={loading}>
            <Button disabled={locked} icon="swap" onClick={() => setOpen('transfer')}>{c.transferBtn}</Button>
          </FormRow>
          <FormRow label={c.pause} description={c.pauseDesc} loading={loading}>
            {paused ? <Button icon="play" onClick={() => { setPaused(false); toast(c.resumed, { tone: 'ok' }); }}>{c.resume}</Button>
              : <Button disabled={locked} icon="pause" onClick={() => setOpen('pause')}>{c.pauseBtn}</Button>}
          </FormRow>
          <FormRow label={c.del} description={c.delDesc} danger loading={loading}>
            <Button variant="danger" disabled={locked} icon="trash" onClick={() => setOpen('delete')}>{c.delBtn}</Button>
          </FormRow>
        </Stack>
      </Panel>
      <ConfirmDialog open={open === 'transfer'} tone="primary" title={c.transferQ} action={c.transferAction} busy={busy} onClose={() => setOpen(null)}
        onConfirm={() => run(() => toast(c.transferred(orgs.find((o) => o.value === org)!.label), { tone: 'ok' }))}>
        <Stack gap={4}>
          <Field label={c.transferTo}><Select options={orgs} value={org} onChange={setOrg} /></Field>
          <p>{c.transferBody}</p>
          <KeyValue items={[[c.billing, c.billingV], [c.members, c.membersV], [c.urls, c.urlsV]]} />
        </Stack>
      </ConfirmDialog>
      <ConfirmDialog open={open === 'pause'} tone="primary" title={c.pauseQ} action={c.pauseBtn} busy={busy} onClose={() => setOpen(null)}
        onConfirm={() => run(() => { setPaused(true); toast(c.paused, { action: { label: c.resume, onClick: () => setPaused(false) } }); })}>
        <p>{c.pauseBody}</p>
      </ConfirmDialog>
      <ConfirmDialog open={open === 'delete'} title={c.delQ} action={c.delAction} confirmWord="northwind-shop" busy={busy} onClose={() => setOpen(null)}
        onConfirm={() => run(() => (state === 'error' ? setFailed(true) : toast(c.deleted, { tone: 'ok' })))}>
        <p>{c.delBody}</p>
      </ConfirmDialog>
      <Toaster />
    </>
  );
}

On a phone

On a phone each button goes under its sentence, full width.

Skeleton

The rows keep their words while the project's state is checked; only the buttons wait.

Danger zone

Transfer the project

Move northwind-shop to another organization. Its members there decide who keeps access.

Pause the project

Its API stops answering and nothing is billed. Data and backups are kept. Resume at any time.

Delete the project

Deletes its databases, files, keys and every backup. This cannot be undone.

When deleting fails

Type the name and confirm: the deletion is refused, and the message says why and that nothing was deleted.

Danger zone

Transfer the project

Move northwind-shop to another organization. Its members there decide who keeps access.

Pause the project

Its API stops answering and nothing is billed. Data and backups are kept. Resume at any time.

Delete the project

Deletes its databases, files, keys and every backup. This cannot be undone.

For someone who is not an owner

The zone is there, disabled, with who can do it.

Danger zone

Only owners can do this

Ask an owner of Northwind: Ana Ruiz or Luis Ortega.

Transfer the project

Move northwind-shop to another organization. Its members there decide who keeps access.

Pause the project

Its API stops answering and nothing is billed. Data and backups are kept. Resume at any time.

Delete the project

Deletes its databases, files, keys and every backup. This cannot be undone.

When to use it

Use it when

  • Transfer, pause, delete: actions on the whole project or account.
  • Saying in the row what happens and whether it can be undone, before any dialog.

Do not use it when

  • Mixing these rows with ordinary settings.
  • Hiding the zone from people who cannot use it: show it disabled and say who can.

For everyone

  • Each row names its control: the button is read with the row's title and sentence.
  • Disabled buttons stay in the page and the note above says why.

Props

FormRowTypeDefault
labelrequired ReactNode
danger boolean
description ReactNode
htmlFor string
loading

The setting's value is on its way: the text stays and the control (`children`, when given) is drawn as a skeleton of its own box; without children, a skeleton of a control's height.

boolean
ConfirmDialogTypeDefault
actionrequired

The confirm button says exactly what it does: «Delete table», never «OK».

string
onCloserequired () => void
onConfirmrequired () => void
titlerequired

The question, naming the thing: «Delete the table orders?»

ReactNode
busy boolean
children

What will happen, in plain words, and whether it can be undone.

ReactNode
confirmWord

For the most destructive actions: the person must type this word (usually the name of the thing) to enable the button.

string
open boolean true
tone

danger: it cannot be undone. primary: it can, but it matters.

"primary" | "danger" danger
  1. On a desktop
  2. On a phone
  3. Skeleton
  4. When deleting fails
  5. For someone who is not an owner
  6. When to use it
  7. For everyone
  8. Props

    Search pages, sections and terms