import { RelationTypeEnum } from '@bit-ui-libs/common'; import { useQuery } from '@tanstack/react-query'; import { useChainService, useServicesStore } from '../../../stores'; import { getRelatedEvents } from '../utils'; interface UseRelatedEventsOpts { eventId: string; } export function useRelatedEvents(opts: UseRelatedEventsOpts) { const { eventId } = opts; const chainService = useChainService(); const [checkInService, userService] = useServicesStore((s) => [s.checkIn, s.endUser]); const { data, isLoading } = useQuery({ queryKey: ['get-related-events', eventId], queryFn: async () => { const childEvents = (await chainService.getDetailsByEvent(eventId))?.witness ?? []; return { childEvents, officials: await getRelatedEvents({ childEvents, relationType: RelationTypeEnum.Official, checkInService, userService, }), witnesses: await getRelatedEvents({ childEvents, relationType: RelationTypeEnum.Witness, checkInService, userService, }), buyerWitnesses: await getRelatedEvents({ childEvents, relationType: RelationTypeEnum.Buyer, checkInService, userService, }), sellerWitnesses: await getRelatedEvents({ childEvents, relationType: RelationTypeEnum.Seller, checkInService, userService, }), inspector: await getRelatedEvents({ childEvents, relationType: RelationTypeEnum.Inspector, checkInService, userService, }), archive: await getRelatedEvents({ childEvents, relationType: RelationTypeEnum.Archive, checkInService, userService, }), }; }, }); return { officials: data?.officials, witnesses: [...(data?.witnesses ?? []), ...(data?.buyerWitnesses ?? []), ...(data?.sellerWitnesses ?? [])], inspector: data?.inspector, childEvents: data?.childEvents, archive: data?.archive, isLoading, }; }