fix(ip-info-card): handle offline state and clashConfig absence in IP info fetching (#6085)

* fix(ip-info-card): handle offline state and clashConfig absence in IP info fetching

* fix: eslint errors
This commit is contained in:
Tunglies 2026-01-25 15:12:17 +08:00 committed by GitHub
parent 93e7ac1bce
commit 0c6631ebb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,6 +8,7 @@ import { Box, Button, IconButton, Skeleton, Typography } from "@mui/material";
import { memo, useCallback, useEffect, useRef, useState } from "react"; import { memo, useCallback, useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useAppData } from "@/providers/app-data-context";
import { getIpInfo } from "@/services/api"; import { getIpInfo } from "@/services/api";
import { EnhancedCard } from "./enhanced-card"; import { EnhancedCard } from "./enhanced-card";
@ -55,6 +56,7 @@ const getCountryFlag = (countryCode: string) => {
// IP信息卡片组件 // IP信息卡片组件
export const IpInfoCard = () => { export const IpInfoCard = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const { clashConfig } = useAppData();
const [ipInfo, setIpInfo] = useState<any>(null); const [ipInfo, setIpInfo] = useState<any>(null);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [error, setError] = useState(""); const [error, setError] = useState("");
@ -90,6 +92,20 @@ export const IpInfoCard = () => {
console.warn("Failed to read IP info from sessionStorage:", e); console.warn("Failed to read IP info from sessionStorage:", e);
} }
if (typeof navigator !== "undefined" && !navigator.onLine) {
setLoading(false);
lastFetchRef.current = Date.now();
setCountdown(IP_REFRESH_SECONDS);
return;
}
if (!clashConfig) {
setLoading(false);
lastFetchRef.current = Date.now();
setCountdown(IP_REFRESH_SECONDS);
return;
}
try { try {
setLoading(true); setLoading(true);
const data = await getIpInfo(); const data = await getIpInfo();
@ -113,11 +129,13 @@ export const IpInfoCard = () => {
? err.message ? err.message
: t("home.components.ipInfo.errors.load"), : t("home.components.ipInfo.errors.load"),
); );
lastFetchRef.current = Date.now();
setCountdown(IP_REFRESH_SECONDS);
} finally { } finally {
setLoading(false); setLoading(false);
} }
}, },
[t], [t, clashConfig],
); );
// 组件加载时获取IP信息并启动基于上次请求时间的倒计时 // 组件加载时获取IP信息并启动基于上次请求时间的倒计时