25 lines
798 B
TypeScript
25 lines
798 B
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { api, ApiError } from '@/lib/api'
|
|
import { queryKeys } from '@/lib/queryKeys'
|
|
|
|
export function useReports() {
|
|
const fetchOverview = async (year: number, month: number, type: 'income' | 'expense' = 'expense') => {
|
|
return api.getReportOverview(year, month, type)
|
|
}
|
|
|
|
return { fetchOverview }
|
|
}
|
|
|
|
export function useReportOverview(year: number, month: number, type: 'income' | 'expense') {
|
|
const query = useQuery({
|
|
queryKey: queryKeys.reports.overview(year, month, type),
|
|
queryFn: () => api.getReportOverview(year, month, type),
|
|
})
|
|
|
|
return {
|
|
overview: query.data ?? null,
|
|
loading: query.isLoading,
|
|
error: query.error instanceof ApiError ? query.error.message : query.error ? '获取报表失败' : null,
|
|
}
|
|
}
|