nex_shift/src/components/PinnedImage.tsx

147 lines
4.4 KiB
TypeScript
Raw Normal View History

import { isTauri } from "@tauri-apps/api/core";
2026-10-25 04:24:05 +00:00
import { listen } from "@tauri-apps/api/event";
import { writeImage } from "@tauri-apps/plugin-clipboard-manager";
import { Copy, X } from "lucide-react";
2026-10-25 04:24:05 +00:00
import { useEffect, useRef, useState } from "react";
import { useI18n } from "../i18n";
import { api } from "../lib/api";
2026-10-25 04:24:05 +00:00
interface PinnedPreview {
id: number;
path: string;
dataUrl: string;
width: number;
height: number;
}
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();
2026-10-25 04:24:05 +00:00
const [path, setPath] = useState(() =>
decodePath(new URLSearchParams(window.location.search).get("path")),
);
const [src, setSrc] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
2026-10-25 04:24:05 +00:00
const latestId = useRef(0);
const renderedId = useRef(0);
useEffect(() => {
if (!isTauri()) {
setSrc("/demo-preview.svg");
return;
}
2026-10-25 04:24:05 +00:00
let disposed = false;
const apply = (preview: PinnedPreview | null) => {
if (disposed) return;
if (!preview) {
if (latestId.current === 0) setSrc(null);
return;
}
if (preview.id < latestId.current) return;
if (preview.id === renderedId.current) return;
latestId.current = preview.id;
setPath(preview.path);
const image = new Image();
image.onload = () => {
if (disposed) return;
renderedId.current = preview.id;
setSrc(preview.dataUrl);
setError(null);
// Let React commit the loaded image before the native window is shown.
requestAnimationFrame(() => {
requestAnimationFrame(() => {
if (!disposed) void api.pinReady(preview.id);
});
});
};
image.onerror = () => {
if (disposed) return;
renderedId.current = preview.id;
setError(t("pinLoadFailed"));
void api.pinReady(preview.id);
};
image.src = preview.dataUrl;
};
const refresh = () => {
void api
.getPinnedImage()
.then(apply)
.catch((reason) => {
if (disposed) return;
setError(String(reason).replace(/^Error:\s*/, ""));
void api.pinReady(null);
});
};
const refreshWhenVisible = () => {
if (document.visibilityState === "visible") refresh();
};
window.addEventListener("focus", refresh);
document.addEventListener("visibilitychange", refreshWhenVisible);
let unlisten: Promise<() => void> | null = null;
// Register first: a reused pin window can receive a new image immediately
// after the previous pin is closed. Querying first would lose that event.
unlisten = listen<PinnedPreview>("pin://show", (event) => apply(event.payload)).then(
(stopListening) => {
if (!disposed) refresh();
return stopListening;
},
);
return () => {
disposed = true;
window.removeEventListener("focus", refresh);
document.removeEventListener("visibilitychange", refreshWhenVisible);
void unlisten?.then((release) => release());
};
// Language changes only affect error text, not the image payload.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
async function copy() {
if (!path) return;
await writeImage(path);
}
if (error) {
return <div className="pin-error">{error}</div>;
}
return (
<div className="pin-shell">
<div
className="pin-surface"
data-tauri-drag-region
2026-10-25 04:24:05 +00:00
onDoubleClick={() => void api.closePin()}
>
{src ? (
2026-10-25 04:24:05 +00:00
<img
src={src}
alt=""
draggable={false}
data-tauri-drag-region
onError={() => setError(t("pinLoadFailed"))}
/>
) : (
<div className="pin-loading" />
)}
</div>
<div className="pin-actions">
<button type="button" title={t("copy")} onClick={() => void copy()}>
<Copy size={13} />
</button>
2026-10-25 04:24:05 +00:00
<button type="button" title={t("close")} onClick={() => void api.closePin()}>
<X size={13} />
</button>
</div>
</div>
);
}