import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { exposeLeadService } from '../services/exposeLeadService' import type { CreateExposeLeadInput } from '../domain/exposeLead' import { STALE_EXPOSE_LEADS } from '../lib/constants' import { useToastStore } from '../stores/toastStore' export function useExposeLeads() { return useQuery({ queryKey: ['expose-leads'], queryFn: async () => (await exposeLeadService.getAll()).data, staleTime: STALE_EXPOSE_LEADS, }) } export function useCreateExposeLead() { const queryClient = useQueryClient() return useMutation({ mutationFn: async (input: CreateExposeLeadInput) => (await exposeLeadService.create(input)).data, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['expose-leads'] }) }, onError: (err: Error) => { useToastStore.getState().showToast(err.message || 'Lead konnte nicht weitergeleitet werden.', 'error') }, }) } export function useArchiveExposeLead() { const queryClient = useQueryClient() return useMutation({ mutationFn: async (id: string) => (await exposeLeadService.archive(id)).data, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['expose-leads'] }) }, onError: () => { useToastStore.getState().showToast('Lead konnte nicht archiviert werden.', 'error') }, }) }