refactor: improve code quality and consistency
Changes: 1. Remove unused variable in useSettings.ts (readPersistedLanguage) 2. Replace manual state management with React Query in UsageFooter - Create useUsageQuery hook with 5-minute cache - Simplify component from 227 lines to 81 lines (-64%) - Improve consistency with project's React Query pattern - Enable automatic refetch and error handling
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import React, { useEffect, useState, useRef } from "react";
|
||||
import React from "react";
|
||||
import { RefreshCw, AlertCircle } from "lucide-react";
|
||||
import { usageApi, type AppType } from "@/lib/api";
|
||||
import { UsageResult, UsageData } from "../types";
|
||||
import { type AppType } from "@/lib/api";
|
||||
import { useUsageQuery } from "@/lib/query/queries";
|
||||
import { UsageData } from "../types";
|
||||
|
||||
interface UsageFooterProps {
|
||||
providerId: string;
|
||||
@@ -14,47 +15,11 @@ const UsageFooter: React.FC<UsageFooterProps> = ({
|
||||
appType,
|
||||
usageEnabled,
|
||||
}) => {
|
||||
const [usage, setUsage] = useState<UsageResult | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
// 记录上次请求的关键参数,防止重复请求
|
||||
const lastFetchParamsRef = useRef<string>("");
|
||||
|
||||
const fetchUsage = async () => {
|
||||
// 防止并发请求
|
||||
if (loading) return;
|
||||
|
||||
setLoading(true);
|
||||
try {
|
||||
const result = await usageApi.query(providerId, appType);
|
||||
setUsage(result);
|
||||
} catch (error: any) {
|
||||
console.error("查询用量失败:", error);
|
||||
setUsage({
|
||||
success: false,
|
||||
error: error?.message || "查询失败",
|
||||
});
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (usageEnabled) {
|
||||
// 生成当前参数的唯一标识(包含 usageEnabled 状态)
|
||||
const currentParams = `${providerId}-${appType}-${usageEnabled}`;
|
||||
|
||||
// 只有参数真正变化时才发起请求
|
||||
if (currentParams !== lastFetchParamsRef.current) {
|
||||
lastFetchParamsRef.current = currentParams;
|
||||
fetchUsage();
|
||||
}
|
||||
} else {
|
||||
// 如果禁用了,清空记录和数据
|
||||
lastFetchParamsRef.current = "";
|
||||
setUsage(null);
|
||||
}
|
||||
}, [providerId, usageEnabled, appType]);
|
||||
const { data: usage, isLoading: loading, refetch } = useUsageQuery(
|
||||
providerId,
|
||||
appType,
|
||||
usageEnabled,
|
||||
);
|
||||
|
||||
// 只在启用用量查询且有数据时显示
|
||||
if (!usageEnabled || !usage) return null;
|
||||
@@ -71,7 +36,7 @@ const UsageFooter: React.FC<UsageFooterProps> = ({
|
||||
|
||||
{/* 刷新按钮 */}
|
||||
<button
|
||||
onClick={() => fetchUsage()}
|
||||
onClick={() => refetch()}
|
||||
disabled={loading}
|
||||
className="p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors disabled:opacity-50 flex-shrink-0"
|
||||
title="刷新用量"
|
||||
@@ -96,7 +61,7 @@ const UsageFooter: React.FC<UsageFooterProps> = ({
|
||||
套餐用量
|
||||
</span>
|
||||
<button
|
||||
onClick={() => fetchUsage()}
|
||||
onClick={() => refetch()}
|
||||
disabled={loading}
|
||||
className="p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors disabled:opacity-50"
|
||||
title="刷新用量"
|
||||
|
||||
@@ -69,7 +69,6 @@ export function useSettings(): UseSettingsResult {
|
||||
initialLanguage,
|
||||
updateSettings,
|
||||
resetSettings: resetForm,
|
||||
readPersistedLanguage,
|
||||
syncLanguage,
|
||||
} = useSettingsForm();
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useQuery, type UseQueryResult } from "@tanstack/react-query";
|
||||
import { providersApi, settingsApi, type AppType } from "@/lib/api";
|
||||
import type { Provider, Settings } from "@/types";
|
||||
import { providersApi, settingsApi, usageApi, type AppType } from "@/lib/api";
|
||||
import type { Provider, Settings, UsageResult } from "@/types";
|
||||
|
||||
const sortProviders = (
|
||||
providers: Record<string, Provider>,
|
||||
@@ -77,3 +77,17 @@ export const useSettingsQuery = (): UseQueryResult<Settings> => {
|
||||
queryFn: async () => settingsApi.get(),
|
||||
});
|
||||
};
|
||||
|
||||
export const useUsageQuery = (
|
||||
providerId: string,
|
||||
appType: AppType,
|
||||
enabled: boolean = true,
|
||||
): UseQueryResult<UsageResult> => {
|
||||
return useQuery({
|
||||
queryKey: ["usage", providerId, appType],
|
||||
queryFn: async () => usageApi.query(providerId, appType),
|
||||
enabled: enabled && !!providerId,
|
||||
refetchOnWindowFocus: false,
|
||||
staleTime: 5 * 60 * 1000, // 5分钟
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user