2025-08-13 14:23:48 +01:00
|
|
|
import { useRef, useCallback, useEffect } from "react";
|
|
|
|
|
import { useQuery } from "@tanstack/react-query";
|
2025-09-29 13:00:56 +01:00
|
|
|
import { CAM_BASE } from "../utils/config";
|
2025-08-13 14:23:48 +01:00
|
|
|
|
2025-09-29 13:00:56 +01:00
|
|
|
const apiUrl = CAM_BASE;
|
2025-11-10 11:55:15 +00:00
|
|
|
|
|
|
|
|
async function fetchSnapshot(cameraSide: string): Promise<Blob> {
|
2025-10-06 14:21:56 +01:00
|
|
|
const response = await fetch(`${apiUrl}/${cameraSide}-preview`, {
|
2025-11-05 16:30:27 +00:00
|
|
|
signal: AbortSignal.timeout(300000),
|
2025-11-10 11:55:15 +00:00
|
|
|
cache: "no-store",
|
2025-10-06 14:21:56 +01:00
|
|
|
});
|
2025-08-13 14:23:48 +01:00
|
|
|
if (!response.ok) {
|
2025-11-10 11:55:15 +00:00
|
|
|
throw new Error(`Cannot reach endpoint (${response.status})`);
|
|
|
|
|
}
|
|
|
|
|
return response.blob();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Draw an ImageBitmap to canvas with aspect-fill (like object-fit: cover) */
|
|
|
|
|
function drawBitmapToCanvas(canvas: HTMLCanvasElement, bitmap: ImageBitmap) {
|
|
|
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
|
if (!ctx) return;
|
|
|
|
|
|
|
|
|
|
const dpr = window.devicePixelRatio || 1;
|
|
|
|
|
const cssWidth = canvas.clientWidth;
|
|
|
|
|
const cssHeight = canvas.clientHeight;
|
|
|
|
|
|
|
|
|
|
const width = Math.floor(cssWidth * dpr);
|
|
|
|
|
const height = Math.floor(cssHeight * dpr);
|
|
|
|
|
|
|
|
|
|
if (canvas.width !== width || canvas.height !== height) {
|
|
|
|
|
canvas.width = width;
|
|
|
|
|
canvas.height = height;
|
2025-08-13 14:23:48 +01:00
|
|
|
}
|
2025-09-12 08:21:52 +01:00
|
|
|
|
2025-11-10 11:55:15 +00:00
|
|
|
ctx.clearRect(0, 0, width, height);
|
|
|
|
|
|
|
|
|
|
const srcW = bitmap.width;
|
|
|
|
|
const srcH = bitmap.height;
|
|
|
|
|
const srcAspect = srcW / srcH;
|
|
|
|
|
const dstAspect = width / height;
|
|
|
|
|
|
|
|
|
|
let drawWidth = width;
|
|
|
|
|
let drawHeight = height;
|
|
|
|
|
|
|
|
|
|
// aspect-fit calculation (no cropping)
|
|
|
|
|
if (srcAspect > dstAspect) {
|
|
|
|
|
// image is wider → fit to canvas width
|
|
|
|
|
drawWidth = width;
|
|
|
|
|
drawHeight = width / srcAspect;
|
|
|
|
|
} else {
|
|
|
|
|
// image is taller → fit to canvas height
|
|
|
|
|
drawHeight = height;
|
|
|
|
|
drawWidth = height * srcAspect;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// center image (adds black borders if aspect ratios differ)
|
|
|
|
|
const dx = (width - drawWidth) / 50;
|
|
|
|
|
const dy = (height - drawHeight) / 2;
|
|
|
|
|
|
|
|
|
|
ctx.imageSmoothingEnabled = true;
|
|
|
|
|
ctx.imageSmoothingQuality = "high";
|
|
|
|
|
ctx.drawImage(bitmap, 0, 0, srcW, srcH, dx, dy, drawWidth, drawHeight);
|
2025-08-13 14:23:48 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-30 11:11:46 +01:00
|
|
|
export function useGetOverviewSnapshot(side: string) {
|
2025-08-13 14:23:48 +01:00
|
|
|
const canvasRef = useRef<HTMLCanvasElement | null>(null);
|
2025-11-10 11:55:15 +00:00
|
|
|
const latestBitmapRef = useRef<ImageBitmap | null>(null);
|
2025-08-13 14:23:48 +01:00
|
|
|
|
2025-11-10 11:55:15 +00:00
|
|
|
// Redraw helper; always draws the current bitmap if available
|
|
|
|
|
const draw = useCallback(() => {
|
2025-08-13 14:23:48 +01:00
|
|
|
const canvas = canvasRef.current;
|
2025-11-10 11:55:15 +00:00
|
|
|
const bmp = latestBitmapRef.current;
|
|
|
|
|
if (!canvas || !bmp) return;
|
|
|
|
|
drawBitmapToCanvas(canvas, bmp);
|
2025-08-13 14:23:48 +01:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
data: snapshotBlob,
|
|
|
|
|
isError,
|
|
|
|
|
error,
|
2025-09-10 09:05:47 +01:00
|
|
|
isPending,
|
2025-08-13 14:23:48 +01:00
|
|
|
} = useQuery({
|
2025-11-10 11:55:15 +00:00
|
|
|
queryKey: ["overviewSnapshot", side],
|
2025-09-30 11:11:46 +01:00
|
|
|
queryFn: () => fetchSnapshot(side),
|
2025-11-10 11:55:15 +00:00
|
|
|
// Poll ~4 fps when visible; pause when tab hidden
|
|
|
|
|
refetchInterval: () => (document.visibilityState === "visible" ? 250 : false),
|
2025-08-13 14:23:48 +01:00
|
|
|
refetchOnWindowFocus: false,
|
2025-11-10 11:55:15 +00:00
|
|
|
// Avoid keeping lots of blobs around in cache
|
|
|
|
|
gcTime: 0, // v5 name (cacheTime in v4)
|
|
|
|
|
staleTime: 0,
|
|
|
|
|
retry: false, // or a small number if you prefer retries
|
2025-08-13 14:23:48 +01:00
|
|
|
});
|
|
|
|
|
|
2025-11-10 11:55:15 +00:00
|
|
|
// Convert Blob -> ImageBitmap and draw
|
2025-08-13 14:23:48 +01:00
|
|
|
useEffect(() => {
|
2025-11-10 11:55:15 +00:00
|
|
|
let cancelled = false;
|
2025-08-13 14:23:48 +01:00
|
|
|
if (!snapshotBlob) return;
|
|
|
|
|
|
2025-11-10 11:55:15 +00:00
|
|
|
(async () => {
|
|
|
|
|
try {
|
|
|
|
|
const bitmap = await createImageBitmap(snapshotBlob);
|
|
|
|
|
if (cancelled) {
|
|
|
|
|
bitmap.close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-13 14:23:48 +01:00
|
|
|
|
2025-11-10 11:55:15 +00:00
|
|
|
// Dispose previous bitmap to free memory
|
|
|
|
|
if (latestBitmapRef.current) {
|
|
|
|
|
latestBitmapRef.current.close();
|
|
|
|
|
}
|
|
|
|
|
latestBitmapRef.current = bitmap;
|
2025-08-13 14:23:48 +01:00
|
|
|
|
2025-11-10 11:55:15 +00:00
|
|
|
// Draw now (and again on next resize)
|
|
|
|
|
draw();
|
|
|
|
|
} catch {
|
|
|
|
|
// noop — fetch handler surfaces the main error path
|
|
|
|
|
}
|
|
|
|
|
})();
|
2025-08-13 14:23:48 +01:00
|
|
|
|
|
|
|
|
return () => {
|
2025-11-10 11:55:15 +00:00
|
|
|
cancelled = true;
|
|
|
|
|
};
|
|
|
|
|
}, [snapshotBlob, draw]);
|
|
|
|
|
|
|
|
|
|
// Redraw on resize & DPR changes
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const onResize = () => draw();
|
|
|
|
|
const onDPR = () => draw();
|
|
|
|
|
window.addEventListener("resize", onResize);
|
|
|
|
|
// Listen for DPR changes (some browsers support this)
|
|
|
|
|
const mql = window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`);
|
|
|
|
|
mql.addEventListener?.("change", onDPR);
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener("resize", onResize);
|
|
|
|
|
mql.removeEventListener?.("change", onDPR);
|
2025-08-13 14:23:48 +01:00
|
|
|
};
|
2025-11-10 11:55:15 +00:00
|
|
|
}, [draw]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const el = canvasRef.current?.parentElement; // the box
|
|
|
|
|
if (!el) return;
|
|
|
|
|
const ro = new ResizeObserver(() => draw()); // your draw() calls aspect-fit logic
|
|
|
|
|
ro.observe(el);
|
|
|
|
|
return () => ro.disconnect();
|
|
|
|
|
}, [draw]);
|
2025-08-13 14:23:48 +01:00
|
|
|
|
2025-11-10 11:55:15 +00:00
|
|
|
// Cleanup on unmount
|
2025-08-13 14:23:48 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
return () => {
|
2025-11-10 11:55:15 +00:00
|
|
|
if (latestBitmapRef.current) {
|
|
|
|
|
latestBitmapRef.current.close();
|
|
|
|
|
latestBitmapRef.current = null;
|
|
|
|
|
}
|
2025-08-13 14:23:48 +01:00
|
|
|
};
|
2025-11-10 11:55:15 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
// Optional: normalize error type
|
|
|
|
|
const typedError = error instanceof Error ? error : undefined;
|
2025-08-13 14:23:48 +01:00
|
|
|
|
2025-11-10 11:55:15 +00:00
|
|
|
return { canvasRef, isError, error: typedError, isPending };
|
2025-08-13 14:23:48 +01:00
|
|
|
}
|