SPB Git

Repository Public

Workers.tsx 5.9 KB 93 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/Workers.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 type { Worker } from '../types.ts';47import { ActionStatus, Await, CopyButton, Empty, Field, Notice, PageHead, Panel, TableWrap, Tag, useAction, useLoad } from '../ui/core.tsx';4849export function Workers() {50  const { t, dt, num } = usePrefs();51  const s = useSession();52  const list = useLoad(() => get<{ workers: Worker[] }>('/v1/workers'), []);53  const [f, setF] = useState({ name: '', host: '' });54  const [created, setCreated] = useState<{ id: string; name: string; token: string } | null>(null);55  const [disabling, setDisabling] = useState('');56  const a = useAction();57  const create = async () => { const r = await a.run(() => post<{ id: string; name: string; token: string }>('/v1/workers', { name: f.name, host: f.host })); if (r) { setCreated(r); setF({ name: '', host: '' }); list.reload(); } };58  const disable = async (id: string) => { setDisabling(''); if (await a.run(() => post(`/v1/workers/${id}/disable`), t('c.saved'))) list.reload(); };59  const bool = (v: boolean | undefined) => (v === undefined ? t('wk.notReported') : v ? t('c.yes') : t('c.no'));60  return (61    <>62      <PageHead title={t('wk.title')} lead={t('wk.lead')}><button type="button" onClick={list.reload} disabled={list.loading}>{t('c.reload')}</button></PageHead>63      {created ? (64        <Notice tone="warn">65          <p><strong>{t('wk.tokenOnce', { name: created.name })}</strong></p>66          <p>{t('wk.tokenWarn')}</p>67          <p><code className="token">{created.token}</code></p>68          <p className="row"><CopyButton text={created.token} /><button type="button" className="small-btn" onClick={() => setCreated(null)}>{t('wk.tokenHide')}</button></p>69        </Notice>70      ) : null}71      <Await of={list}>{(d) => d.workers.length === 0 ? <Empty>{t('wk.empty')}</Empty> : (72        <TableWrap caption={t('wk.title')}>73          <thead><tr><th scope="col">{t('c.name')}</th><th scope="col">{t('wk.host')}</th><th scope="col">{t('c.status')}</th><th scope="col">{t('wk.heartbeat')}</th><th scope="col">{t('c.version')}</th><th scope="col">{t('wk.headed')}</th><th scope="col" className="num">{t('wk.disk')}</th><th scope="col">{t('wk.ax')}</th><th scope="col">{t('wk.screen')}</th>{s.isOwner ? <th scope="col">{t('c.actions')}</th> : null}</tr></thead>74          <tbody>{d.workers.map((w) => { const i = w.info ?? {}; return (75            <tr key={w.id}><th scope="row">{w.name}</th><td>{w.host || '—'}</td><td><Tag prefix="workerStatus" value={w.status} /></td><td>{w.last_heartbeat_at ? dt(w.last_heartbeat_at) : t('c.never')}</td><td>{i.version ? <code>{i.version}</code> : t('wk.notReported')}</td><td>{bool(i.headed)}</td><td className="num">{typeof i.freeDiskGb === 'number' ? `${num(i.freeDiskGb, { maximumFractionDigits: 1 })} Go` : t('wk.notReported')}</td><td>{bool(i.axPermission)}</td><td>{bool(i.screenPermission)}</td>76              {s.isOwner ? <td>{w.status === 'disabled' ? '—' : disabling === w.id ? <span className="row"><button type="button" className="small-btn danger" disabled={a.busy} onClick={() => disable(w.id)}>{t('c.confirm')}</button><button type="button" className="small-btn" onClick={() => setDisabling('')}>{t('c.cancel')}</button></span> : <button type="button" className="small-btn" onClick={() => setDisabling(w.id)}>{t('wk.disable')}</button>}</td> : null}</tr>77          ); })}</tbody>78        </TableWrap>79      )}</Await>80      <ActionStatus a={a} />81      <Panel title={t('wk.new')}>82        {!s.isOwner ? <p className="small">{t('c.roleNeeded', { role: t('role.owner') })}</p> : (83          <form className="form inline-form" onSubmit={(e) => { e.preventDefault(); void create(); }}>84            <Field label={t('c.name')} hint={t('wk.nameHint')}><input value={f.name} onChange={(e) => setF({ ...f, name: e.target.value })} pattern="[A-Za-z0-9._-]{2,40}" className="mono" required /></Field>85            <Field label={`${t('wk.host')} (${t('c.optional')})`}><input value={f.host} onChange={(e) => setF({ ...f, host: e.target.value })} maxLength={80} /></Field>86            <button type="submit" className="primary" disabled={a.busy || !/^[A-Za-z0-9._-]{2,40}$/.test(f.name)}>{t('wk.create')}</button>87          </form>88        )}89      </Panel>90    </>91  );92}93
Files main
Menu

Theme