118 lines
3.1 KiB
TypeScript
118 lines
3.1 KiB
TypeScript
import { useQuery } from '@tanstack/react-query';
|
|
import { SKATE_COACH_KIND, parseSkateCoach, type SkateCoach } from '@/lib/types';
|
|
import { useSkateRelays } from '@/hooks/useSkateRelays';
|
|
|
|
export function useCoaches(limit = 100) {
|
|
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
|
|
|
|
return useQuery({
|
|
queryKey: ['coaches', limit, getQueryKeySuffix()],
|
|
queryFn: async () => {
|
|
const signal = AbortSignal.timeout(10000);
|
|
|
|
console.log('[useCoaches] Querying for coaches...');
|
|
|
|
const events = await querySkateContent(
|
|
[
|
|
{
|
|
kinds: [SKATE_COACH_KIND],
|
|
'#t': ['skatecoach'],
|
|
limit,
|
|
},
|
|
],
|
|
{ signal }
|
|
);
|
|
|
|
console.log(`[useCoaches] Received ${events.length} events`);
|
|
|
|
const coaches: SkateCoach[] = [];
|
|
for (const event of events) {
|
|
const coach = parseSkateCoach(event);
|
|
if (coach) coaches.push(coach);
|
|
}
|
|
|
|
console.log(`[useCoaches] Parsed coaches: ${coaches.length}`);
|
|
|
|
return coaches.sort((a, b) => b.createdAt - a.createdAt);
|
|
},
|
|
staleTime: 30000,
|
|
refetchOnMount: 'always',
|
|
});
|
|
}
|
|
|
|
export function useCoach(identifier: string, pubkey?: string) {
|
|
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
|
|
|
|
return useQuery({
|
|
queryKey: ['coach', identifier, pubkey, getQueryKeySuffix()],
|
|
queryFn: async () => {
|
|
const signal = AbortSignal.timeout(10000);
|
|
|
|
const filter: {
|
|
kinds: number[];
|
|
'#d': string[];
|
|
authors?: string[];
|
|
limit: number;
|
|
} = {
|
|
kinds: [SKATE_COACH_KIND],
|
|
'#d': [identifier],
|
|
limit: 1,
|
|
};
|
|
|
|
if (pubkey) filter.authors = [pubkey];
|
|
|
|
console.log(`[useCoach] Querying for coach: ${identifier}`);
|
|
|
|
const events = await querySkateContent([filter], { signal });
|
|
|
|
console.log(`[useCoach] Received ${events.length} events`);
|
|
|
|
if (events.length === 0) {
|
|
console.warn(`[useCoach] No events found for identifier: ${identifier}`);
|
|
return null;
|
|
}
|
|
|
|
return parseSkateCoach(events[0]);
|
|
},
|
|
enabled: !!identifier,
|
|
staleTime: 30000,
|
|
refetchOnMount: 'always',
|
|
});
|
|
}
|
|
|
|
export function useUserCoachProfile(pubkey: string) {
|
|
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
|
|
|
|
return useQuery({
|
|
queryKey: ['user-coach-profile', pubkey, getQueryKeySuffix()],
|
|
queryFn: async () => {
|
|
const signal = AbortSignal.timeout(10000);
|
|
|
|
console.log(`[useUserCoachProfile] Querying for coach profile: ${pubkey}`);
|
|
|
|
const events = await querySkateContent(
|
|
[
|
|
{
|
|
kinds: [SKATE_COACH_KIND],
|
|
authors: [pubkey],
|
|
'#d': ['coach-profile'],
|
|
limit: 1,
|
|
},
|
|
],
|
|
{ signal }
|
|
);
|
|
|
|
console.log(`[useUserCoachProfile] Received ${events.length} events`);
|
|
|
|
if (events.length === 0) {
|
|
console.warn(`[useUserCoachProfile] No coach profile found for pubkey: ${pubkey}`);
|
|
return null;
|
|
}
|
|
|
|
return parseSkateCoach(events[0]);
|
|
},
|
|
enabled: !!pubkey,
|
|
staleTime: 30000,
|
|
refetchOnMount: 'always',
|
|
});
|
|
}
|