refactor(icon): extract icon cache logic into reusable hook

This commit is contained in:
Slinetrac
2026-01-01 16:35:36 +08:00
parent f3b9eedcf7
commit 522eccdd0e
4 changed files with 69 additions and 81 deletions
+54
View File
@@ -0,0 +1,54 @@
import { convertFileSrc } from "@tauri-apps/api/core";
import { useMemo } from "react";
import useSWR from "swr";
import { downloadIconCache } from "@/services/cmds";
import { SWR_DEFAULTS } from "@/services/config";
export interface UseIconCacheOptions {
icon?: string | null;
cacheKey?: string;
enabled?: boolean;
}
const getFileNameFromUrl = (url: string) => {
const lastSlashIndex = url.lastIndexOf("/");
return lastSlashIndex >= 0 ? url.slice(lastSlashIndex + 1) : url;
};
export const useIconCache = ({
icon,
cacheKey,
enabled = true,
}: UseIconCacheOptions) => {
const iconValue = icon?.trim() ?? "";
const cacheKeyValue = cacheKey?.trim() ?? "";
const swrKey = useMemo(() => {
if (!enabled || !iconValue.startsWith("http") || cacheKeyValue === "") {
return null;
}
return ["icon-cache", iconValue, cacheKeyValue] as const;
}, [enabled, iconValue, cacheKeyValue]);
const { data } = useSWR(
swrKey,
async () => {
try {
const fileName = `${cacheKeyValue}-${getFileNameFromUrl(iconValue)}`;
const iconPath = await downloadIconCache(iconValue, fileName);
return convertFileSrc(iconPath);
} catch {
return "";
}
},
SWR_DEFAULTS,
);
if (!swrKey) {
return "";
}
return data ?? "";
};