import { useEffect, useRef, useState, useCallback } from "react"; import { useWindows } from "@/contexts/windows"; import { MILLISECONDS_IN_SECOND, ONE_TIME_PASSIVE_EVENT, PEEK_MAX_WIDTH, } from "@/utils/constants"; import { isCanvasDrawn } from "@/utils/functions"; import * as htmlToImage from "html-to-image"; const FPS = 10; // Reduce FPS to improve performance const FRAME_INTERVAL = MILLISECONDS_IN_SECOND / FPS; const CACHE_DELAY = 2500; // 2.5 seconds delay const cache = new Map(); const getCanvasFromElement = async (element: HTMLElement) => { const spacing = element.tagName === "VIDEO" ? { margin: "0", padding: "0" } : {}; return htmlToImage.toCanvas(element, { ...(element.clientWidth > PEEK_MAX_WIDTH && { canvasHeight: Math.round( (PEEK_MAX_WIDTH / element.clientWidth) * element.clientHeight ), canvasWidth: PEEK_MAX_WIDTH, }), filter: (el) => !(el instanceof HTMLSourceElement), skipAutoScale: true, style: { inset: "0", ...spacing, }, }); }; const renderFrame = async ( previewElement: HTMLElement, animate: React.MutableRefObject, callback: (url: string) => void, latestDataUrl: React.MutableRefObject, id: string ): Promise => { if (!animate.current) return; const nextFrame = (): void => { setTimeout(() => { window.requestAnimationFrame(() => { renderFrame(previewElement, animate, callback, latestDataUrl, id); }); }, FRAME_INTERVAL); }; let dataCanvas: HTMLCanvasElement | undefined; try { dataCanvas = await getCanvasFromElement(previewElement); } catch { // Ignore failure to capture } if (dataCanvas && dataCanvas.width > 0 && dataCanvas.height > 0) { if (isCanvasDrawn(dataCanvas)) { dataCanvas.toBlob( (blob) => { if (!blob) return; const objectUrl = URL.createObjectURL(blob); latestDataUrl.current = objectUrl; cache.set(id, objectUrl); // Store the object URL in the cache callback(objectUrl); window.setTimeout(nextFrame, MILLISECONDS_IN_SECOND / FPS); }, "image/webp" // Using WebP format for smaller size ); } else { window.setTimeout(nextFrame, MILLISECONDS_IN_SECOND / FPS); } } else { nextFrame(); } }; const useWindowPeek = (id: string): string => { const { windows: { [id]: currentWindow }, } = useWindows(); const { peekElement, componentWindow } = currentWindow || {}; const previewTimer = useRef(); const [imageSrc, setImageSrc] = useState(""); const animate = useRef(true); const latestDataUrl = useRef(""); const startRendering = useCallback( (element: HTMLElement) => { window.requestAnimationFrame(() => renderFrame(element, animate, setImageSrc, latestDataUrl, id) ); animate.current = true; }, [id] ); const captureInitialFrame = useCallback( async (element: HTMLElement) => { try { const dataCanvas = await getCanvasFromElement(element); if ( dataCanvas && dataCanvas.width > 0 && dataCanvas.height > 0 && isCanvasDrawn(dataCanvas) ) { dataCanvas.toBlob( (blob) => { if (!blob) return; const objectUrl = URL.createObjectURL(blob); latestDataUrl.current = objectUrl; cache.set(id, objectUrl); // Store the initial capture in the cache setImageSrc(objectUrl); }, "image/webp" // Using WebP format for smaller size ); } } catch { // Ignore failure to capture } }, [id] ); useEffect(() => { const previewElement = peekElement || componentWindow; if (cache.has(id)) { setImageSrc(cache.get(id) as string); const cacheTimeout = window.setTimeout(() => { if (!previewTimer.current && previewElement) { previewTimer.current = window.setTimeout( () => startRendering(previewElement), document.querySelector(".peekWindow") ? 0 : MILLISECONDS_IN_SECOND / 2 ); } }, CACHE_DELAY); return () => { clearTimeout(cacheTimeout); // Update cache with the latest data URL if available if (latestDataUrl.current) { cache.set(id, latestDataUrl.current); } }; } else if (!previewTimer.current && previewElement) { // Capture the initial frame immediately captureInitialFrame(previewElement); previewTimer.current = window.setTimeout( () => startRendering(previewElement), document.querySelector(".peekWindow") ? 0 : MILLISECONDS_IN_SECOND / 2 ); } return () => { if (previewTimer.current) { clearTimeout(previewTimer.current); previewTimer.current = undefined; } animate.current = false; // Update cache with the latest data URL if available if (latestDataUrl.current) { cache.set(id, latestDataUrl.current); } }; }, [componentWindow, peekElement, id, startRendering, captureInitialFrame]); return imageSrc; }; export default useWindowPeek;