import { convertFileSrc, isTauri } from "@tauri-apps/api/core"; import { emitTo } from "@tauri-apps/api/event"; import { getCurrentWindow } from "@tauri-apps/api/window"; import { writeImage, writeText } from "@tauri-apps/plugin-clipboard-manager"; import { Copy, FileVideo, FolderSearch, Images, Send } from "lucide-react"; import { useCallback, useEffect, useRef, useState } from "react"; import { useI18n } from "../i18n"; import { api } from "../lib/api"; import { basename, decodeHexParam, messageFrom } from "../lib/utils"; import type { CaptureResult } from "../types"; const THUMB_EXTENSIONS = ["png", "jpg", "jpeg", "webp", "gif", "bmp", "tiff", "tif"]; function readInfo(): CaptureResult | null { const raw = decodeHexParam(new URLSearchParams(window.location.search).get("info")); if (!raw) return null; try { const parsed = JSON.parse(raw) as CaptureResult; return parsed.path ? parsed : null; } catch { return null; } } /** * The card that appears the moment a screenshot or recording is saved. It answers * the four things a capture raises, in the order they get asked: put it on the * clipboard, keep it on screen, hand it to another machine, open it. It hides * itself, so ignoring it is a valid answer too. */ export function ResultCard() { const { t } = useI18n(); const info = readInfo(); const ttl = info?.ttlMs ?? 8000; const [remaining, setRemaining] = useState(ttl); const [failed, setFailed] = useState(false); const [note, setNote] = useState(null); const hold = useRef(null); const close = useCallback(() => { void getCurrentWindow() .close() .catch(() => undefined); }, []); useEffect(() => { const started = Date.now(); const tick = window.setInterval(() => { const left = Math.max(0, ttl - (Date.now() - started)); setRemaining(left); if (left === 0) { window.clearInterval(tick); close(); } }, 100); return () => window.clearInterval(tick); }, [close, ttl]); // Hovering pauses the fuse: the difference between a helper and a reflex test. function pause() { if (hold.current) window.clearTimeout(hold.current); } function resume() { pause(); hold.current = window.setTimeout(close, 2500); } useEffect(() => () => pause(), []); async function copy() { if (!info) return; try { if (info.kind === "image") await writeImage(info.path); else await writeText(info.path); setNote(t("copied")); } catch (error) { setNote(t(messageFrom(error))); } } async function open() { if (!info) return; await api.openInEditor(info.path); close(); } async function pin() { if (!info || info.kind !== "image") return; await api.pinImage(info.path); close(); } async function reveal() { if (!info) return; await api.revealFile(info.path); close(); } function send() { if (!info) return; // The main window owns the peer list, so the request goes there. void emitTo("main", "app://send-files", { paths: [info.path] }); close(); } if (!info) return null; const extension = info.path.split(".").pop()?.toLowerCase() ?? ""; const showThumb = info.kind === "image" && THUMB_EXTENSIONS.includes(extension) && !failed; const percent = ttl > 0 ? (remaining / ttl) * 100 : 0; return (
{showThumb ? ( setFailed(true)} /> ) : info.kind === "video" ? ( ) : ( )}
{t("captureSaved")} {basename(info.path)} {note ? {note} : null}
{info.kind === "image" ? ( ) : null}
); }