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/Sources.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 { api, get, post } from '../../../../shared/api.ts';44import { usePrefs } from '../../prefs.tsx';45import { brandQuery, useSession } from '../../session.tsx';46import type { AgentRunResult, GitSource, SourceEvent } from '../../types.ts';47import { ActionStatus, Await, Empty, Field, JsonView, Panel, TableWrap, Tag, useAction, useLoad } from '../../ui/core.tsx';48import { RunResult } from './Agents.tsx';4950const KINDS = ['manual_campaign', 'calendar', 'product_data'] as const;5152export function Sources() {53 const { t, dt } = usePrefs();54 const s = useSession();55 const git = useLoad(() => get<{ sources: GitSource[] }>(`/v1/git-sources${brandQuery(s.brandId)}`), [s.brandId]);56 const events = useLoad(() => get<{ events: SourceEvent[] }>(`/v1/source-events${brandQuery(s.brandId)}`), [s.brandId]);57 const [g, setG] = useState({ brandId: s.brandId, repo: '' });58 const [m, setM] = useState({ brandId: s.brandId, kind: 'manual_campaign' as (typeof KINDS)[number], source: '', text: '' });59 const [res, setRes] = useState<AgentRunResult | null>(null);60 const ga = useAction(); const ea = useAction(); const ma = useAction();61 const brandSelect = (value: string, on: (v: string) => void) => <select value={value} onChange={(e) => on(e.target.value)} required><option value="">{t('c.select')}</option>{s.brands.map((b) => <option key={b.id} value={b.id}>{b.name}</option>)}</select>;62 const addGit = async () => { if (await ga.run(() => post('/v1/git-sources', { brandId: g.brandId, repo: g.repo.trim() }), t('c.saved'))) { setG({ ...g, repo: '' }); git.reload(); } };63 const delGit = async (id: string) => { if (await ga.run(() => api('DELETE', `/v1/git-sources/${id}`), t('c.saved'))) git.reload(); };64 const mark = async (id: string, decision: 'public_ok' | 'ignore') => { if (await ea.run(() => post(`/v1/source-events/${id}/mark`, { decision }), t('c.saved'))) events.reload(); };65 const copy = async (e: SourceEvent) => { setRes(null); const r = await ea.run(() => post<AgentRunResult>('/v1/agents/run', { agent: 'copywriter', brandId: e.brand_id, sourceEventId: e.id })); if (r) { setRes(r); events.reload(); } };66 const addManual = async () => { if (await ma.run(() => post('/v1/source-events', { brandId: m.brandId, kind: m.kind, source: m.source.trim(), text: m.text.trim() }), t('c.saved'))) { setM({ ...m, source: '', text: '' }); events.reload(); } };67 return (68 <>69 <Panel title={t('so.git')}>70 <p className="small">{t('so.gitLead')}</p>71 <Await of={git}>{(d) => d.sources.length === 0 ? <Empty>{t('so.gitEmpty')}</Empty> : (72 <TableWrap caption={t('so.git')}>73 <thead><tr><th scope="col">{t('so.repo')}</th><th scope="col">{t('c.brand')}</th><th scope="col">{t('so.enabled')}</th><th scope="col">{t('so.polled')}</th><th scope="col">{t('so.lastError')}</th>{s.isAdmin ? <th scope="col">{t('c.actions')}</th> : null}</tr></thead>74 <tbody>{d.sources.map((x) => <tr key={x.id}><th scope="row"><code>{x.repo}</code></th><td>{s.brandById(x.brand_id)?.name ?? '—'}</td><td>{x.enabled ? t('c.yes') : t('c.no')}</td><td>{x.last_polled_at ? dt(x.last_polled_at) : t('c.never')}</td><td>{x.last_error ? <code>{x.last_error}</code> : '—'}</td>{s.isAdmin ? <td><button type="button" className="small-btn" disabled={ga.busy} onClick={() => delGit(x.id)}>{t('so.detach')}</button></td> : null}</tr>)}</tbody>75 </TableWrap>76 )}</Await>77 {s.isAdmin ? (78 <form className="form inline-form" onSubmit={(e) => { e.preventDefault(); void addGit(); }}>79 <Field label={t('c.brand')}>{brandSelect(g.brandId, (v) => setG({ ...g, brandId: v }))}</Field>80 <Field label={t('so.repo')} hint={t('so.repoHint')}><input value={g.repo} onChange={(e) => setG({ ...g, repo: e.target.value })} pattern="[A-Za-z0-9._-]{1,80}" className="mono" required /></Field>81 <button type="submit" disabled={ga.busy}>{t('so.attach')}</button>82 </form>83 ) : null}84 <ActionStatus a={ga} />85 </Panel>86 <Panel title={t('so.events')} actions={<button type="button" onClick={events.reload} disabled={events.loading}>{t('c.reload')}</button>}>87 <p className="small">{t('so.eventsLead')}</p>88 <ActionStatus a={ea} />89 {res ? <RunResult r={res} /> : null}90 <Await of={events}>{(d) => d.events.length === 0 ? <Empty>{t('so.eventsEmpty')}</Empty> : (91 <TableWrap caption={t('so.events')}>92 <thead><tr><th scope="col">{t('c.date')}</th><th scope="col">{t('so.kind')}</th><th scope="col">{t('c.source')}</th><th scope="col">{t('c.brand')}</th><th scope="col">{t('c.status')}</th><th scope="col">{t('so.public')}</th><th scope="col">{t('so.payload')}</th><th scope="col">{t('c.actions')}</th></tr></thead>93 <tbody>{d.events.map((e) => (94 <tr key={e.id}><td>{dt(e.created_at)}</td><td><code>{e.kind}</code></td><th scope="row">{e.source}</th><td>{s.brandById(e.brand_id)?.name ?? '—'}</td><td><Tag prefix="so.status" value={e.status} tone={e.status === 'ignored' ? 'idle' : e.status === 'new' ? 'hold' : 'ok'} /></td><td><Tag prefix="so.pub" value={e.public_ok ? 'yes' : 'no'} tone={e.public_ok ? 'ok' : 'hold'} /></td>95 <td><details><summary>{t('c.details')}</summary><JsonView value={e.payload} /></details></td>96 <td><span className="row">97 {!e.public_ok && e.status !== 'ignored' ? <button type="button" className="small-btn" disabled={ea.busy} onClick={() => mark(e.id, 'public_ok')}>{t('so.markPublic')}</button> : null}98 {e.status !== 'ignored' ? <button type="button" className="small-btn" disabled={ea.busy} onClick={() => mark(e.id, 'ignore')}>{t('so.ignore')}</button> : null}99 {e.public_ok && e.status !== 'ignored' ? <button type="button" className="small-btn" disabled={ea.busy} onClick={() => copy(e)}>{t('so.copywriter')}</button> : null}100 </span></td></tr>101 ))}</tbody>102 </TableWrap>103 )}</Await>104 </Panel>105 <Panel title={t('so.manual')}>106 <p className="small">{t('so.manualLead')}</p>107 <form className="form" onSubmit={(e) => { e.preventDefault(); void addManual(); }}>108 <div className="form-grid">109 <Field label={t('c.brand')}>{brandSelect(m.brandId, (v) => setM({ ...m, brandId: v }))}</Field>110 <Field label={t('so.kind')}><select value={m.kind} onChange={(e) => setM({ ...m, kind: e.target.value as (typeof KINDS)[number] })}>{KINDS.map((k) => <option key={k} value={k}>{t(`so.kind.${k}`)}</option>)}</select></Field>111 <Field label={t('c.source')} hint={t('so.sourceHint')} wide><input value={m.source} onChange={(e) => setM({ ...m, source: e.target.value })} maxLength={200} required /></Field>112 <Field label={t('so.text')} wide><textarea value={m.text} onChange={(e) => setM({ ...m, text: e.target.value })} rows={4} maxLength={4000} required /></Field>113 </div>114 <div className="row"><button type="submit" disabled={ma.busy || m.source.trim().length < 2 || m.text.trim().length < 5}>{t('so.manualGo')}</button></div>115 <ActionStatus a={ma} />116 </form>117 </Panel>118 </>119 );120}121