mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-28 07:14:40 +08:00
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:
@@ -8,6 +8,7 @@ import { Box, Button, IconButton, Skeleton, Typography } from "@mui/material";
|
||||
import { memo, useCallback, useEffect, useRef, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { useAppData } from "@/providers/app-data-context";
|
||||
import { getIpInfo } from "@/services/api";
|
||||
|
||||
import { EnhancedCard } from "./enhanced-card";
|
||||
@@ -55,6 +56,7 @@ const getCountryFlag = (countryCode: string) => {
|
||||
// IP信息卡片组件
|
||||
export const IpInfoCard = () => {
|
||||
const { t } = useTranslation();
|
||||
const { clashConfig } = useAppData();
|
||||
const [ipInfo, setIpInfo] = useState<any>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState("");
|
||||
@@ -90,6 +92,20 @@ export const IpInfoCard = () => {
|
||||
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 {
|
||||
setLoading(true);
|
||||
const data = await getIpInfo();
|
||||
@@ -113,11 +129,13 @@ export const IpInfoCard = () => {
|
||||
? err.message
|
||||
: t("home.components.ipInfo.errors.load"),
|
||||
);
|
||||
lastFetchRef.current = Date.now();
|
||||
setCountdown(IP_REFRESH_SECONDS);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
},
|
||||
[t],
|
||||
[t, clashConfig],
|
||||
);
|
||||
|
||||
// 组件加载时获取IP信息并启动基于上次请求时间的倒计时
|
||||
|
||||
Reference in New Issue
Block a user