SPB Git

Repository Public

Approvals.tsx 5.7 KB 110 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/Approvals.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 { brandQuery, useSession } from '../session.tsx';46import { Link } from '../router.tsx';47import type { PendingApproval } from '../types.ts';48import { ActionStatus, Await, Empty, Field, Hash, KV, Notice, PageHead, Tag, useAction, useLoad } from '../ui/core.tsx';49import { Preview, useMediaIndex } from '../ui/Preview.tsx';5051export function Approvals() {52  const { t } = usePrefs();53  const s = useSession();54  const list = useLoad(() => get<{ items: PendingApproval[] }>(`/v1/approvals/pending${brandQuery(s.brandId)}`), [s.brandId]);55  const [last, setLast] = useState<string | null>(null);56  return (57    <>58      <PageHead title={t('ap.title')} lead={t('ap.lead')}><button type="button" onClick={list.reload} disabled={list.loading}>{t('c.reload')}</button></PageHead>59      {!s.isAdmin ? <p className="small">{t('c.brandRoleHint')}</p> : null}60      <p role="status" aria-live="polite">{last ?? ''}</p>61      <Await of={list}>{(d) => d.items.length === 0 ? <Empty>{t('ap.empty')}</Empty> : (62        <ul className="plain stack">{d.items.map((it) => <Item key={`${it.id}:${it.version_id}`} it={it} onDecided={(msg) => { setLast(msg); list.reload(); }} reload={list.reload} />)}</ul>63      )}</Await>64    </>65  );66}6768function Item({ it, onDecided, reload }: { it: PendingApproval; onDecided: (msg: string) => void; reload: () => void }) {69  const { t, tl } = usePrefs();70  const s = useSession();71  const media = useMediaIndex(it.brand_id);72  const [comment, setComment] = useState('');73  const a = useAction();74  const mine = it.created_by === s.me.user.id || (it.author_kind === 'user' && it.author_id === s.me.user.id);75  // Exactly what is displayed is what is sent: version, hash and page of this preview.76  const decide = async (decision: 'approve' | 'reject') => {77    if (!it.page_id) return;78    const r = await a.run(() => post<{ state: string }>(`/v1/content/${it.id}/approve`, { versionId: it.version_id, contentHash: it.content_hash, pageId: it.page_id, decision, comment: comment.trim() }));79    if (r) onDecided(t(decision === 'approve' ? 'ap.doneApprove' : 'ap.doneReject', { title: it.title, state: tl('contentState', r.state) }));80  };81  const stale = (a.error as any)?.code === 'stale_approval';82  return (83    <li className="card approval">84      <div className="card-head"><h2><Link to={`/content/${it.id}`}>{it.title}</Link></h2><span className="small">{it.brand_name}</span></div>85      <div className="split">86        <Preview content={{ lang: it.lang, body: it.body, linkUrl: it.link_url, altText: it.alt_text, mediaSha256: it.media_sha256 }} media={media} pageName={it.page_name} hash={it.content_hash} version={it.version} />87        <div>88          <KV rows={[89            [t('c.page'), it.page_id ? <>{it.page_name} <Tag prefix="pageStatus" value={it.page_status} /></> : t('pv.noPage')],90            [t('c.version'), <>{it.version} · <Hash value={it.version_id} /></>],91            [t('ct.author'), it.author_kind === 'agent' ? `${t('ct.byAgent')} (${it.author_id})` : t('ct.byHuman')],92          ]} />93          {mine ? <Notice tone="warn"><p>{t('ap.mine')}</p></Notice> : null}94          {!it.page_id ? <Notice tone="warn"><p>{t('ap.noPage')}</p></Notice> : null}95          <form className="form" onSubmit={(e) => e.preventDefault()}>96            <Field label={`${t('ed.comment')} (${t('c.optional')})`} wide><textarea value={comment} onChange={(e) => setComment(e.target.value)} rows={2} maxLength={1000} /></Field>97            <p className="small">{t('ap.binding')}</p>98            <div className="row">99              <button type="button" className="primary" onClick={() => decide('approve')} disabled={a.busy || !it.page_id}>{t('ap.approve')}</button>100              <button type="button" onClick={() => decide('reject')} disabled={a.busy || !it.page_id}>{t('ap.reject')}</button>101              {stale ? <button type="button" onClick={reload}>{t('ap.reloadPreview')}</button> : null}102            </div>103            <ActionStatus a={a} />104          </form>105        </div>106      </div>107    </li>108  );109}110
Files main
Menu

Theme