Repository Public
1// Zone d'aperçu : ligne de boîte de réception simulée, cadre isolé, bascule ordinateur/téléphone, clair/sombre, ajustement.2import { useEffect, useRef, useState } from "react"3import { t } from "../../i18n"4import type { ColorScheme } from "../../templates/darkmode"5import { PREHEADER_SOFT_LIMIT, SUBJECT_SOFT_LIMIT, truncateVisible } from "../../templates/variables"6import { Icon, IconButton } from "../kit"7import { TemplateFrame } from "./Frame"89export type Device = "desktop" | "mobile"10const DEVICE_W: Record<Device, number> = { desktop: 680, mobile: 390 }11const MOBILE_MAX_H = 78012const BEZEL = 121314export interface PreviewPaneProps {15 /** HTML à montrer (rendu ou brut) ; null tant que le premier aperçu n'est pas arrivé */16 html: string | null17 name: string18 subject: string19 preheader: string20 sender: string21 loading: boolean22 error: string | null23 raw: boolean24 onToggleRaw: () => void25}2627export function PreviewPane({ html, name, subject, preheader, sender, loading, error, raw, onToggleRaw }: PreviewPaneProps) {28 const stage = useRef<HTMLDivElement>(null)29 const [device, setDevice] = useState<Device>("desktop")30 const [scheme, setScheme] = useState<ColorScheme>("light")31 const [fit, setFit] = useState(true)32 const [box, setBox] = useState<{ w: number; h: number }>({ w: 0, h: 0 })33 const [info, setInfo] = useState<{ hasDark: boolean; images: number }>({ hasDark: true, images: 0 })3435 useEffect(() => {36 const el = stage.current37 if (!el) return38 const measure = () => setBox((b) => (b.w === el.clientWidth && b.h === el.clientHeight ? b : { w: el.clientWidth, h: el.clientHeight }))39 const ro = new ResizeObserver(measure)40 ro.observe(el)41 measure()42 return () => ro.disconnect()43 }, [])4445 const chrome = device === "mobile" ? BEZEL * 2 : 246 const availW = Math.max(0, box.w - 32 - chrome)47 const availH = Math.max(0, box.h - 32 - chrome)48 const width = DEVICE_W[device]49 const scale = fit && availW > 0 ? Math.min(1, availW / width) : 150 const height = Math.max(320, Math.floor(device === "mobile" ? Math.min(MOBILE_MAX_H, availH / scale) : availH / scale))51 const ready = html !== null && box.w > 052 const shownSubject = subject.trim() || t("tpl.noSubject")5354 return (55 <div className="tpl-preview">56 <div className="tpl-preview-bar" role="toolbar" aria-label={t("tpl.preview.toolbar")}>57 <div className="segmented" role="group" aria-label={t("tpl.preview.device")}>58 <button type="button" aria-pressed={device === "desktop"} onClick={() => setDevice("desktop")} title={t("tpl.preview.desktop")}>59 <Icon name="desktop" small />60 <span className="tpl-seg-label">{t("tpl.preview.desktop")}</span>61 </button>62 <button type="button" aria-pressed={device === "mobile"} onClick={() => setDevice("mobile")} title={t("tpl.preview.mobile")}>63 <Icon name="phone" small />64 <span className="tpl-seg-label">{t("tpl.preview.mobile")}</span>65 </button>66 </div>67 <div className="segmented" role="group" aria-label={t("tpl.preview.scheme")}>68 <button type="button" aria-pressed={scheme === "light"} onClick={() => setScheme("light")} title={t("tpl.preview.light")}>69 <Icon name="sun" small />70 <span className="tpl-seg-label">{t("tpl.preview.light")}</span>71 </button>72 <button type="button" aria-pressed={scheme === "dark"} onClick={() => setScheme("dark")} title={t("tpl.preview.dark")}>73 <Icon name="moon" small />74 <span className="tpl-seg-label">{t("tpl.preview.dark")}</span>75 </button>76 </div>77 <span className="spacer" />78 <IconButton icon="braces" label={t("tpl.preview.raw")} pressed={raw} onClick={onToggleRaw} />79 <IconButton icon="fit" label={t("tpl.preview.fit")} pressed={fit} onClick={() => setFit((f) => !f)} />80 <span className="muted small mono tpl-zoom" aria-label={t("tpl.preview.zoom", { pct: Math.round(scale * 100) })}>81 {Math.round(scale * 100)} %82 </span>83 </div>8485 <div className="tpl-inbox" aria-label={t("tpl.preview.inboxLine")} role="group" data-testid="inbox-line">86 <span className="tpl-inbox-avatar" aria-hidden="true">87 {(Array.from(sender.trim())[0] ?? "•").toUpperCase()}88 </span>89 <div className="tpl-inbox-text">90 <div className="row">91 <strong className="truncate spacer">{sender}</strong>92 <span className="muted small">9:41</span>93 </div>94 <p className="truncate">95 <span className="tpl-inbox-subject" data-testid="inbox-subject">96 {truncateVisible(shownSubject, SUBJECT_SOFT_LIMIT + 30)}97 </span>98 {preheader.trim() ? (99 <span className="muted" data-testid="inbox-preheader">100 {" — "}101 {truncateVisible(preheader.trim(), PREHEADER_SOFT_LIMIT + 30)}102 </span>103 ) : null}104 </p>105 </div>106 </div>107108 {error ? (109 <div className="banner warn" role="alert">110 <Icon name="warn" small />111 <span>{error}</span>112 </div>113 ) : null}114 {scheme === "dark" && ready && !info.hasDark ? (115 <p className="muted small tpl-preview-note" role="status">116 {t("tpl.preview.noDark")}117 </p>118 ) : null}119120 <div ref={stage} className={`tpl-stage${fit ? "" : " scroll"}`} data-device={device} data-scheme={scheme} data-loading={loading || undefined} data-testid="preview-stage">121 {ready ? (122 <div className={`tpl-device ${device}`} style={device === "mobile" ? { padding: BEZEL * scale, borderRadius: 34 * scale } : undefined}>123 <TemplateFrame html={html} title={t("tpl.preview.frameTitle", { name })} scheme={scheme} width={width} height={height} scale={scale} onInfo={setInfo} />124 </div>125 ) : (126 <div className="tpl-stage-wait">127 <div className="skeleton tpl-stage-skel" />128 </div>129 )}130 </div>131 </div>132 )133}134