SPB Git

Repository Public

Icons.tsx 4.7 KB 94 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : KA Maps Engine8 * File             : studio/web/src/Icons.tsx9 * Module           : studio10 * Repository       : https://git.spboucher.ai/ka-maps-engine11 * Created          : 2026-12-0112 * Last Modified    : 2026-12-0113 *14 * Author           : Simon-Pierre Boucher15 * Position         : Professor of Finance16 * Institution      : Université du Québec en Outaouais (UQO)17 * Organization     : SP Boucher18 * Website          : https://spboucher.ai19 * Git              : https://git.spboucher.ai20 *21 * Preferred Contact: support@spboucher.ai22 * Development      : dev@spboucher.ai23 * Support          : support@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 { useRef, useState } from 'react';43import { api, fmtBytes, fmtDate } from './api.ts';44import type { Meta } from './main.tsx';45import { Notice, useAsync } from './ui.tsx';4647export function Icons({ meta }: { meta: Meta }) {48  const { data, reload } = useAsync(() => api.get('/api/icons'), []);49  const [name, setName] = useState('');50  const [msg, setMsg] = useState<{ kind: 'ok' | 'bad'; text: string } | null>(null);51  const file = useRef<HTMLInputElement>(null);52  const replaceRef = useRef<HTMLInputElement>(null);53  const [target, setTarget] = useState('');54  const upload = async (n: string, f: File | undefined) => {55    if (!f) return;56    setMsg(null);57    try {58      const r = await api.put(`/api/icons/${n}`, { svg: await f.text() });59      setMsg({ kind: 'ok', text: r.created ? `Icône « ${n} » ajoutée.` : `Icône « ${n} » remplacée.` });60      setName('');61      reload();62    } catch (e: any) {63      setMsg({ kind: 'bad', text: e.message });64    }65  };66  return (67    <>68      <div className="head"><div><h1>Icônes</h1>69        <p className="muted">Bibliothèque SVG servie aux applications par <code>{meta.publicMaps}/v1/icons/</code>. Formes et couleurs seulement : tout script, lien ou ressource externe est refusé. Les icônes dessinées <em>par le moteur</em> sur la carte restent les formes du format kastyle (point, carré, anneau, cartouche) ; cette bibliothèque sert les interfaces des produits.</p></div></div>70      {msg ? <Notice kind={msg.kind}>{msg.text}</Notice> : null}71      <div className="panel row" style={{ marginBottom: 14 }}>72        <label className="f">Nom (minuscules, chiffres, tirets)<input type="text" value={name} onChange={(e) => setName(e.target.value.toLowerCase())} placeholder="ex. metro" /></label>73        <label className="f">Fichier SVG (64 ko au plus)<input type="file" accept=".svg,image/svg+xml" ref={file} /></label>74        <button className="btn primary" style={{ alignSelf: 'flex-end' }} disabled={!/^[a-z][a-z0-9-]{0,40}$/.test(name)} onClick={() => upload(name, file.current?.files?.[0])}>Ajouter</button>75      </div>76      <input type="file" accept=".svg,image/svg+xml" ref={replaceRef} hidden onChange={(e) => { upload(target, e.target.files?.[0]); e.target.value = ''; }} />77      <div className="icons">78        {data?.icons.map((i: any) => (79          <div className="ic" key={i.name}>80            <img alt={`Icône ${i.name}`} src={`data:image/svg+xml;utf8,${encodeURIComponent(i.svg)}`} />81            <strong className="mono">{i.name}</strong>82            <div className="small faint">{fmtBytes(i.bytes)} · {fmtDate(i.updated_at)}<br />{i.updated_by}</div>83            <div className="row" style={{ justifyContent: 'center', marginTop: 8 }}>84              <button className="btn small" onClick={() => { setTarget(i.name); replaceRef.current?.click(); }}>Remplacer</button>85              <button className="btn small danger" onClick={async () => { if (confirm(`Supprimer l'icône « ${i.name} » ?`)) { await api.del(`/api/icons/${i.name}`); reload(); } }}>Supprimer</button>86            </div>87          </div>88        ))}89      </div>90      {data && !data.icons.length ? <p className="muted">Aucune icône pour l'instant.</p> : null}91    </>92  );93}94
Files main
Menu

Theme