SPB Git

Repository Public

Recipes.tsx 7.2 KB 121 lines tsx

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/Recipes.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 type { Recipe } from '../../types.ts';46import { ActionStatus, Await, Check, Empty, Field, Hash, JsonView, Modal, Panel, TableWrap, Tag, useAction, useLoad } from '../../ui/core.tsx';4748type VersionRow = { id: string; recipe_id: string; version: number; definition: unknown; sha256: string; status: string; origin: string; validated_at: string | null; created_at: string };4950export function Recipes() {51  const { t, dt } = usePrefs();52  const list = useLoad(() => get<{ recipes: Recipe[] }>('/v1/recipes'), []);53  const [view, setView] = useState<{ recipe: Recipe; versionId: string } | null>(null);54  const [draft, setDraft] = useState<{ recipe: Recipe; from: unknown } | null>(null);55  return (56    <>57      <p className="small">{t('rc.lead')}</p>58      <Await of={list}>{(d) => d.recipes.length === 0 ? <Empty>{t('rc.empty')}</Empty> : (<>59        {d.recipes.map((r) => (60          <Panel key={r.id} title={r.title} actions={<button type="button" onClick={() => setDraft({ recipe: r, from: null })}>{t('rc.newVersion')}</button>}>61            <p className="small"><code>{r.slug}</code> · {t('rc.purpose')} <code>{r.purpose}</code> · {t('pg.platform')} <code>{r.platform}</code></p>62            {!r.versions?.length ? <Empty>{t('rc.noVersion')}</Empty> : (63              <TableWrap caption={r.title}>64                <thead><tr><th scope="col">{t('c.version')}</th><th scope="col">{t('c.status')}</th><th scope="col">{t('rc.origin')}</th><th scope="col">{t('c.created')}</th><th scope="col">{t('rc.validatedAt')}</th><th scope="col">sha256</th><th scope="col">{t('c.actions')}</th></tr></thead>65                <tbody>{r.versions.map((v) => <tr key={v.id}><th scope="row">{v.version}</th><td><Tag prefix="rc.status" value={v.status} tone={v.status === 'validated' ? 'ok' : v.status === 'draft' ? 'hold' : 'idle'} /></td><td><code>{v.origin}</code></td><td>{dt(v.created_at)}</td><td>{v.validated_at ? dt(v.validated_at) : '—'}</td><td><Hash value={v.sha256} /></td><td><button type="button" className="small-btn" onClick={() => setView({ recipe: r, versionId: v.id })}>{t('rc.view')}</button></td></tr>)}</tbody>66              </TableWrap>67            )}68          </Panel>69        ))}70      </>)}</Await>71      {view ? <ViewVersion recipe={view.recipe} id={view.versionId} onClose={() => setView(null)} onChanged={list.reload} onDraftFrom={(def) => { setDraft({ recipe: view.recipe, from: def }); setView(null); }} /> : null}72      {draft ? <DraftVersion recipe={draft.recipe} from={draft.from} onClose={() => setDraft(null)} onDone={() => { setDraft(null); list.reload(); }} /> : null}73    </>74  );75}7677function ViewVersion({ recipe, id, onClose, onChanged, onDraftFrom }: { recipe: Recipe; id: string; onClose: () => void; onChanged: () => void; onDraftFrom: (def: unknown) => void }) {78  const { t } = usePrefs();79  const v = useLoad(() => get<VersionRow>(`/v1/recipe-versions/${id}`), [id]);80  const [attest, setAttest] = useState(false);81  const a = useAction();82  const validate = async () => { if (await a.run(() => post(`/v1/recipe-versions/${id}/validate`, { attest: true }), t('rc.validated'))) { v.reload(); onChanged(); } };83  return (84    <Modal wide title={`${recipe.title}`} onClose={onClose}>85      <Await of={v}>{(d) => (<>86        <p>{t('c.version')} {d.version} · <Tag prefix="rc.status" value={d.status} tone={d.status === 'validated' ? 'ok' : d.status === 'draft' ? 'hold' : 'idle'} /> · <Hash value={d.sha256} /></p>87        <JsonView value={d.definition} />88        <div className="row"><button type="button" onClick={() => onDraftFrom(d.definition)}>{t('rc.draftFrom')}</button></div>89        {d.status === 'draft' ? (90          <div className="form">91            <h3>{t('rc.validate')}</h3>92            <p>{t('rc.validateLead')}</p>93            <Check label={t('rc.attest')} checked={attest} onChange={setAttest} />94            <div className="row"><button type="button" className="primary" disabled={!attest || a.busy} onClick={validate}>{t('rc.validate')}</button></div>95          </div>96        ) : null}97        <ActionStatus a={a} />98      </>)}</Await>99    </Modal>100  );101}102103function DraftVersion({ recipe, from, onClose, onDone }: { recipe: Recipe; from: unknown; onClose: () => void; onDone: () => void }) {104  const { t } = usePrefs();105  const [text, setText] = useState(() => (from ? JSON.stringify(from, null, 2) : ''));106  const a = useAction();107  let parsed: unknown; let parseError = '';108  try { parsed = JSON.parse(text); } catch (e) { parseError = text.trim() ? (e as Error).message : ' '; }109  const send = async () => { if (await a.run(() => post(`/v1/recipes/${recipe.id}/versions`, { definition: parsed, origin: 'human' }))) onDone(); };110  return (111    <Modal wide title={`${t('rc.newVersion')} — ${recipe.title}`} onClose={onClose}>112      <p>{t('rc.draftLead', { slug: recipe.slug })}</p>113      <form className="form" onSubmit={(e) => { e.preventDefault(); if (!parseError) void send(); }}>114        <Field label={t('rc.definition')} hint={parseError.trim() ? `${t('po.jsonInvalid')} ${parseError}` : undefined} wide><textarea value={text} onChange={(e) => setText(e.target.value)} rows={20} className="mono" spellCheck={false} aria-invalid={!!parseError.trim()} required /></Field>115        <div className="row"><button type="submit" className="primary" disabled={!!parseError || a.busy}>{t('rc.saveDraft')}</button><button type="button" onClick={onClose}>{t('c.cancel')}</button></div>116        <ActionStatus a={a} />117      </form>118    </Modal>119  );120}121
Files main
Menu

Theme