Files
NapCatQQ/packages/napcat-webui-frontend/src/hooks/use-preload-images.ts
T
手瓜一十雪 ed19c52f25 refactor: 整体重构 (#1381)
* feat: pnpm new

* Refactor build and release workflows, update dependencies

Switch build scripts and workflows from npm to pnpm, update build and artifact paths, and simplify release workflow by removing version detection and changelog steps. Add new dependencies (silk-wasm, express, ws, node-pty-prebuilt-multiarch), update exports in package.json files, and add vite config for napcat-framework. Also, rename manifest.json for framework package and fix static asset copying in shell build config.
2025-11-13 15:39:42 +08:00

70 lines
1.7 KiB
TypeScript

import { useEffect, useRef, useState } from 'react';
// 全局图片缓存
const imageCache = new Map<string, HTMLImageElement>();
export function usePreloadImages (urls: string[]) {
const [loadedUrls, setLoadedUrls] = useState<Record<string, boolean>>({});
const [isLoading, setIsLoading] = useState(true);
const isMounted = useRef(true);
useEffect(() => {
isMounted.current = true;
// 检查是否所有图片都已缓存
const allCached = urls.every((url) => imageCache.has(url));
if (allCached) {
setLoadedUrls(urls.reduce((acc, url) => ({ ...acc, [url]: true }), {}));
setIsLoading(false);
return;
}
setIsLoading(true);
const loadedImages: Record<string, boolean> = {};
let pendingCount = urls.length;
urls.forEach((url) => {
// 如果已经缓存,直接标记为已加载
if (imageCache.has(url)) {
loadedImages[url] = true;
pendingCount--;
if (pendingCount === 0) {
setLoadedUrls(loadedImages);
setIsLoading(false);
}
return;
}
const img = new Image();
img.onload = () => {
if (!isMounted.current) return;
loadedImages[url] = true;
imageCache.set(url, img);
pendingCount--;
if (pendingCount === 0) {
setLoadedUrls(loadedImages);
setIsLoading(false);
}
};
img.onerror = () => {
if (!isMounted.current) return;
loadedImages[url] = false;
pendingCount--;
if (pendingCount === 0) {
setLoadedUrls(loadedImages);
setIsLoading(false);
}
};
img.src = url;
});
return () => {
isMounted.current = false;
};
}, [urls]);
return { loadedUrls, isLoading };
}