SPB Git

Repository Public

Emergency.tsx 5.2 KB 104 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Social Engine & Remote Navigator8 * File             : apps/navigator-web/src/screens/Emergency.tsx9 * Module           : screens10 * Repository       : https://git.spboucher.ai/maclustr-social11 * Project Website  : https://social.spboucher.ai12 * Created          : 2026-12-0113 * Last Modified    : 2026-12-0114 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Permanent emergency-stop control. Engaging needs a typed reason and an explicit confirmation;43// only an owner may release it (the API enforces this too).44import { useId, useState, type FormEvent } from 'react';45import { post, type Me } from '../../../shared/api.ts';46import { useLang, useT } from '../i18n.ts';47import { IcoStop } from '../icons.tsx';48import { ErrorLine, fmtTime } from '../ui.tsx';4950export function EmergencyControl({ me, engaged, refreshMe, compact = false }: { me: Me; engaged: boolean; refreshMe: () => Promise<void>; compact?: boolean }) {51  const t = useT();52  const id = useId();53  const [open, setOpen] = useState(false);54  const [reason, setReason] = useState(''); const [sure, setSure] = useState(false);55  const [busy, setBusy] = useState(false); const [err, setErr] = useState<unknown>(null);56  const isOwner = me.user.role === 'owner';57  const engage = !engaged;58  const close = () => { setOpen(false); setReason(''); setSure(false); setErr(null); };59  const submit = async (e: FormEvent) => {60    e.preventDefault(); setBusy(true); setErr(null);61    try { await post('/v1/emergency-stop', { engage, reason: reason.trim() }); await refreshMe(); close(); } catch (x) { setErr(x); } finally { setBusy(false); }62  };63  const label = engaged ? t('em.release_open') : t('em.engage_open');64  return (65    <div className="em">66      <button type="button" className={`btn em-btn${engaged ? ' on' : ''}`} aria-expanded={open} aria-controls={`${id}-p`} onClick={() => (open ? close() : setOpen(true))} aria-label={label} title={label}>67        <IcoStop /><span className={compact ? 'sr-only' : undefined}>{label}</span>68      </button>69      {open && (70        <form id={`${id}-p`} className="em-panel" onSubmit={submit} aria-label={label}>71          <h2>{engaged ? t('em.release_title') : t('em.engage_title')}</h2>72          <p>{engaged ? t('em.release_lead') : t('em.engage_lead')}</p>73          {engaged && !isOwner ? <p className="warn-text">{t('em.owner_only')}</p> : <>74            <label htmlFor={`${id}-r`}>{t('em.reason')}</label>75            <textarea id={`${id}-r`} value={reason} onChange={(e) => setReason(e.target.value)} minLength={3} maxLength={500} rows={3} required />76            <label className="check"><input type="checkbox" checked={sure} onChange={(e) => setSure(e.target.checked)} /> <span>{engaged ? t('em.release_confirm') : t('em.engage_confirm')}</span></label>77            <ErrorLine error={err} />78            <div className="row">79              <button type="submit" className={`btn ${engaged ? 'primary' : 'danger'}`} disabled={busy || !sure || reason.trim().length < 3}>{busy ? t('common.working') : engaged ? t('em.release_do') : t('em.engage_do')}</button>80              <button type="button" className="btn" onClick={close}>{t('common.cancel')}</button>81            </div>82          </>}83          {engaged && !isOwner && <div className="row"><button type="button" className="btn" onClick={close}>{t('common.close')}</button></div>}84        </form>85      )}86    </div>87  );88}8990export function EmergencyBanner({ me, engaged }: { me: Me; engaged: boolean }) {91  const t = useT(); const { lang } = useLang();92  if (!engaged) return null;93  const known = me.emergency.emergency_stop;94  return (95    <div className="banner em-banner" role="alert">96      <IcoStop />97      <p><strong>{t('em.banner')}</strong> {t('em.banner_x')}98        {known && me.emergency.reason && <> {t('em.banner_reason')} <q>{me.emergency.reason}</q></>}99        {known && me.emergency.changed_at && <> · <time dateTime={me.emergency.changed_at}>{fmtTime(me.emergency.changed_at, lang)}</time></>}100      </p>101    </div>102  );103}104
Files main
Menu

Theme