SPB Git

Repository Public

controls.tsx 4.8 KB 83 lines tsx

1"use client";2import { useEffect, useState } from "react";3import { useLocale } from "@/i18n/client";45const icon = { width: 18, height: 18, viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 1.75, strokeLinecap: "round" as const, strokeLinejoin: "round" as const, "aria-hidden": true };67/** Light / dark switch. The choice is stored in the browser only. */8export function ModeToggle() {9  const locale = useLocale();10  const [mode, setMode] = useState<"light" | "dark" | null>(null);11  useEffect(() => {12    const stored = document.documentElement.dataset.mode;13    setMode(stored === "light" || stored === "dark" ? stored : window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light");14  }, []);15  const toggle = () => {16    const next = mode === "dark" ? "light" : "dark";17    document.documentElement.dataset.mode = next;18    try { localStorage.setItem("sheet-mode", next); } catch { /* private mode */ }19    setMode(next);20  };21  return (22    <button type="button" onClick={toggle} className="btn btn-ghost btn-sm !min-h-11 !px-3" aria-label={locale === "fr" ? (mode === "dark" ? "Passer au thème clair" : "Passer au thème sombre") : mode === "dark" ? "Switch to light theme" : "Switch to dark theme"}>23      {mode === "dark"24        ? <svg {...icon}><circle cx="12" cy="12" r="4" /><path d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4" /></svg>25        : <svg {...icon}><path d="M20 14.5A8 8 0 0 1 9.5 4a8 8 0 1 0 10.5 10.5z" /></svg>}26    </button>27  );28}2930/** FR / EN switch for the interface. Sets a first-party cookie and reloads the same page. */31export function LocaleSwitch() {32  const locale = useLocale();33  const other = locale === "fr" ? "en" : "fr";34  const go = () => { document.cookie = `sheet_locale=${other}; path=/; max-age=31536000; samesite=lax`; window.location.reload(); };35  return (36    <button type="button" onClick={go} className="btn btn-ghost btn-sm !min-h-11 !px-3 font-semibold" lang={other} aria-label={other === "en" ? "EN — Switch to English" : "FR — Passer en français"}>37      {other.toUpperCase()}38    </button>39  );40}4142/** Native share sheet on phones, copy-to-clipboard elsewhere. */43export function ShareButtons({ title, url }: { title: string; url: string }) {44  const locale = useLocale();45  const [copied, setCopied] = useState(false);46  const [canShare, setCanShare] = useState(false);47  useEffect(() => { setCanShare(typeof navigator !== "undefined" && typeof navigator.share === "function"); }, []);48  const copy = async () => { try { await navigator.clipboard.writeText(url); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch { window.prompt(locale === "fr" ? "Copier le lien :" : "Copy the link:", url); } };49  const enc = encodeURIComponent;50  return (51    <div className="no-print flex flex-wrap items-center gap-2">52      {canShare && (53        <button type="button" className="btn btn-sm" onClick={() => { void navigator.share({ title, url }).catch(() => {}); }}>54          <svg {...icon}><path d="M12 15V3M8 7l4-4 4 4M5 12v7a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-7" /></svg>{locale === "fr" ? "Partager" : "Share"}55        </button>56      )}57      <button type="button" className="btn btn-sm" onClick={copy} aria-live="polite">58        <svg {...icon}><rect x="9" y="9" width="11" height="11" rx="2" /><path d="M5 15V6a2 2 0 0 1 2-2h9" /></svg>59        {copied ? (locale === "fr" ? "Lien copié" : "Link copied") : locale === "fr" ? "Copier le lien" : "Copy link"}60      </button>61      <a className="btn btn-sm" href={`https://www.linkedin.com/sharing/share-offsite/?url=${enc(url)}`} target="_blank" rel="noopener noreferrer">LinkedIn</a>62      <a className="btn btn-sm" href={`https://x.com/intent/post?url=${enc(url)}&text=${enc(title)}`} target="_blank" rel="noopener noreferrer">X</a>63      <a className="btn btn-sm" href={`mailto:?subject=${enc(title)}&body=${enc(url)}`}>{locale === "fr" ? "Courriel" : "Email"}</a>64    </div>65  );66}6768/** Counts one view when a published sheet is actually displayed (not on prefetch, not for bots). */69export function ViewBeacon({ slug, locale }: { slug: string; locale: "fr" | "en" }) {70  useEffect(() => {71    if (document.visibilityState !== "visible") return;72    const t = setTimeout(() => { void fetch("/api/public/views", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ slug, locale }), keepalive: true }).catch(() => {}); }, 3000);73    return () => clearTimeout(t);74  }, [slug, locale]);75  return null;76}7778/** Registers the service worker (installable app; pages are always fetched from the network first). */79export function RegisterSw() {80  useEffect(() => { if ("serviceWorker" in navigator && window.location.protocol === "https:") void navigator.serviceWorker.register("/sw.js").catch(() => {}); }, []);81  return null;82}83
Files main
Menu

Theme