import useSWR, { mutate } from "swr"; import { useLockFn } from "ahooks"; import { useEffect, useMemo, useRef, useState } from "react"; import { useSetRecoilState } from "recoil"; import { Box, Button, Grid, IconButton, Stack, TextField } from "@mui/material"; import { CachedRounded } from "@mui/icons-material"; import { useTranslation } from "react-i18next"; import { getProfiles, patchProfile, importProfile, enhanceProfiles, getRuntimeLogs, deleteProfile, } from "@/services/cmds"; import { closeAllConnections, getProxies, updateProxy } from "@/services/api"; import { atomCurrentProfile } from "@/services/states"; import { BasePage, Notice } from "@/components/base"; import { ProfileViewer, ProfileViewerRef, } from "@/components/profile/profile-viewer"; import { ProfileItem } from "@/components/profile/profile-item"; import { ProfileMore } from "@/components/profile/profile-more"; import { useProfiles } from "@/hooks/use-profiles"; const ProfilePage = () => { const { t } = useTranslation(); const [url, setUrl] = useState(""); const [disabled, setDisabled] = useState(false); const setCurrentProfile = useSetRecoilState(atomCurrentProfile); const { profiles = {}, patchProfiles, mutateProfiles } = useProfiles(); const { data: chainLogs = {}, mutate: mutateLogs } = useSWR( "getRuntimeLogs", getRuntimeLogs ); const chain = profiles.chain || []; const viewerRef = useRef(null); // distinguish type const { regularItems, enhanceItems } = useMemo(() => { const items = profiles.items || []; const chain = profiles.chain || []; const type1 = ["local", "remote"]; const type2 = ["merge", "script"]; const regularItems = items.filter((i) => type1.includes(i.type!)); const restItems = items.filter((i) => type2.includes(i.type!)); const restMap = Object.fromEntries(restItems.map((i) => [i.uid, i])); const enhanceItems = chain .map((i) => restMap[i]!) .concat(restItems.filter((i) => !chain.includes(i.uid))); return { regularItems, enhanceItems }; }, [profiles]); // sync selected proxy useEffect(() => { if (profiles.current == null) return; const current = profiles.current; const profile = regularItems.find((p) => p.uid === current); setCurrentProfile(current); if (!profile) return; setTimeout(async () => { const proxiesData = await getProxies(); mutate("getProxies", proxiesData); // init selected array const { selected = [] } = profile; const selectedMap = Object.fromEntries( selected.map((each) => [each.name!, each.now!]) ); let hasChange = false; const { global, groups } = proxiesData; [global, ...groups].forEach((group) => { const { type, name, now } = group; if (type !== "Selector" && type !== "Fallback") return; if (!now || selectedMap[name] === now) return; if (selectedMap[name] == null) { selectedMap[name] = now!; } else { hasChange = true; updateProxy(name, selectedMap[name]); } }); // update profile selected list profile.selected = Object.entries(selectedMap).map(([name, now]) => ({ name, now, })); patchProfile(current!, { selected: profile.selected }); // update proxies cache if (hasChange) mutate("getProxies", getProxies()); }, 100); }, [profiles, regularItems]); const onImport = async () => { if (!url) return; setUrl(""); setDisabled(true); try { await importProfile(url); Notice.success("Successfully import profile."); getProfiles().then((newProfiles) => { mutate("getProfiles", newProfiles); const remoteItem = newProfiles.items?.find((e) => e.type === "remote"); if (!newProfiles.current && remoteItem) { const current = remoteItem.uid; patchProfiles({ current }); mutateProfiles(); mutateLogs(); } }); } catch (err: any) { Notice.error(err.message || err.toString()); } finally { setDisabled(false); } }; const onSelect = useLockFn(async (current: string, force: boolean) => { if (!force && current === profiles.current) return; try { await patchProfiles({ current }); setCurrentProfile(current); mutateLogs(); closeAllConnections(); Notice.success("Refresh clash config", 1000); } catch (err: any) { Notice.error(err?.message || err.toString(), 4000); } }); const onEnhance = useLockFn(async () => { try { await enhanceProfiles(); mutateLogs(); Notice.success("Refresh clash config", 1000); } catch (err: any) { Notice.error(err.message || err.toString(), 3000); } }); const onEnable = useLockFn(async (uid: string) => { if (chain.includes(uid)) return; const newChain = [...chain, uid]; await patchProfiles({ chain: newChain }); mutateLogs(); }); const onDisable = useLockFn(async (uid: string) => { if (!chain.includes(uid)) return; const newChain = chain.filter((i) => i !== uid); await patchProfiles({ chain: newChain }); mutateLogs(); }); const onDelete = useLockFn(async (uid: string) => { try { await onDisable(uid); await deleteProfile(uid); mutateProfiles(); mutateLogs(); } catch (err: any) { Notice.error(err?.message || err.toString()); } }); const onMoveTop = useLockFn(async (uid: string) => { if (!chain.includes(uid)) return; const newChain = [uid].concat(chain.filter((i) => i !== uid)); await patchProfiles({ chain: newChain }); mutateLogs(); }); const onMoveEnd = useLockFn(async (uid: string) => { if (!chain.includes(uid)) return; const newChain = chain.filter((i) => i !== uid).concat([uid]); await patchProfiles({ chain: newChain }); mutateLogs(); }); return ( } > setUrl(e.target.value)} sx={{ input: { py: 0.65, px: 1.25 } }} placeholder={t("Profile URL")} /> {regularItems.map((item) => ( onSelect(item.uid, f)} onEdit={() => viewerRef.current?.edit(item)} /> ))} {enhanceItems.length > 0 && ( {enhanceItems.map((item) => ( onEnable(item.uid)} onDisable={() => onDisable(item.uid)} onDelete={() => onDelete(item.uid)} onMoveTop={() => onMoveTop(item.uid)} onMoveEnd={() => onMoveEnd(item.uid)} onEdit={() => viewerRef.current?.edit(item)} /> ))} )} mutateProfiles()} /> ); }; export default ProfilePage;