// src/hooks/queries/useSuggestedCorrectionsQuery.ts import { useQuery } from '@tanstack/react-query'; import { getSuggestedCorrections } from '../../services/apiClient'; import { queryKeys } from '../../config/queryKeys'; import type { SuggestedCorrection } from '../../types'; /** * Query hook for fetching user-submitted corrections (admin feature). * * Returns a list of pending corrections that need admin review/approval. * * Uses TanStack Query for automatic caching and refetching (ADR-0005 Phase 5). * * @returns TanStack Query result with SuggestedCorrection[] data */ export const useSuggestedCorrectionsQuery = () => { return useQuery({ queryKey: queryKeys.suggestedCorrections(), queryFn: async (): Promise => { const response = await getSuggestedCorrections(); if (!response.ok) { const error = await response.json().catch(() => ({ message: `Request failed with status ${response.status}`, })); throw new Error(error.message || 'Failed to fetch suggested corrections'); } 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; }, staleTime: 1000 * 60, // 1 minute - corrections change moderately }); };