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/Dashboard.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 { CONTENT_STATES, JOB_STATES, SESSION_STATES } from '../consts.ts';43import { get, post } from '../../../shared/api.ts';44import { usePrefs } from '../prefs.tsx';45import { useSession } from '../session.tsx';46import { Link } from '../router.tsx';47import type { Overview } from '../types.ts';48import { ActionStatus, Await, Empty, PageHead, Panel, TableWrap, Tag, useAction, useLoad } from '../ui/core.tsx';4950/** Counts grouped by the server; a state absent from the result has zero rows, which is a real count. */51function Counts({ rows, order, prefix, field, caption }: { rows: { n: number }[]; order: readonly string[]; prefix: string; field: 'state' | 'status'; caption: string }) {52 const { t, num } = usePrefs();53 const by = new Map(rows.map((r) => [String((r as any)[field]), r.n]));54 const keys = [...order, ...[...by.keys()].filter((k) => !order.includes(k))];55 return (56 <TableWrap caption={caption}>57 <thead><tr><th scope="col">{field === 'state' ? t('c.state') : t('c.status')}</th><th scope="col" className="num">{t('dash.col.count')}</th></tr></thead>58 <tbody>{keys.map((k) => <tr key={k}><th scope="row"><Tag prefix={prefix} value={k} /></th><td className="num">{num(by.get(k) ?? 0)}</td></tr>)}</tbody>59 </TableWrap>60 );61}6263export function Dashboard() {64 const { t, tl, dt, num } = usePrefs();65 const s = useSession();66 const ov = useLoad(() => get<Overview>('/v1/overview'), []);67 const ack = useAction();68 return (69 <>70 <PageHead title={t('dash.title')} lead={t('dash.lead')}><button type="button" onClick={ov.reload} disabled={ov.loading}>{t('c.reload')}</button></PageHead>71 <Await of={ov}>{(o) => (72 <>73 <p className="small" role="status">{t('dash.generated', { date: dt(o.generatedAt) })}</p>74 <div className="grid-2">75 <Panel title={t('dash.needsHuman')}>76 {o.needsHuman.length === 0 ? <Empty>{t('dash.needsHuman.empty')}</Empty> : (77 <ul className="ledger">{o.needsHuman.map((j) => <li key={j.id}><Link to={`/jobs/${j.id}`}>{j.title}</Link><Tag prefix="jobState" value={j.state} />{j.state_reason ? <span className="small">{j.state_reason}</span> : null}</li>)}</ul>78 )}79 </Panel>80 <Panel title={t('dash.pending')}>81 <p className="figure">{num(o.pendingApprovals)}</p>82 <p><Link to="/approvals">{t('dash.pending.go')}</Link></p>83 </Panel>84 </div>85 <Panel title={t('dash.upcoming')}>86 {o.upcoming.length === 0 ? <Empty>{t('dash.upcoming.empty')}</Empty> : (87 <TableWrap caption={t('dash.upcoming')}>88 <thead><tr><th scope="col">{t('dash.col.when')}</th><th scope="col">{t('c.title')}</th><th scope="col">{t('c.brand')}</th><th scope="col">{t('dash.col.mode')}</th><th scope="col">{t('c.state')}</th></tr></thead>89 <tbody>{o.upcoming.map((j) => <tr key={j.id}><td>{dt(j.scheduled_at, j.timezone)}</td><th scope="row"><Link to={`/jobs/${j.id}`}>{j.title}</Link></th><td>{j.brand_name}</td><td>{tl('mode', j.mode)}</td><td><Tag prefix="jobState" value={j.state} /></td></tr>)}</tbody>90 </TableWrap>91 )}92 </Panel>93 <div className="grid-2">94 <Panel title={t('dash.content')}><Counts rows={o.content} order={CONTENT_STATES} prefix="contentState" field="state" caption={t('dash.content')} /></Panel>95 <Panel title={t('dash.jobs')}><Counts rows={o.jobs} order={JOB_STATES} prefix="jobState" field="state" caption={t('dash.jobs')} /></Panel>96 <Panel title={t('dash.comments')}><Counts rows={o.comments} order={['open', 'human_required', 'drafted', 'closed']} prefix="commentStatus" field="status" caption={t('dash.comments')} /></Panel>97 <Panel title={t('dash.sessions')}>98 {o.sessions === null ? <Empty>{t('dash.adminOnly')}</Empty> : o.sessions.length === 0 ? <Empty>{t('dash.sessions.empty')}</Empty> : <Counts rows={o.sessions} order={SESSION_STATES.filter((x) => o.sessions!.some((r) => r.state === x))} prefix="sessState" field="state" caption={t('dash.sessions')} />}99 </Panel>100 <Panel title={t('dash.workers')}>101 {o.workers === null ? <Empty>{t('dash.adminOnly')}</Empty> : o.workers.length === 0 ? <Empty>{t('dash.workers.empty')}</Empty> : <Counts rows={o.workers} order={['online', 'offline', 'disabled']} prefix="workerStatus" field="status" caption={t('dash.workers')} />}102 </Panel>103 </div>104 <Panel title={t('dash.alerts')}>105 {o.alerts.length === 0 ? <Empty>{t('dash.alerts.empty')}</Empty> : (106 <TableWrap caption={t('dash.alerts')}>107 <thead><tr><th scope="col">{t('dash.col.severity')}</th><th scope="col">{t('dash.col.kind')}</th><th scope="col">{t('dash.col.message')}</th><th scope="col">{t('c.date')}</th>{s.isAdmin ? <th scope="col">{t('c.actions')}</th> : null}</tr></thead>108 <tbody>{o.alerts.map((al) => (109 <tr key={al.id}><td><Tag prefix="sev" value={al.severity} /></td><td><code>{al.kind}</code></td><td>{al.message}</td><td>{dt(al.created_at)}</td>110 {s.isAdmin ? <td><button type="button" className="small-btn" disabled={ack.busy} onClick={async () => { if (await ack.run(() => post(`/v1/alerts/${al.id}/ack`))) ov.reload(); }}>{t('dash.alerts.ack')}</button></td> : null}</tr>111 ))}</tbody>112 </TableWrap>113 )}114 <ActionStatus a={ack} />115 </Panel>116 </>117 )}</Await>118 </>119 );120}121