import { useQuery } from '@tanstack/react-query'; import { useSkateRelays } from '@/hooks/useSkateRelays'; import { SKATE_CREW_KIND, SKATE_SPOT_KIND, parseSkateCrew, type SkateCrew, } from '@/lib/types'; /** * Reverse link: which crews list this spot as a home spot? * * Crews store home spots as `a` tags (`30818:pubkey:d`). We query crews whose * `a` tag references this spot coordinate, so a spot page can show * "Crews that skate here". */ export function useCrewsForSpot(spotPubkey?: string, spotIdentifier?: string, limit = 20) { const { querySkateContent, getQueryKeySuffix } = useSkateRelays(); const spotCoord = spotPubkey && spotIdentifier ? `${SKATE_SPOT_KIND}:${spotPubkey}:${spotIdentifier}` : ''; return useQuery({ queryKey: ['crews-for-spot', spotCoord, getQueryKeySuffix()], enabled: !!spotCoord, staleTime: 5 * 60 * 1000, gcTime: 30 * 60 * 1000, queryFn: async () => { const signal = AbortSignal.timeout(10000); const events = await querySkateContent( [{ kinds: [SKATE_CREW_KIND], '#a': [spotCoord], limit }], { signal }, ); const crews: SkateCrew[] = []; for (const ev of events) { const crew = parseSkateCrew(ev); // Guard: relay `#a` matching can be loose — confirm the ref is present. if (crew && crew.spotRefs.includes(spotCoord)) crews.push(crew); } return crews.sort((a, b) => b.members.length - a.members.length); }, }); }