feat: support cache for groups editor

This commit is contained in:
MystiPanda 2024-07-04 18:53:39 +08:00
parent f5ee4fb5b5
commit 60d0b29236
No known key found for this signature in database

View File

@ -9,6 +9,9 @@ import {
import { DeleteForeverRounded, UndoRounded } from "@mui/icons-material"; import { DeleteForeverRounded, UndoRounded } from "@mui/icons-material";
import { useSortable } from "@dnd-kit/sortable"; import { useSortable } from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities"; import { CSS } from "@dnd-kit/utilities";
import { downloadIconCache } from "@/services/cmds";
import { convertFileSrc } from "@tauri-apps/api/tauri";
import { useEffect, useState } from "react";
interface Props { interface Props {
type: "prepend" | "original" | "delete" | "append"; type: "prepend" | "original" | "delete" | "append";
group: IProxyGroupConfig; group: IProxyGroupConfig;
@ -28,6 +31,26 @@ export const GroupItem = (props: Props) => {
transform: null, transform: null,
transition: null, transition: null,
}; };
const [iconCachePath, setIconCachePath] = useState("");
useEffect(() => {
initIconCachePath();
}, [group]);
async function initIconCachePath() {
if (group.icon && group.icon.trim().startsWith("http")) {
const fileName =
group.name.replaceAll(" ", "") + "-" + getFileName(group.icon);
const iconPath = await downloadIconCache(group.icon, fileName);
setIconCachePath(convertFileSrc(iconPath));
}
}
function getFileName(url: string) {
return url.substring(url.lastIndexOf("/") + 1);
}
return ( return (
<ListItem <ListItem
dense dense
@ -49,7 +72,7 @@ export const GroupItem = (props: Props) => {
> >
{group.icon && group.icon?.trim().startsWith("http") && ( {group.icon && group.icon?.trim().startsWith("http") && (
<img <img
src={group.icon} src={iconCachePath === "" ? group.icon : iconCachePath}
width="32px" width="32px"
style={{ style={{
marginRight: "12px", marginRight: "12px",