import { useEffect, useRef, useState } from "react"; import { API_V1, CREDENTIALS, checkTenant, UNAUTHORIZED_EVENT } from "../apiContract"; import { ConfirmDialog } from "../ui/ConfirmDialog"; type LogoState = { status: "loading" } | { status: "ready"; url: string | null } | { status: "error" }; type LogoWrite = { method: "PUT"; file: File } | { method: "DELETE" }; const IMAGE_TYPES = ["image/png", "image/jpeg"]; async function requestLogo(options: RequestInit = {}): Promise { const response = await fetch(`${API_V1}/tenant-brand/logo`, { credentials: CREDENTIALS, cache: "no-store", ...options, }); if (checkTenant(response)) throw new Error("Workspace changed"); if (response.status === 401) { window.dispatchEvent(new CustomEvent(UNAUTHORIZED_EVENT)); throw new Error("Session expired"); } if (!response.ok && !(response.status === 404 && !options.method)) throw new Error("Logo request failed"); return response; } function useStoredLogo(revision: number): LogoState { const [state, setState] = useState({ status: "loading" }); useEffect(() => { const controller = new AbortController(); let url: string | null = null; setState({ status: "loading" }); async function load() { try { const response = await requestLogo({ signal: controller.signal }); const image = response.status === 404 ? null : await response.blob(); if (controller.signal.aborted) return; if (image && !IMAGE_TYPES.includes(image.type)) throw new Error("Invalid logo image"); url = image ? URL.createObjectURL(image) : null; setState({ status: "ready", url }); } catch { if (!controller.signal.aborted) setState({ status: "error" }); } } void load(); return () => { controller.abort(); if (url) URL.revokeObjectURL(url); }; }, [revision]); return state; } function useLogoWrites(reload: () => void) { const [busy, setBusy] = useState(false); const [error, setError] = useState(""); const [failed, setFailed] = useState(null); const inFlight = useRef(false); async function save(write: LogoWrite) { if (inFlight.current) return; setError(""); setFailed(null); if (write.method === "PUT" && (!IMAGE_TYPES.includes(write.file.type) || write.file.size === 0 || write.file.size > 2 * 1024 * 1024)) { setError("Choose a PNG or JPEG up to 2 MB."); return; } inFlight.current = true; setBusy(true); try { await requestLogo({ method: write.method, ...(write.method === "PUT" ? { headers: { "Content-Type": write.file.type }, body: write.file, } : {}) }); reload(); } catch { setError("The logo could not be saved."); setFailed(write); } finally { inFlight.current = false; setBusy(false); } } return { busy, error, failed, save }; } export default function BrandPane() { const [revision, setRevision] = useState(0); const [confirming, setConfirming] = useState(false); const fileRef = useRef(null); const reload = () => setRevision(value => value + 1); const logo = useStoredLogo(revision); const { busy, error, failed, save } = useLogoWrites(reload); const unavailable = busy || logo.status !== "ready"; return (

Brand

Your logo on report files

{logo.status === "loading" ?
: null} {logo.status === "error" ?
The logo could not be loaded.
: null} {logo.status === "ready" ? (logo.url ? Report logo :

No logo yet

) : null}
{ const file = event.currentTarget.files?.[0]; event.currentTarget.value = ""; if (file) void save({ method: "PUT", file }); }} /> {logo.status === "ready" && logo.url ? : null}
{error ?
{error}
: null} setConfirming(false)} onConfirm={() => void save({ method: "DELETE" }).then(() => setConfirming(false))} />
); }