Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Social Engine & Remote Navigator8 * File : apps/social-web/src/screens/settings/Policy.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 */4142import { useState } from 'react';43import { get, post } from '../../../../shared/api.ts';44import { usePrefs } from '../../prefs.tsx';45import { useSession } from '../../session.tsx';46import { ActionStatus, Await, Field, Hash, JsonView, Panel, TableWrap, Tag, useAction, useLoad } from '../../ui/core.tsx';4748type PolicyRes = { active: Record<string, unknown> & { _version: number }; versions: { version: number; sha256: string; note: string; created_by: string | null; created_at: string; active: boolean }[] };4950export function Policy() {51 const { t, dt } = usePrefs();52 const s = useSession();53 const p = useLoad(() => get<PolicyRes>('/v1/policy'), []);54 return (55 <Await of={p}>{(d) => { const { _version, ...doc } = d.active; return (<>56 <Panel title={t('po.active', { v: _version })}>57 <p className="small">{t('po.readonly')}</p>58 <JsonView value={doc} />59 </Panel>60 <Panel title={t('po.history')}>61 <TableWrap caption={t('po.history')}>62 <thead><tr><th scope="col">{t('c.version')}</th><th scope="col">{t('c.date')}</th><th scope="col">{t('po.note')}</th><th scope="col">sha256</th><th scope="col">{t('c.state')}</th></tr></thead>63 <tbody>{d.versions.map((v) => <tr key={v.version}><th scope="row">{v.version}</th><td>{dt(v.created_at)}</td><td className="pre">{v.note || '—'}</td><td><Hash value={v.sha256} /></td><td>{v.active ? <Tag prefix="po.state" value="active" tone="ok" /> : <Tag prefix="po.state" value="past" tone="idle" />}</td></tr>)}</tbody>64 </TableWrap>65 </Panel>66 {s.isOwner ? <Submit doc={doc} onDone={() => { p.reload(); void s.reloadMe(); }} /> : <p className="small">{t('po.ownerOnly')}</p>}67 </>); }}</Await>68 );69}7071function Submit({ doc, onDone }: { doc: unknown; onDone: () => void }) {72 const { t } = usePrefs();73 const [text, setText] = useState(() => JSON.stringify(doc, null, 2));74 const [note, setNote] = useState('');75 const [armed, setArmed] = useState(false);76 const a = useAction();77 let parsed: unknown; let parseError = '';78 try { parsed = JSON.parse(text); } catch (e) { parseError = (e as Error).message; }79 const unchanged = !parseError && JSON.stringify(parsed) === JSON.stringify(doc);80 const ready = !parseError && note.trim().length >= 10 && !unchanged;81 const send = async () => { setArmed(false); const r = await a.run(() => post<{ version: number }>('/v1/policy', { document: parsed, note: note.trim() })); if (r) { setNote(''); onDone(); } };82 return (83 <Panel title={t('po.submit')}>84 <p>{t('po.submitLead')}</p>85 <form className="form" onSubmit={(e) => { e.preventDefault(); if (ready) setArmed(true); }}>86 <Field label={t('po.document')} hint={parseError ? `${t('po.jsonInvalid')} ${parseError}` : unchanged ? t('po.unchanged') : t('po.jsonOk')} wide><textarea value={text} onChange={(e) => { setText(e.target.value); setArmed(false); }} rows={18} className="mono" spellCheck={false} aria-invalid={!!parseError} /></Field>87 <Field label={t('po.noteLabel')} wide><textarea value={note} onChange={(e) => setNote(e.target.value)} rows={3} maxLength={2000} required /></Field>88 {armed ? <div className="row"><button type="button" className="danger" onClick={send} disabled={a.busy}>{t('po.confirm')}</button><button type="button" onClick={() => setArmed(false)}>{t('c.cancel')}</button></div>89 : <div className="row"><button type="submit" disabled={!ready || a.busy}>{t('po.activate')}</button></div>}90 <ActionStatus a={a} />91 </form>92 </Panel>93 );94}95