Files
clash-proxy/src/components/proxy/proxy-render.tsx

236 lines
6.4 KiB
TypeScript
Raw Normal View History

2022-11-23 18:27:57 +08:00
import {
alpha,
Box,
ListItemText,
2022-12-14 15:16:49 +08:00
ListItemButton,
2022-11-23 18:27:57 +08:00
Typography,
styled,
} from "@mui/material";
import {
ExpandLessRounded,
ExpandMoreRounded,
InboxRounded,
} from "@mui/icons-material";
import { HeadState } from "./use-head-state";
import { ProxyHead } from "./proxy-head";
import { ProxyItem } from "./proxy-item";
import { ProxyItemMini } from "./proxy-item-mini";
2022-11-23 18:27:57 +08:00
import type { IRenderItem } from "./use-render-list";
2024-02-24 12:38:17 +08:00
import { useVerge } from "@/hooks/use-verge";
2024-06-07 12:27:37 +08:00
import { useThemeMode } from "@/services/states";
import { useEffect, useMemo, useState } from "react";
2024-03-15 16:42:17 +08:00
import { convertFileSrc } from "@tauri-apps/api/tauri";
import { downloadIconCache } from "@/services/cmds";
2022-11-23 18:27:57 +08:00
interface RenderProps {
item: IRenderItem;
indent: boolean;
onLocation: (group: IProxyGroupItem) => void;
onCheckAll: (groupName: string) => void;
onHeadState: (groupName: string, patch: Partial<HeadState>) => void;
onChangeProxy: (group: IProxyGroupItem, proxy: IProxyItem) => void;
}
export const ProxyRender = (props: RenderProps) => {
const { indent, item, onLocation, onCheckAll, onHeadState, onChangeProxy } =
props;
const { type, group, headState, proxy, proxyCol } = item;
2024-02-24 12:38:17 +08:00
const { verge } = useVerge();
const enable_group_icon = verge?.enable_group_icon ?? true;
2024-06-07 12:27:37 +08:00
const mode = useThemeMode();
const isDark = mode === "light" ? false : true;
const itembackgroundcolor = isDark ? "#282A36" : "#ffffff";
2024-03-15 16:42:17 +08:00
const [iconCachePath, setIconCachePath] = useState("");
useEffect(() => {
initIconCachePath();
}, [group]);
async function initIconCachePath() {
if (group.icon && group.icon.trim().startsWith("http")) {
2024-03-21 20:29:16 +08:00
const fileName =
group.name.replaceAll(" ", "") + "-" + getFileName(group.icon);
2024-03-15 16:42:17 +08:00
const iconPath = await downloadIconCache(group.icon, fileName);
setIconCachePath(convertFileSrc(iconPath));
}
}
function getFileName(url: string) {
return url.substring(url.lastIndexOf("/") + 1);
}
2022-11-23 18:27:57 +08:00
2024-01-11 12:34:05 +08:00
if (type === 0 && !group.hidden) {
2022-11-23 18:27:57 +08:00
return (
2022-12-14 15:16:49 +08:00
<ListItemButton
2022-11-23 18:27:57 +08:00
dense
style={{
background: itembackgroundcolor,
2024-03-10 22:13:25 +08:00
height: "100%",
2024-03-10 07:00:24 +08:00
margin: "8px 8px",
borderRadius: "8px",
}}
2022-11-23 18:27:57 +08:00
onClick={() => onHeadState(group.name, { open: !headState?.open })}
>
2024-02-24 12:38:17 +08:00
{enable_group_icon &&
group.icon &&
group.icon.trim().startsWith("http") && (
<img
2024-03-15 16:42:17 +08:00
src={iconCachePath === "" ? group.icon : iconCachePath}
2024-03-20 20:38:45 +08:00
width="32px"
style={{ marginRight: "12px", borderRadius: "6px" }}
2024-02-24 12:38:17 +08:00
/>
)}
{enable_group_icon &&
group.icon &&
group.icon.trim().startsWith("data") && (
<img
src={group.icon}
2024-03-20 20:38:45 +08:00
width="32px"
style={{ marginRight: "12px", borderRadius: "6px" }}
2024-02-24 12:38:17 +08:00
/>
)}
{enable_group_icon &&
group.icon &&
group.icon.trim().startsWith("<svg") && (
<img
src={`data:image/svg+xml;base64,${btoa(group.icon)}`}
2024-03-20 20:38:45 +08:00
width="32px"
2024-02-24 12:38:17 +08:00
/>
)}
2022-11-23 18:27:57 +08:00
<ListItemText
primary={<StyledPrimary>{group.name}</StyledPrimary>}
2022-11-23 18:27:57 +08:00
secondary={
<ListItemTextChild
2022-11-23 18:27:57 +08:00
sx={{
overflow: "hidden",
display: "flex",
alignItems: "center",
pt: "2px",
}}
>
2024-03-10 21:56:49 +08:00
<Box sx={{ marginTop: "2px" }}>
<StyledTypeBox>{group.type}</StyledTypeBox>
2024-03-10 22:13:25 +08:00
<StyledSubtitle sx={{ color: "text.secondary" }}>
{group.now}
</StyledSubtitle>
2024-03-10 21:56:49 +08:00
</Box>
</ListItemTextChild>
2022-11-23 18:27:57 +08:00
}
secondaryTypographyProps={{
sx: { display: "flex", alignItems: "center", color: "#ccc" },
2022-11-23 18:27:57 +08:00
}}
/>
{headState?.open ? <ExpandLessRounded /> : <ExpandMoreRounded />}
2022-12-14 15:16:49 +08:00
</ListItemButton>
2022-11-23 18:27:57 +08:00
);
}
2024-01-11 12:34:05 +08:00
if (type === 1 && !group.hidden) {
2022-11-23 18:27:57 +08:00
return (
<ProxyHead
2024-03-10 11:12:54 +08:00
sx={{ pl: 2, pr: 3, mt: indent ? 1 : 0.5, mb: 1 }}
2022-11-23 18:27:57 +08:00
groupName={group.name}
headState={headState!}
onLocation={() => onLocation(group)}
onCheckDelay={() => onCheckAll(group.name)}
onHeadState={(p) => onHeadState(group.name, p)}
/>
);
}
2024-01-11 12:34:05 +08:00
if (type === 2 && !group.hidden) {
2022-11-23 18:27:57 +08:00
return (
<ProxyItem
group={group}
2022-11-23 18:27:57 +08:00
proxy={proxy!}
selected={group.now === proxy?.name}
showType={headState?.showType}
2024-03-10 13:19:28 +08:00
sx={{ py: 0, pl: 2 }}
2022-11-23 18:27:57 +08:00
onClick={() => onChangeProxy(group, proxy!)}
/>
);
}
2024-01-11 12:34:05 +08:00
if (type === 3 && !group.hidden) {
2022-11-23 18:27:57 +08:00
return (
<Box
sx={{
py: 2,
2024-03-10 11:12:54 +08:00
pl: 0,
2022-11-23 18:27:57 +08:00
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
}}
>
<InboxRounded sx={{ fontSize: "2.5em", color: "inherit" }} />
<Typography sx={{ color: "inherit" }}>No Proxies</Typography>
</Box>
);
}
2024-01-11 12:34:05 +08:00
if (type === 4 && !group.hidden) {
const proxyColItemsMemo = useMemo(() => {
return proxyCol?.map((proxy) => (
<ProxyItemMini
key={item.key + proxy.name}
group={group}
proxy={proxy!}
selected={group.now === proxy.name}
showType={headState?.showType}
onClick={() => onChangeProxy(group, proxy!)}
/>
));
}, [proxyCol, group, headState]);
return (
<Box
sx={{
2022-12-14 15:07:51 +08:00
height: 56,
display: "grid",
gap: 1,
2024-03-10 11:12:54 +08:00
pl: 2,
pr: 2,
pb: 1,
gridTemplateColumns: `repeat(${item.col! || 2}, 1fr)`,
}}
>
{proxyColItemsMemo}
</Box>
);
}
2022-11-23 18:27:57 +08:00
return null;
};
const StyledPrimary = styled("span")`
2024-03-10 22:13:25 +08:00
font-size: 15px;
font-weight: 700;
line-height: 1.5;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
`;
2022-11-23 18:27:57 +08:00
const StyledSubtitle = styled("span")`
2024-03-10 22:13:25 +08:00
font-size: 13px;
2022-11-23 18:27:57 +08:00
overflow: hidden;
2024-03-10 21:52:40 +08:00
color: text.secondary;
2022-11-23 18:27:57 +08:00
text-overflow: ellipsis;
white-space: nowrap;
`;
const ListItemTextChild = styled("span")`
display: block;
`;
const StyledTypeBox = styled(ListItemTextChild)(({ theme }) => ({
2022-11-23 18:27:57 +08:00
display: "inline-block",
border: "1px solid #ccc",
borderColor: alpha(theme.palette.primary.main, 0.5),
color: alpha(theme.palette.primary.main, 0.8),
borderRadius: 4,
fontSize: 10,
padding: "0 4px",
lineHeight: 1.5,
marginRight: "8px",
2022-11-23 18:27:57 +08:00
}));