Files
property-match/src/hooks/useMatches.ts
T
Benjamin Sutter e36c5bc979 refactor(arch): eliminate direct service calls in components — route all through hooks
New hook files:
- useAuth.ts: useLogin, useSwitchDemoRole, useSwitchOrganization
- useAI.ts: useParseNeed, useGenerateOfferEmail, useGenerateDecisionBrief, useParseListingText
- useAssistant.ts: useAssistantSuggestions (useQuery), useAssistantAnswer (useMutation)
- useOfferReport.ts: useOfferReportByInquiry, useCreateOfferReport, useUpdateOfferReport, useGenerateOfferReportPdf
- useInquiryReport.ts: useInquiryReportByInquiry, useCreateInquiryReport, useUpdateInquiryReport, useFinalizeInquiryReport
- useMarketReport.ts: useMarketReport
- useWeighting.ts: useDefaultWeights (synchronous wrapper)
- useUnitMatches.ts: useUnitMatchesMap, useUnitBundle, useBundleMatches (useMemo wrappers)

Extended hooks: useProperties (add update/create/remove mutations),
useNeeds (add useCreateNeed), useMatches (add useNeedMatchesForProperty,
useAdditionalMatchesForInquiry), useReviewQueue (add useCreateReviewTask)

Updated 21 components/pages: all direct service imports replaced with hooks.
Deliberate exception: getRecommendedActions in DataQuality.tsx (pure sync utility, no provider access).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-24 14:56:32 +02:00

78 lines
2.3 KiB
TypeScript

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { matchService } from '../services/matchService'
import { STALE_MATCHES } from '../lib/constants'
import { useToastStore } from '../stores/toastStore'
export function useMatches() {
return useQuery({
queryKey: ['matches'],
queryFn: () => matchService.getAll(),
staleTime: STALE_MATCHES,
select: (res) => res.data ?? [],
})
}
export function useMatchesByNeed(needId: string) {
return useQuery({
queryKey: ['matches', 'need', needId],
queryFn: () => matchService.getByNeed(needId),
staleTime: STALE_MATCHES,
enabled: Boolean(needId),
select: (res) => res.data ?? [],
})
}
export function useMatchesByProperty(propertyId: string) {
return useQuery({
queryKey: ['matches', 'property', propertyId],
queryFn: () => matchService.getByProperty(propertyId),
staleTime: STALE_MATCHES,
enabled: Boolean(propertyId),
select: (res) => res.data ?? [],
})
}
export function useApproveMatch() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (matchId: string) => matchService.approve(matchId, 'current-user'),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['matches'] })
},
onError: () => {
useToastStore.getState().showToast('Match konnte nicht genehmigt werden.', 'error')
},
})
}
export function useMatchDetail(id: string) {
return useQuery({
queryKey: ['match', id],
queryFn: () => matchService.getById(id),
staleTime: STALE_MATCHES,
enabled: Boolean(id),
select: (res) => res.data ?? null,
})
}
export function useNeedMatchesForProperty(propertyId: string, opts?: { minScore?: number }) {
return useQuery({
queryKey: ['need-matches-for-property', propertyId, opts ?? {}],
queryFn: () => matchService.getNeedMatchesForProperty(propertyId, opts),
staleTime: STALE_MATCHES,
enabled: Boolean(propertyId),
})
}
export function useAdditionalMatchesForInquiry(
inquiryId: string | null,
opts?: { minScore?: number; excludePropertyId?: string },
) {
return useQuery({
queryKey: ['additional-matches-for-inquiry', inquiryId, opts ?? {}],
queryFn: () => matchService.getAdditionalMatchesForInquiry(inquiryId!, opts),
staleTime: STALE_MATCHES,
enabled: !!inquiryId,
})
}