Repository Public
1import { createContext, useContext, useEffect, useRef, useState, useSyncExternalStore, type ReactNode } from "react"2import type { CallManager } from "../core/calls"3import type { Engine } from "../core/engine"45// ---------- language (French or English, from the browser) ----------6const fr = (navigator.language || "en").toLowerCase().startsWith("fr")7export const t = (en: string, french: string) => (fr ? french : en)8export const locale = fr ? "fr-CA" : "en-CA"910// ---------- icons (original, 24px stroke set) ----------11const S = ({ children, fill }: { children: ReactNode; fill?: boolean }) => (12 <svg viewBox="0 0 24 24" fill={fill ? "currentColor" : "none"} stroke={fill ? "none" : "currentColor"} strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">13 {children}14 </svg>15)16export const I = {17 chat: <S><path d="M5 5h10a4 4 0 0 1 4 4v3a4 4 0 0 1-4 4h-4l-4 3v-3H5a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2z" /></S>,18 phone: <S><path d="M6.5 3.5h2.6l1.4 4-2 1.5a12 12 0 0 0 6.5 6.5l1.5-2 4 1.4v2.6a2 2 0 0 1-2.2 2A16.5 16.5 0 0 1 4.5 5.700a2 2 0 0 1 2-2.2z" /></S>,19 video: <S><rect x="3" y="6" width="12" height="12" rx="3.5" /><path d="M15 10.5 21 7v10l-6-3.500" /></S>,20 users: <S><circle cx="9" cy="8.500" r="3.500" /><path d="M2.5 19.500a6.500 6.500 0 0 1 13 0" /><path d="M16 5.200a3.500 3.500 0 0 1 0 6.600M18 14.500a6.300 6.300 0 0 1 3.500 5" /></S>,21 gear: <S><circle cx="12" cy="12" r="3.200" /><path d="M12 2.800v2.400M12 18.800v2.400M2.800 12h2.400M18.800 12h2.400M5.500 5.500l1.700 1.700M16.800 16.800l1.700 1.700M5.500 18.500l1.700-1.700M16.800 7.200l1.700-1.700" /></S>,22 shield: <S><path d="M12 3 4.500 6v5.500c0 4.600 3.100 8 7.500 9.500 4.400-1.500 7.500-4.900 7.500-9.500V6z" /><path d="m9 12 2.200 2.200L15.200 10" /></S>,23 lock: <S><rect x="5" y="10.500" width="14" height="10" rx="3" /><path d="M8 10.500V8a4 4 0 0 1 8 0v2.500" /></S>,24 search: <S><circle cx="11" cy="11" r="6.500" /><path d="m16 16 4.500 4.500" /></S>,25 plus: <S><path d="M12 5v14M5 12h14" /></S>,26 x: <S><path d="m6 6 12 12M18 6 6 18" /></S>,27 back: <S><path d="M14.500 5.500 8 12l6.500 6.500" /></S>,28 send: <S><path d="M12 19V5M5.500 11.500 12 5l6.500 6.500" /></S>,29 clip: <S><path d="m20 11.500-7.800 7.800a5 5 0 0 1-7-7l8.300-8.300a3.300 3.300 0 0 1 4.700 4.700l-8.300 8.300a1.700 1.700 0 0 1-2.400-2.400l7.600-7.600" /></S>,30 mic: <S><rect x="9" y="3" width="6" height="11" rx="3" /><path d="M5.500 11.500a6.500 6.500 0 0 0 13 0M12 18v3" /></S>,31 micOff: <S><path d="M9 9V6a3 3 0 0 1 5.700-1.300M15 10.500V11a3 3 0 0 1-4.600 2.500M5.500 11.500a6.500 6.500 0 0 0 10.300 5.300M18.500 11.500a6.400 6.400 0 0 1-.6 2.700M12 18v3M4 4l16 16" /></S>,32 camOff: <S><path d="M3 8.500V15a3.500 3.500 0 0 0 3.500 3.500H13M9 6h3.500A3.500 3.500 0 0 1 15 8.500v2l6-3.500v10M4 4l16 16" /></S>,33 camera: <S><path d="M4 8.500A2.500 2.500 0 0 1 6.500 6H8l1.500-2h5L16 6h1.500A2.500 2.500 0 0 1 20 8.500v8a2.500 2.500 0 0 1-2.500 2.500h-11A2.500 2.500 0 0 1 4 16.500z" /><circle cx="12" cy="12.500" r="3.300" /></S>,34 screen: <S><rect x="3" y="4.500" width="18" height="12" rx="2.500" /><path d="M8.500 20h7M12 16.500V20" /></S>,35 flip: <S><path d="M4 9a8 8 0 0 1 14.500-2.500M20 15a8 8 0 0 1-14.500 2.500" /><path d="M19 3v4h-4M5 21v-4h4" /></S>,36 hang: <S><path d="M3.500 14.500c0-1 .3-2 1-2.700a11 11 0 0 1 15 0c.7.700 1 1.700 1 2.700l-.3 1.300a1.300 1.300 0 0 1-1.600.900l-2.700-.800a1.300 1.300 0 0 1-.900-1.200v-1.400a9 9 0 0 0-6 0v1.400a1.300 1.300 0 0 1-.900 1.200l-2.700.800a1.300 1.300 0 0 1-1.600-.900z" /></S>,37 reply: <S><path d="M9.500 6 4 11.500 9.500 17" /><path d="M4 11.500h9a7 7 0 0 1 7 7V19" /></S>,38 forward: <S><path d="M14.500 6 20 11.500 14.500 17" /><path d="M20 11.500h-9a7 7 0 0 0-7 7V19" /></S>,39 edit: <S><path d="m4 20 1-4.500L16.500 4a2.100 2.100 0 0 1 3 3L8 18.500z" /></S>,40 trash: <S><path d="M4.500 7h15M9.500 7V4.500h5V7M6.500 7l.8 12a1.500 1.500 0 0 0 1.500 1.500h6.400a1.500 1.500 0 0 0 1.500-1.500l.8-12" /></S>,41 pin: <S><path d="M9 3.500h6l-1 6 3.500 3.500h-11L10 9.500zM12 13v7.500" /></S>,42 copy: <S><rect x="8.500" y="8.500" width="11" height="11" rx="2.500" /><path d="M5.500 15.500A2 2 0 0 1 4.500 14V6.500a2 2 0 0 1 2-2H14a2 2 0 0 1 1.500 1" /></S>,43 bookmark: <S><path d="M6.500 4h11v16.500L12 16.500l-5.500 4z" /></S>,44 smile: <S><circle cx="12" cy="12" r="8.500" /><path d="M8.500 14a4.300 4.300 0 0 0 7 0M9 9.800v.2M15 9.800v.2" /></S>,45 info: <S><circle cx="12" cy="12" r="8.500" /><path d="M12 11v5.500M12 7.800V8" /></S>,46 check: <S><path d="m5 12.500 4.500 4.500L19 7.500" /></S>,47 checks: <S><path d="m2.500 12.500 4 4L15 8M12 15.500l1 1L21.500 8" /></S>,48 clock: <S><circle cx="12" cy="12" r="8.500" /><path d="M12 7.500V12l3 2" /></S>,49 file: <S><path d="M7 3.500h6.500L18 8v11a1.500 1.500 0 0 1-1.500 1.500h-9A1.500 1.500 0 0 1 6 19V5a1.500 1.500 0 0 1 1-1.500z" /><path d="M13.500 3.500V8H18" /></S>,50 download: <S><path d="M12 4v11M7 10.500l5 5 5-5M5 20h14" /></S>,51 play: <S fill><path d="M8 5.500v13a1 1 0 0 0 1.500.900l10.500-6.500a1 1 0 0 0 0-1.800L9.500 4.600A1 1 0 0 0 8 5.500z" /></S>,52 pause: <S fill><rect x="6.500" y="5" width="4" height="14" rx="1.300" /><rect x="13.500" y="5" width="4" height="14" rx="1.300" /></S>,53 warn: <S><path d="M12 4 2.800 19.500h18.400z" /><path d="M12 10v4.500M12 17v.2" /></S>,54 key: <S><circle cx="8" cy="15" r="4" /><path d="m11 12 8.500-8.500M16 7l2.500 2.500M13.500 9.500l2 2" /></S>,55 device: <S><rect x="7" y="2.500" width="10" height="19" rx="2.800" /><path d="M11 18.500h2" /></S>,56 timer: <S><circle cx="12" cy="13.500" r="7.500" /><path d="M12 9.500v4l2.500 1.500M9.500 3h5" /></S>,57 bell: <S><path d="M6 16.500V11a6 6 0 0 1 12 0v5.500l1.500 2h-15zM10 20.500a2.200 2.200 0 0 0 4 0" /></S>,58 bellOff: <S><path d="M6 16.500V11a6 6 0 0 1 .8-3M9 5.800A6 6 0 0 1 18 11v4M4.500 18.500H16M10 20.500a2.200 2.200 0 0 0 4 0M4 4l16 16" /></S>,59 tasks: <S><path d="m4 6.500 1.800 1.800L9 5M4 13.500l1.800 1.800L9 12M12.500 7h7.500M12.500 14h7.500M4.500 19.500h.2M12.500 19.500h7.500" /></S>,60 note: <S><path d="M5 4.500h14v10l-5 5H5z" /><path d="M14 19.500v-5h5M8.500 9h7M8.500 12.500h4" /></S>,61 image: <S><rect x="3.500" y="4.500" width="17" height="15" rx="3" /><circle cx="9" cy="10" r="1.700" /><path d="m4.500 17.500 5-4.500 3.500 3 3-2.500 4 3.500" /></S>,62 logout: <S><path d="M10 4.500H6.500A2 2 0 0 0 4.500 6.500v11a2 2 0 0 0 2 2H10M15.500 8l4 4-4 4M19.500 12H9.500" /></S>,63 link: <S><path d="M10 14a4 4 0 0 0 5.700 0l3-3a4 4 0 0 0-5.700-5.700l-1 1M14 10a4 4 0 0 0-5.700 0l-3 3a4 4 0 0 0 5.700 5.700l1-1" /></S>,64 down: <S><path d="m6 9.500 6 6 6-6" /></S>,65 more: <S fill><circle cx="5.500" cy="12" r="1.700" /><circle cx="12" cy="12" r="1.700" /><circle cx="18.500" cy="12" r="1.700" /></S>,66 crown: <S><path d="m4 17.500-1-9 5 3.500 4-6.500 4 6.500 5-3.500-1 9z" /></S>,67 sparkle: <S><path d="M12 3.500 13.800 9 19.500 11l-5.700 2-1.800 5.500-1.800-5.500L4.500 11l5.700-2z" /></S>,68 eye: <S><path d="M2.500 12s3.500-6.500 9.500-6.500S21.500 12 21.500 12s-3.500 6.500-9.500 6.500S2.500 12 2.500 12z" /><circle cx="12" cy="12" r="2.800" /></S>,69 speaker: <S><path d="M4.500 9.500v5h3.500l5 4v-13l-5 4zM16.500 9a4.500 4.500 0 0 1 0 6M18.800 6.500a8 8 0 0 1 0 11" /></S>,70}7172// ---------- contexts ----------73export const EngineCtx = createContext<{ engine: Engine; calls: CallManager; signOut: (wipe: boolean) => void } | null>(null)74export function useApp() {75 const c = useContext(EngineCtx)76 if (!c) throw new Error("no engine")77 useSyncExternalStore(c.engine.subscribe, () => c.engine.version)78 return c79}80export function useCalls() {81 const { calls } = useContext(EngineCtx)!82 useSyncExternalStore(calls.subscribe, () => calls.version)83 return calls84}8586// ---------- avatar ----------87const HUES = [18, 340, 262, 205, 168, 42, 300, 228]88export function Avatar({ id, name, size = 44, online }: { id: string; name: string; size?: number; online?: boolean | null }) {89 let h = 090 for (const ch of id) h = (h * 31 + ch.charCodeAt(0)) >>> 091 const hue = HUES[h % HUES.length]92 const initials = name.trim().split(/\s+/).slice(0, 2).map((w) => w[0]?.toUpperCase() ?? "").join("")93 return (94 <span className="avatar" style={{ ["--s" as never]: `${size}px`, background: `linear-gradient(135deg, hsl(${hue} 85% 62%), hsl(${(hue + 40) % 360} 75% 48%))` }} aria-hidden="true">95 {initials || "?"}96 {online ? <i className="dot" /> : null}97 </span>98 )99}100101// ---------- modal sheet ----------102export function Sheet({ title, onClose, children, wide }: { title: ReactNode; onClose: () => void; children: ReactNode; wide?: boolean }) {103 const ref = useRef<HTMLDivElement>(null)104 useEffect(() => {105 const prev = document.activeElement as HTMLElement | null106 ref.current?.focus()107 const onKey = (e: KeyboardEvent) => e.key === "Escape" && (e.stopPropagation(), onClose())108 window.addEventListener("keydown", onKey, true)109 return () => {110 window.removeEventListener("keydown", onKey, true)111 prev?.focus?.()112 }113 }, [onClose])114 return (115 <div className="backdrop" onMouseDown={(e) => e.target === e.currentTarget && onClose()}>116 <div className={`sheet ${wide ? "wide" : ""}`} role="dialog" aria-modal="true" tabIndex={-1} ref={ref}>117 <div className="sheet-head">118 <h2 className="h2">{title}</h2>119 <button className="icon-btn" onClick={onClose} aria-label={t("Close", "Fermer")}>{I.x}</button>120 </div>121 {children}122 </div>123 </div>124 )125}126127export function Switch({ checked, onChange, label }: { checked: boolean; onChange: (v: boolean) => void; label: string }) {128 return <button className="switch" role="switch" aria-checked={checked} aria-label={label} onClick={() => onChange(!checked)} />129}130131// ---------- context menu ----------132export type MenuItem = { label: string; icon?: ReactNode; danger?: boolean; onClick: () => void }133export function Menu({ x, y, items, quick, onClose }: { x: number; y: number; items: MenuItem[]; quick?: { emoji: string; onClick: () => void }[]; onClose: () => void }) {134 const ref = useRef<HTMLDivElement>(null)135 const [pos, setPos] = useState({ x, y })136 useEffect(() => {137 const r = ref.current!.getBoundingClientRect()138 setPos({ x: Math.max(8, Math.min(x, innerWidth - r.width - 8)), y: Math.max(8, Math.min(y, innerHeight - r.height - 8)) })139 const close = (e: Event) => !ref.current?.contains(e.target as Node) && onClose()140 const key = (e: KeyboardEvent) => e.key === "Escape" && onClose()141 setTimeout(() => {142 window.addEventListener("pointerdown", close)143 window.addEventListener("keydown", key)144 })145 return () => {146 window.removeEventListener("pointerdown", close)147 window.removeEventListener("keydown", key)148 }149 }, [x, y, onClose])150 return (151 <div className="menu" ref={ref} style={{ left: pos.x, top: pos.y }} role="menu">152 {quick && (153 <div className="quick">154 {quick.map((q) => (155 <button key={q.emoji} onClick={() => (q.onClick(), onClose())} aria-label={q.emoji}>{q.emoji}</button>156 ))}157 </div>158 )}159 {items.map((it) => (160 <button key={it.label} role="menuitem" className={it.danger ? "danger" : ""} onClick={() => (it.onClick(), onClose())}>161 {it.icon}162 {it.label}163 </button>164 ))}165 </div>166 )167}168169// ---------- toasts ----------170type Toast = { id: number; text: string; action?: { label: string; run: () => void } }171let pushToast: (t: Omit<Toast, "id">) => void = () => {}172export const toast = (text: string, action?: Toast["action"]) => pushToast({ text, action })173export function Toasts() {174 const [list, setList] = useState<Toast[]>([])175 useEffect(() => {176 pushToast = (x) => {177 const id = Date.now() + Math.random()178 setList((l) => [...l.slice(-3), { ...x, id }])179 setTimeout(() => setList((l) => l.filter((y) => y.id !== id)), x.action ? 9000 : 4200)180 }181 }, [])182 return (183 <div className="toasts" role="status" aria-live="polite">184 {list.map((x) => (185 <div className="toast" key={x.id}>186 <span className="grow">{x.text}</span>187 {x.action && <button className="btn sm primary" onClick={() => (x.action!.run(), setList((l) => l.filter((y) => y.id !== x.id)))}>{x.action.label}</button>}188 </div>189 ))}190 </div>191 )192}193194// ---------- formatting ----------195export function timeShort(ts: number) {196 const d = new Date(ts), now = new Date()197 if (d.toDateString() === now.toDateString()) return d.toLocaleTimeString(locale, { hour: "2-digit", minute: "2-digit" })198 if (now.getTime() - ts < 6 * 86400_000) return d.toLocaleDateString(locale, { weekday: "short" })199 return d.toLocaleDateString(locale, { day: "numeric", month: "short" })200}201export const clock = (ts: number) => new Date(ts).toLocaleTimeString(locale, { hour: "2-digit", minute: "2-digit" })202export function dayLabel(ts: number) {203 const d = new Date(ts), now = new Date()204 if (d.toDateString() === now.toDateString()) return t("Today", "Aujourd’hui")205 if (new Date(now.getTime() - 86400_000).toDateString() === d.toDateString()) return t("Yesterday", "Hier")206 return d.toLocaleDateString(locale, { weekday: "long", day: "numeric", month: "long", year: d.getFullYear() === now.getFullYear() ? undefined : "numeric" })207}208export const dateTime = (ts: number | string) => new Date(ts).toLocaleString(locale, { dateStyle: "medium", timeStyle: "short" })209export function haptic(ms = 8) {210 try {211 navigator.vibrate?.(ms)212 } catch {}213}214export function errText(e: unknown) {215 const m = (e as Error)?.message ?? String(e)216 return m === "Network unavailable" ? t("Network unavailable.", "Réseau indisponible.") : m217}218export function ttlLabel(s: number) {219 if (!s) return t("Off", "Désactivé")220 if (s < 3600) return `${Math.round(s / 60)} min`221 if (s < 86400) return `${Math.round(s / 3600)} h`222 return `${Math.round(s / 86400)} ${t("days", "jours")}`223}224