110 lines
2.9 KiB
TypeScript
110 lines
2.9 KiB
TypeScript
import { useQuery } from '@tanstack/react-query';
|
|
import { SKATE_COACH_KIND, parseSkateCoach, type SkateCoach } from '@/lib/types';
|
|
import { useSkateRelays } from '@/hooks/useSkateRelays';
|
|
import { useAdminModeration, filterAdminHidden } from '@/hooks/useAdminModeration';
|
|
|
|
export function useCoaches(limit = 100) {
|
|
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
|
|
const hidden = useAdminModeration();
|
|
|
|
return useQuery({
|
|
queryKey: ['coaches', limit, getQueryKeySuffix()],
|
|
select: (coaches: SkateCoach[]) => filterAdminHidden(coaches, SKATE_COACH_KIND, hidden),
|
|
queryFn: async () => {
|
|
const signal = AbortSignal.timeout(10000);
|
|
|
|
const events = await querySkateContent(
|
|
[
|
|
{
|
|
kinds: [SKATE_COACH_KIND],
|
|
'#t': ['skatecoach'],
|
|
limit,
|
|
},
|
|
],
|
|
{ signal }
|
|
);
|
|
|
|
const coaches: SkateCoach[] = [];
|
|
for (const event of events) {
|
|
const coach = parseSkateCoach(event);
|
|
if (coach) coaches.push(coach);
|
|
}
|
|
|
|
return coaches.sort((a, b) => b.createdAt - a.createdAt);
|
|
},
|
|
staleTime: 30000,
|
|
refetchOnMount: 'always',
|
|
});
|
|
}
|
|
|
|
export function useCoach(identifier: string, pubkey?: string) {
|
|
const { querySkateContent, getQueryKeySuffix } = useSkateRelays();
|
|
const hidden = useAdminModeration();
|
|
|
|
return useQuery({
|
|
queryKey: ['coach', identifier, pubkey, getQueryKeySuffix()],
|
|
select: (coach: SkateCoach | null) =>
|
|
coach && hidden.has({ coord: `${SKATE_COACH_KIND}:${coach.pubkey}:${coach.identifier}`, id: coach.id })
|
|
? null
|
|
: coach,
|
|
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];
|
|
|
|
const events = await querySkateContent([filter], { signal });
|
|
|
|
if (events.length === 0) {
|
|
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);
|
|
|
|
const events = await querySkateContent(
|
|
[
|
|
{
|
|
kinds: [SKATE_COACH_KIND],
|
|
authors: [pubkey],
|
|
'#d': ['coach-profile'],
|
|
limit: 1,
|
|
},
|
|
],
|
|
{ signal }
|
|
);
|
|
|
|
if (events.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
return parseSkateCoach(events[0]);
|
|
},
|
|
enabled: !!pubkey,
|
|
staleTime: 30000,
|
|
refetchOnMount: 'always',
|
|
});
|
|
}
|