import { isTauri } from "@tauri-apps/api/core"; import { getCurrentWindow } from "@tauri-apps/api/window"; import { writeImage } from "@tauri-apps/plugin-clipboard-manager"; import { Copy, X } from "lucide-react"; import { useEffect, useState } from "react"; import { useI18n } from "../i18n"; import { api } from "../lib/api"; function decodePath(value: string | null): string | null { if (!value || value.length % 2 !== 0 || !/^[0-9a-f]+$/i.test(value)) return null; try { const bytes = new Uint8Array((value.match(/../g) ?? []).map((pair) => parseInt(pair, 16))); return new TextDecoder().decode(bytes); } catch { return null; } } export function PinnedImage() { const { t } = useI18n(); const path = decodePath(new URLSearchParams(window.location.search).get("path")); const [src, setSrc] = useState(null); const [error, setError] = useState(null); useEffect(() => { if (!path) { setError("The pinned image is unavailable."); return; } if (!isTauri()) { setSrc("/demo-preview.svg"); return; } void api .readFileDataUrl(path) .then(setSrc) .catch((reason) => setError(String(reason))); }, [path]); async function copy() { if (!path) return; await writeImage(path); } if (error) { return
{error}
; } return (
void getCurrentWindow().close()} > {src ? ( ) : (
)}
); }