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,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