424c22fd87eb81809be1593b7f9bed6b88a55d7b
import { useQuery } from "@tanstack/react-query";
import { apiClient, Insight, InsightType, InsightPeriod } from "@/lib/api";
export const insightKeys = {
all: () => ["insights"] as const,
lists: () => [...insightKeys.all(), "list"] as const,
list: (filters?: InsightFilters) =>
[...insightKeys.lists(), filters] as const,
byType: (type: InsightType) => [...insightKeys.all(), "type", type] as const,
};
interface InsightFilters {
type?: InsightType;
period?: InsightPeriod;
limit?: number;
}
// All insights — for the insights page
export const useInsights = (filters?: InsightFilters) => {
return useQuery({
queryKey: insightKeys.list(filters),
queryFn: () => apiClient.getInsights(filters),
staleTime: 1000 * 60 * 15,
});
// Single type — for widgets that only care about one insight
// Each call caches independently, so mood log page doesn't blow
// away what the insights page already fetched
export const useInsight = (type: InsightType) => {
queryKey: insightKeys.byType(type),
queryFn: () =>
apiClient.getInsights({ type, limit: 1 }).then((res) => res[0] ?? null),