From e184843855932905facd55bae9847cf0b4e0bd46 Mon Sep 17 00:00:00 2001 From: Ahao Date: Sat, 26 Jul 2025 10:19:02 +0800 Subject: [PATCH] add home page control buttons --- src/pages/home.tsx | 243 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 202 insertions(+), 41 deletions(-) diff --git a/src/pages/home.tsx b/src/pages/home.tsx index 1d9fbaa8..924a6364 100644 --- a/src/pages/home.tsx +++ b/src/pages/home.tsx @@ -1,45 +1,49 @@ -import { useTranslation } from "react-i18next"; +import { BasePage } from "@/components/base"; +import { ClashInfoCard } from "@/components/home/clash-info-card"; +import { ClashModeCard } from "@/components/home/clash-mode-card"; +import { CurrentProxyCard } from "@/components/home/current-proxy-card"; +import { EnhancedCard } from "@/components/home/enhanced-card"; +import { EnhancedTrafficStats } from "@/components/home/enhanced-traffic-stats"; +import { HomeProfileCard } from "@/components/home/home-profile-card"; +import { IpInfoCard } from "@/components/home/ip-info-card"; +import { ProxyTunCard } from "@/components/home/proxy-tun-card"; +import { SystemInfoCard } from "@/components/home/system-info-card"; +import { TestCard } from "@/components/home/test-card"; +import { useProfiles } from "@/hooks/use-profiles"; +import { useVerge } from "@/hooks/use-verge"; +import { entry_lightweight_mode, openWebUrl } from "@/services/cmds"; +import { + DnsOutlined, + HelpOutlineRounded, + HistoryEduOutlined, + RouterOutlined, + SettingsOutlined, + SpeedOutlined, + VisibilityOutlined, + VisibilityOffOutlined, +} from "@mui/icons-material"; import { Box, Button, - IconButton, - useTheme, - keyframes, - Dialog, - DialogTitle, - DialogContent, - DialogActions, - FormGroup, - FormControlLabel, Checkbox, - Tooltip, + Dialog, + DialogActions, + DialogContent, + DialogTitle, + FormControlLabel, + FormGroup, Grid, + IconButton, + keyframes, + Popover, + Tooltip, + Typography, + useTheme, } from "@mui/material"; -import { useVerge } from "@/hooks/use-verge"; -import { useProfiles } from "@/hooks/use-profiles"; -import { - RouterOutlined, - SettingsOutlined, - DnsOutlined, - SpeedOutlined, - HelpOutlineRounded, - HistoryEduOutlined, -} from "@mui/icons-material"; -import { useNavigate } from "react-router-dom"; -import { ProxyTunCard } from "@/components/home/proxy-tun-card"; -import { ClashModeCard } from "@/components/home/clash-mode-card"; -import { EnhancedTrafficStats } from "@/components/home/enhanced-traffic-stats"; -import { useState } from "react"; -import { HomeProfileCard } from "@/components/home/home-profile-card"; -import { EnhancedCard } from "@/components/home/enhanced-card"; -import { CurrentProxyCard } from "@/components/home/current-proxy-card"; -import { BasePage } from "@/components/base"; -import { ClashInfoCard } from "@/components/home/clash-info-card"; -import { SystemInfoCard } from "@/components/home/system-info-card"; import { useLockFn } from "ahooks"; -import { entry_lightweight_mode, openWebUrl } from "@/services/cmds"; -import { TestCard } from "@/components/home/test-card"; -import { IpInfoCard } from "@/components/home/ip-info-card"; +import { useState } from "react"; +import { useTranslation } from "react-i18next"; +import { useNavigate } from "react-router-dom"; // 定义旋转动画 const round = keyframes` @@ -69,6 +73,156 @@ interface HomeCardsSettings { [key: string]: boolean; } +// 首页设置对话框组件接口 +interface HomeCardsControlProps { + homeCards: HomeCardsSettings; + onToggle: (key: string) => void; +} + +// 首页卡片显示控制组件 +const HomeCardsControl = ({ homeCards, onToggle }: HomeCardsControlProps) => { + const { t } = useTranslation(); + const [anchorEl, setAnchorEl] = useState(null); + + const handleClick = (event: React.MouseEvent) => { + setAnchorEl(event.currentTarget); + }; + + const handleClose = () => { + setAnchorEl(null); + }; + + const open = Boolean(anchorEl); + const id = open ? "card-control-popover" : undefined; + + return ( + <> + + + {Object.values(homeCards).every((v) => v) ? ( + + ) : Object.values(homeCards).every((v) => !v) ? ( + + ) : ( + + )} + + + + + + + {t("Card Visibility")} + + + onToggle("profile")} + /> + } + label={t("Profiles")} + /> + onToggle("proxy")} + /> + } + label={t("Current Node")} + /> + onToggle("network")} + /> + } + label={t("Network Settings")} + /> + onToggle("mode")} + /> + } + label={t("Proxy Mode")} + /> + onToggle("traffic")} + /> + } + label={t("Traffic Stats")} + /> + onToggle("test")} + /> + } + label={t("Website Tests")} + /> + onToggle("ip")} + /> + } + label={t("IP Information")} + /> + onToggle("clashinfo")} + /> + } + label={t("Clash Info")} + /> + onToggle("systeminfo")} + /> + } + label={t("System Info")} + /> + + + + + ); +}; + // 首页设置对话框组件接口 interface HomeSettingsDialogProps { open: boolean; @@ -201,7 +355,7 @@ const HomeSettingsDialog = ({ export const HomePage = () => { const { t } = useTranslation(); - const { verge } = useVerge(); + const { verge, patchVerge } = useVerge(); const { current, mutateProfiles } = useProfiles(); const navigate = useNavigate(); const theme = useTheme(); @@ -220,9 +374,17 @@ export const HomePage = () => { systeminfo: true, test: true, ip: true, + info: true, }, ); + // 处理卡片显示切换 + const handleCardToggle = useLockFn(async (key: string) => { + const newCards = { ...homeCards, [key]: !homeCards[key] }; + setHomeCards(newCards); + await patchVerge({ home_cards: newCards }); + }); + // 导航到订阅页面 const goToProfiles = () => { navigate("/profile"); @@ -250,11 +412,7 @@ export const HomePage = () => { // 新增:保存设置时用requestIdleCallback/setTimeout const handleSaveSettings = (newCards: HomeCardsSettings) => { - if (window.requestIdleCallback) { - window.requestIdleCallback(() => setHomeCards(newCards)); - } else { - setTimeout(() => setHomeCards(newCards), 0); - } + setHomeCards(newCards); }; return ( @@ -272,6 +430,9 @@ export const HomePage = () => { + + +