Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 37x 327x 219x 219x 3x 3x 114x 114x 1x 113x | // src/hooks/queries/useMasterItemsQuery.ts
import { useQuery } from '@tanstack/react-query';
import * as apiClient from '../../services/apiClient';
import { queryKeys } from '../../config/queryKeys';
import type { MasterGroceryItem } from '../../types';
/**
* Query hook for fetching all master grocery items.
*
* Master items are the canonical list of grocery items that users can watch
* and that flyer items are mapped to. This data changes infrequently, so it's
* cached with a longer stale time.
*
* @returns Query result with master items data, loading state, and error state
*
* @example
* ```tsx
* const { data: masterItems, isLoading, error } = useMasterItemsQuery();
* ```
*/
export const useMasterItemsQuery = () => {
return useQuery({
queryKey: queryKeys.masterItems(),
queryFn: async (): Promise<MasterGroceryItem[]> => {
const response = await apiClient.fetchMasterItems();
if (!response.ok) {
const error = await response.json().catch(() => ({
message: `Request failed with status ${response.status}`,
}));
throw new Error(error.message || 'Failed to fetch master items');
}
const json = await response.json();
// ADR-028: API returns { success: true, data: [...] }
// If success is false or data is not an array, return empty array to prevent .map() errors
if (!json.success || !Array.isArray(json.data)) {
return [];
}
return json.data;
},
// Master items change infrequently, keep data fresh for 10 minutes
staleTime: 1000 * 60 * 10,
// Cache for 30 minutes
gcTime: 1000 * 60 * 30,
});
};
|