// src/hooks/queries/useWatchedItemsQuery.ts import { useQuery } from '@tanstack/react-query'; import * as apiClient from '../../services/apiClient'; import type { MasterGroceryItem } from '../../types'; /** * Query hook for fetching the user's watched items. * * This hook is automatically disabled when the user is not authenticated, * and the cached data is invalidated when the user logs out. * * @param enabled - Whether the query should run (typically based on auth status) * @returns Query result with watched items data, loading state, and error state * * @example * ```tsx * const { data: watchedItems, isLoading, error } = useWatchedItemsQuery(!!user); * ``` */ export const useWatchedItemsQuery = (enabled: boolean) => { return useQuery({ queryKey: ['watched-items'], queryFn: async (): Promise => { const response = await apiClient.fetchWatchedItems(); if (!response.ok) { const error = await response.json().catch(() => ({ message: `Request failed with status ${response.status}`, })); throw new Error(error.message || 'Failed to fetch watched items'); } return response.json(); }, enabled, // Keep data fresh for 1 minute since users actively manage watched items staleTime: 1000 * 60, }); };