mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-05-09 19:01:47 +00:00
feat(dados-client): adotar react query em leituras do app
This commit is contained in:
32
src/features/insights/hooks/use-saved-insights.ts
Normal file
32
src/features/insights/hooks/use-saved-insights.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
"use client";
|
||||
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { z } from "zod";
|
||||
import type { SavedInsightsRecord } from "@/features/insights/queries";
|
||||
import { fetchJson } from "@/shared/lib/fetch-json";
|
||||
import { InsightsResponseSchema } from "@/shared/lib/schemas/insights";
|
||||
|
||||
const savedInsightsRecordSchema = z.object({
|
||||
insights: InsightsResponseSchema,
|
||||
modelId: z.string().min(1),
|
||||
createdAt: z.string().min(1),
|
||||
});
|
||||
|
||||
export const savedInsightsQueryKey = (period: string) =>
|
||||
["insights", "saved", period] as const;
|
||||
|
||||
export function useSavedInsights(period: string) {
|
||||
return useQuery({
|
||||
queryKey: savedInsightsQueryKey(period),
|
||||
queryFn: async () => {
|
||||
const params = new URLSearchParams({ period });
|
||||
const payload = await fetchJson<SavedInsightsRecord | null>(
|
||||
`/api/insights/saved?${params.toString()}`,
|
||||
);
|
||||
|
||||
return payload === null ? null : savedInsightsRecordSchema.parse(payload);
|
||||
},
|
||||
enabled: Boolean(period),
|
||||
staleTime: 60_000,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user