Files
fltr-app/hooks/useSeason.ts
2025-10-29 20:50:21 +11:00

50 lines
1.5 KiB
TypeScript

import { useQuery } from "@tanstack/react-query";
import { getSeason } from "@/apis/seasonApi";
export const useSeason = (showId: number, seasonNumber: number) => {
return useQuery({
queryKey: ["season", showId, seasonNumber],
queryFn: () => getSeason(showId, seasonNumber),
enabled: !!showId && !!seasonNumber,
});
};
export const useSeasonParticipants = (showId: number, seasonNumber: number) => {
const { data: season, ...rest } = useSeason(showId, seasonNumber);
return {
data: season?.participants ?? [],
...rest,
};
};
export const useSeasonDates = (showId: number, seasonNumber: number) => {
const { data: season, ...rest } = useSeason(showId, seasonNumber);
return {
data: season ? { startDate: season.startDate, endDate: season.endDate } : null,
...rest,
};
};
// This is a bit tricky, as we need to fetch seasons sequentially to know the count.
// React Query is not ideal for this kind of sequential fetching.
// However, we can still wrap the existing logic in a useQuery hook.
// This is not the most efficient way, but it's better than nothing.
export const useSeasonCount = (showId: number) => {
return useQuery({
queryKey: ["seasonCount", showId],
queryFn: async () => {
let n = 0;
for (let s = 1; s <= 50; s++) {
try {
const season = await getSeason(showId, s);
if (!season) break;
n = s;
} catch {
break;
}
}
return n;
},
enabled: !!showId,
});
};