/** * useInfoCenterActions Hook * * TanStack Query mutations for InfoCenter CRUD operations. * Provides automatic cache invalidation and optimistic updates. * * @layer Presentation */ import { useMutation, useQueryClient } from '@tanstack/react-query'; import { queryKeys } from '@/lib/query-keys'; import { infoCenterApi } from '@/infrastructure/http/api/info-center'; import type { DiscoveredPage } from '@/domain/entities/DiscoveredPage'; import type { PageElement } from '@/domain/entities/PageAnalysis'; /** * useUpdatePage Hook * * Mutation for updating discovered page metadata. */ export function useUpdatePage() { const queryClient = useQueryClient(); return useMutation({ mutationFn: async ({ id, tenantId, updates, }: { id: string; tenantId: string; updates: Partial; }) => { return await infoCenterApi.updatePage(id, tenantId, updates); }, onSuccess: (_, variables) => { // Invalidate pages query for this tenant queryClient.invalidateQueries({ queryKey: queryKeys.infoCenter.pages(variables.tenantId), }); }, }); } /** * useDeletePage Hook * * Mutation for deleting a discovered page. */ export function useDeletePage() { const queryClient = useQueryClient(); return useMutation({ mutationFn: async ({ id, tenantId, }: { id: string; tenantId: string; }) => { return await infoCenterApi.deletePage(id, tenantId); }, onSuccess: (_, variables) => { // Invalidate pages query for this tenant queryClient.invalidateQueries({ queryKey: queryKeys.infoCenter.pages(variables.tenantId), }); // Also invalidate analyses (page deletion may cascade) queryClient.invalidateQueries({ queryKey: queryKeys.infoCenter.analyses(variables.tenantId), }); }, }); } /** * useRetriggerAnalysis Hook * * Mutation for re-analyzing a page (rescan). */ export function useRetriggerAnalysis() { const queryClient = useQueryClient(); return useMutation({ mutationFn: async ({ pageId, tenantId, priority = 'NORMAL', createNewVersion = true, }: { pageId: string; tenantId: string; priority?: 'LOW' | 'NORMAL' | 'HIGH'; createNewVersion?: boolean; }) => { return await infoCenterApi.retriggerAnalysis(pageId, tenantId, priority, createNewVersion); }, onSuccess: (_, variables) => { // Invalidate pages and analyses queries queryClient.invalidateQueries({ queryKey: queryKeys.infoCenter.pages(variables.tenantId), }); queryClient.invalidateQueries({ queryKey: queryKeys.infoCenter.analyses(variables.tenantId), }); }, }); } /** * useUpdateElement Hook * * Mutation for updating element properties in a page analysis. */ export function useUpdateElement() { const queryClient = useQueryClient(); return useMutation({ mutationFn: async ({ elementId, pageId, tenantId, updates, }: { elementId: string; pageId: string; tenantId: string; updates: Partial; }) => { // Map PageElement to API UpdateElementRequest // Convert ElementType enum to string literals for API const apiUpdates: any = { type: updates.type?.toLowerCase(), // Convert enum to lowercase string selectors: updates.selectors?.map(s => ({ type: s.type.toLowerCase(), // Convert SelectorType enum to lowercase value: s.value, score: s.score, })), semanticDescription: updates.semanticDescription, text: updates.text, isVisible: updates.isVisible, position: updates.position, }; return await infoCenterApi.updateElement(elementId, pageId, tenantId, apiUpdates); }, onSuccess: (_, variables) => { // Invalidate analyses query for this tenant queryClient.invalidateQueries({ queryKey: queryKeys.infoCenter.analyses(variables.tenantId), }); // Also invalidate elements query queryClient.invalidateQueries({ queryKey: queryKeys.infoCenter.elements(variables.tenantId), }); }, }); } /** * useInfoCenterActions Hook * * Provides all InfoCenter mutation hooks in a single object. * Useful for components that need multiple actions. * * @returns Object with all mutation hooks * * @example * ```tsx * function PageActions({ page }: { page: DiscoveredPage }) { * const { updatePage, deletePage, retriggerAnalysis } = useInfoCenterActions(); * * const handleRescan = () => { * retriggerAnalysis.mutate({ * pageId: page.id, * tenantId: page.tenantId, * priority: 'HIGH', * }); * }; * * return ( *
* *
* ); * } * ``` */ export function useInfoCenterActions() { const updatePage = useUpdatePage(); const deletePage = useDeletePage(); const retriggerAnalysis = useRetriggerAnalysis(); const updateElement = useUpdateElement(); return { updatePage, deletePage, retriggerAnalysis, updateElement, }; }