1import type { PreviewAnnotationPayload } from "@t3tools/contracts";
2
3import { dataUrlToFile } from "./imageCompression";
4
5export type PreviewAnnotationCapture =
6 | { readonly status: "captured"; readonly file: File }
7 | { readonly status: "none" }
8 | { readonly status: "failed" };
9
10const PNG_DATA_URL_PREFIX = "data:image/png;base64,";
11
12/** Decode Electron's PNG crop locally; fetching a data URL violates desktop connect-src. */
13export function capturePreviewAnnotationScreenshot(
14 annotation: PreviewAnnotationPayload,
15): PreviewAnnotationCapture {
16 if (!annotation.screenshot) return { status: "none" };
17 try {
18 const { dataUrl } = annotation.screenshot;
19 if (!dataUrl.startsWith(PNG_DATA_URL_PREFIX)) {
20 return { status: "failed" };
21 }
22 const file = dataUrlToFile(dataUrl, `preview-annotation-${annotation.id}.png`, "image/png");
23 return file.size > 0 ? { status: "captured", file } : { status: "failed" };
24 } catch {
25 return { status: "failed" };
26 }
27}