SPB Git

Repository Public

ui.tsx 4.7 KB 101 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Social Engine & Remote Navigator8 * File             : apps/navigator-web/src/ui.tsx9 * Module           : navigator-web10 * 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 */4142// Small shared pieces: error display, state badge, icon button, polling hook.43import { useEffect, useRef, type ButtonHTMLAttributes, type ReactNode } from 'react';44import { ApiError } from '../../shared/api.ts';45import { tCode, useT } from './i18n.ts';46import { StateGlyph } from './icons.tsx';47import type { SessionState } from './types.ts';4849/** Every failure is shown with its code and, when the API gave one, its correlation id. */50export function ErrorLine({ error, onDismiss }: { error: unknown; onDismiss?: () => void }) {51  const t = useT();52  if (!error) return null;53  const api = error instanceof ApiError ? error : null;54  const message = api ? api.message : error instanceof Error ? error.message : String(error);55  return (56    <p className="err" role="alert">57      <strong>{t('err.label')}</strong> <span>{message}</span>58      {api && <> <code>{api.code} · HTTP {api.status}</code></>}59      {api?.correlationId && <> <code>{api.correlationId}</code></>}60      {!api && error instanceof Error && error.name && error.name !== 'Error' && <> <code>{error.name}</code></>}61      {onDismiss && <> <button type="button" className="link" onClick={onDismiss}>{t('common.dismiss')}</button></>}62    </p>63  );64}6566export function StateBadge({ state, explain = false }: { state: SessionState; explain?: boolean }) {67  const t = useT();68  return (69    <span className={`state state-${state}`}>70      <span className="state-tag"><StateGlyph state={state} /><span>{tCode(t, 'state', state)}</span></span>71      {explain && <span className="state-x">{tCode(t, 'statex', state)}</span>}72    </span>73  );74}7576type IconBtnProps = ButtonHTMLAttributes<HTMLButtonElement> & { label: string; icon: ReactNode; text?: ReactNode; pressed?: boolean };77/** Icon button: the accessible name is always the full label, also shown as a tooltip. */78export function IconBtn({ label, icon, text, pressed, className, ...rest }: IconBtnProps) {79  return <button type="button" {...rest} className={`ibtn${className ? ` ${className}` : ''}`} aria-label={label} title={label} aria-pressed={pressed}>{icon}{text !== undefined && <span className="ibtn-text">{text}</span>}</button>;80}8182/** Runs `fn` now, then every `ms` while the tab is visible, and again as soon as the tab becomes visible. */83export function usePoll(fn: () => void | Promise<void>, ms: number, enabled = true) {84  const ref = useRef(fn); ref.current = fn;85  useEffect(() => {86    if (!enabled) return;87    const tick = () => { if (!document.hidden) void ref.current(); };88    tick();89    const id = window.setInterval(tick, ms);90    document.addEventListener('visibilitychange', tick);91    return () => { window.clearInterval(id); document.removeEventListener('visibilitychange', tick); };92  }, [ms, enabled]);93}9495export function fmtTime(iso: string | null | undefined, lang: string): string {96  if (!iso) return '—';97  const d = new Date(iso); if (Number.isNaN(d.getTime())) return '—';98  return new Intl.DateTimeFormat(lang === 'fr' ? 'fr-CA' : 'en-CA', { timeZone: 'America/Toronto', dateStyle: 'medium', timeStyle: 'short' }).format(d);99}100export const shortId = (id: string) => id.slice(0, 8);101
Files main
Menu

Theme