import type { DriveFileInfo } from "@/lib/api/types" import { drivePreviewKind, isSvgFile } from "@/lib/drive/drive-preview" export type DemoDrivePreviewResult = | { type: "url"; url: string; display: "image" } | { type: "text"; content: string } | { type: "svg"; markup: string } function demoSeed(file: { path: string; file_id?: number }): string { return file.file_id ? `ultimail-drive-${file.file_id}` : `ultimail-drive-${file.path}` } /** Stable Picsum URL for demo raster previews / thumbnails. */ export function demoDrivePicsumUrl( file: { path: string; file_id?: number }, width = 400, height = 300 ): string { return `https://picsum.photos/seed/${encodeURIComponent(demoSeed(file))}/${width}/${height}` } const DEMO_TEXT_BY_PATH: Record = { "/Perso/Notes réunion.txt": `# Notes réunion — 9 juin - Roadmap Q3 validée en comité - Beta agenda : livraison cible vendredi - Design system : tokens couleur à figer - Prochaine démo interne jeudi 11h`, "/Release notes v2.3.txt": `# Ultimail v2.3 ## Nouveautés - UltiCal : visio UltiMeet intégrée aux événements - UltiDrive : favoris et corbeille unifiés - Contacts : fusion et labels personnalisés ## Corrections - Sync calendrier CalDAV sur événements récurrents - Aperçu PDF plus rapide sur gros fichiers`, } function demoGenericText(name: string): string { return `# ${name.replace(/\.[^.]+$/, "")} Contenu de démonstration — fichier fictif pour la landing page Ultimail.` } function demoDriveSvgMarkup(name: string): string { const title = name.replace(/\.svg$/i, "") const isLogo = /logo/i.test(name) if (isLogo) { return ` U ${title} ` } return ` ${title} ` } /** Resolve a demo preview without hitting the API. */ export function resolveDemoDrivePreview( file: Pick, size?: { width: number; height: number } ): DemoDrivePreviewResult | null { const width = size?.width ?? 1200 const height = size?.height ?? 900 if (isSvgFile(file)) { return { type: "svg", markup: demoDriveSvgMarkup(file.name) } } const kind = drivePreviewKind(file) if (kind === "text") { return { type: "text", content: DEMO_TEXT_BY_PATH[file.path] ?? demoGenericText(file.name), } } if (kind === "image" || kind === "pdf") { return { type: "url", url: demoDrivePicsumUrl(file, width, height), display: "image" } } // Office / bureautique : vignette Picsum (pas de binaire réel en démo). const mime = (file.mime_type ?? "").toLowerCase() if ( mime.includes("officedocument") || mime.includes("spreadsheet") || mime.includes("presentation") || mime.includes("msword") ) { return { type: "url", url: demoDrivePicsumUrl(file, width, height), display: "image" } } return null }