"use client"; import { useState, useEffect } from "react"; import { RefreshCw, AlertCircle } from "lucide-react"; import { useTranslation } from "react-i18next"; import { useTokenRefresh } from "@/hooks/use-token-refresh"; import { Button } from "@/components/ui/button"; import { ERROR_COLORS } from "@/stores/error-store"; import { cn } from "@/lib/utils"; /** * Token 续签状态指示器组件 * * 当 Token 即将过期或正在刷新时显示提示 */ export function TokenRefreshIndicator(): React.ReactElement | null { const { isTokenExpiringSoon, getTokenRemainingTime, refreshToken } = useTokenRefresh(); const { t } = useTranslation("player"); const [isRefreshing, setIsRefreshing] = useState(false); const [showWarning, setShowWarning] = useState(false); const [remainingSeconds, setRemainingSeconds] = useState(null); // 检测 Token 状态 useEffect(() => { const checkToken = (): void => { const remaining = getTokenRemainingTime(); if (remaining > 0 && remaining < 120000) { // 2 分钟内显示 setShowWarning(true); setRemainingSeconds(Math.floor(remaining / 1000)); } else { setShowWarning(false); setRemainingSeconds(null); } }; checkToken(); const interval = setInterval(checkToken, 10000); // 每 10 秒检查 return () => clearInterval(interval); }, [getTokenRemainingTime, isTokenExpiringSoon]); // 手动刷新 const handleRefresh = async (): Promise => { setIsRefreshing(true); try { await refreshToken(); } finally { setIsRefreshing(false); } }; if (!showWarning) { return null; } const isCritical = remainingSeconds !== null && remainingSeconds < 60; return (
{isCritical ? t("token.critical") : t("token.warning")} {remainingSeconds !== null && ( <> {t("token.remaining", { time: `${Math.floor(remainingSeconds / 60)}:${String( remainingSeconds % 60, ).padStart(2, "0")}`, })}{" "} )} {t("token.autoRenewing")}
); }