import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { researchDocumentService } from '../services/researchDocumentService' import { STALE_SIGNALS } from '../lib/constants' import { useToastStore } from '../stores/toastStore' const KEY = ['researchDocuments'] /** Die manuell abgelegten Newsquellen (Runde 10, §2.6). */ export function useResearchDocuments() { return useQuery({ queryKey: KEY, queryFn: () => researchDocumentService.getAll(), staleTime: STALE_SIGNALS, select: (res) => res.data, }) } export function useUploadResearchDocument() { const queryClient = useQueryClient() return useMutation({ mutationFn: (file: File) => researchDocumentService.upload(file), onSuccess: (res) => { void queryClient.invalidateQueries({ queryKey: KEY }) // Ein Dokument ohne brauchbaren Text wird abgelegt, trägt aber nichts // bei — das muss beim Ablegen gesagt werden und nicht erst, wenn der // Lauf keine Leads daraus findet. if (res.data.extractionNote) { useToastStore.getState().showToast(res.data.extractionNote, 'warning') } else { useToastStore.getState().showToast(`«${res.data.name}» abgelegt.`, 'success') } }, onError: (err: Error) => { useToastStore.getState().showToast(err.message, 'error') }, }) } export function useRemoveResearchDocument() { const queryClient = useQueryClient() return useMutation({ mutationFn: (id: string) => researchDocumentService.remove(id), onSuccess: () => { void queryClient.invalidateQueries({ queryKey: KEY }) }, onError: () => { useToastStore.getState().showToast('Das Dokument konnte nicht entfernt werden.', 'error') }, }) }