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:
Jason
2025-10-17 19:18:10 +08:00
parent d9c56511b1
commit 0cff882a3f
3 changed files with 27 additions and 49 deletions

View File

@@ -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分钟
});
};