feat: auto-refresh rule data when switching to rules page

This commit is contained in:
wonfen 2025-04-24 10:24:29 +08:00
parent 0bb042e085
commit 4d37e6870c
3 changed files with 18 additions and 4 deletions

View File

@ -31,6 +31,7 @@
- 系统代理 Bypass 设置
- Windows 下使用 Startup 文件夹的方式实现开机自启,解决管理员模式下开机自启的各种问题
- 增加 IP 信息请求重试机制,减少 Network Error 发生的情况
- 切换到规则页面时自动刷新规则数据
## v2.2.3

View File

@ -1,4 +1,4 @@
import { useState, useMemo, useRef } from "react";
import { useState, useMemo, useRef, useEffect } from "react";
import { useTranslation } from "react-i18next";
import { Virtuoso, VirtuosoHandle } from "react-virtuoso";
import { Box } from "@mui/material";
@ -9,15 +9,28 @@ import { BaseSearchBox } from "@/components/base/base-search-box";
import { useTheme } from "@mui/material/styles";
import { ScrollTopButton } from "@/components/layout/scroll-top-button";
import { useAppData } from "@/providers/app-data-provider";
import { useVisibility } from "@/hooks/use-visibility";
const RulesPage = () => {
const { t } = useTranslation();
const { rules = [] } = useAppData();
const { rules = [], refreshRules, refreshRuleProviders } = useAppData();
const theme = useTheme();
const isDark = theme.palette.mode === "dark";
const [match, setMatch] = useState(() => (_: string) => true);
const virtuosoRef = useRef<VirtuosoHandle>(null);
const [showScrollTop, setShowScrollTop] = useState(false);
const pageVisible = useVisibility();
// 在组件挂载时和页面获得焦点时刷新规则数据
useEffect(() => {
refreshRules();
refreshRuleProviders();
if (pageVisible) {
refreshRules();
refreshRuleProviders();
}
}, [refreshRules, refreshRuleProviders, pageVisible]);
const filteredRules = useMemo(() => {
return rules.filter((item) => match(item.payload));

View File

@ -117,12 +117,12 @@ export const AppDataProvider = ({ children }: { children: React.ReactNode }) =>
}
);
// 高频率更新数据 (1秒)
// 高频率更新数据 (2秒)
const { data: uptimeData } = useSWR(
"appUptime",
getAppUptime,
{
refreshInterval: 1000,
refreshInterval: 2000,
revalidateOnFocus: false,
suspense: false
}