Files
lolympus-tournament-support/apps/web/src/api/hooks.ts
T
2026-04-26 01:22:38 -07:00

103 lines
2.7 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { DEFAULT_COMMUNITY_ID, useAuthUserId } from '../config';
import { apiFetch } from '../lib/api';
import type {
ApiAuction,
ApiCommunity,
ApiMatch,
DdragonManifest,
MatchStatus,
MatchesResponse,
MeResponse,
} from './types';
const DDRAGON_MANIFEST_STALE_MS = 24 * 60 * 60 * 1000;
interface MatchesQueryOptions {
status?: MatchStatus;
limit?: number;
}
function createMatchesPath(
communityId: string,
options: MatchesQueryOptions = {},
) {
const searchParams = new URLSearchParams();
if (options.status) searchParams.set('status', options.status);
if (options.limit) searchParams.set('limit', String(options.limit));
const queryString = searchParams.toString();
return `/communities/${communityId}/matches${queryString ? `?${queryString}` : ''}`;
}
export function useCommunity(communityId = DEFAULT_COMMUNITY_ID) {
return useQuery({
queryKey: ['community', communityId],
queryFn: () => apiFetch<ApiCommunity>(`/communities/${communityId}`),
enabled: Boolean(communityId),
});
}
export function useMatches(
options: MatchesQueryOptions,
communityId = DEFAULT_COMMUNITY_ID,
) {
return useQuery({
queryKey: ['matches', communityId, options.status ?? null, options.limit ?? null],
queryFn: async () => {
const response = await apiFetch<MatchesResponse>(
createMatchesPath(communityId, options),
);
return response.matches;
},
enabled: Boolean(communityId),
});
}
export function useMatch(matchId: string | undefined, communityId = DEFAULT_COMMUNITY_ID) {
return useQuery({
queryKey: ['match', communityId, matchId ?? null],
queryFn: () =>
apiFetch<ApiMatch>(`/communities/${communityId}/matches/${matchId}`),
enabled: Boolean(communityId && matchId),
retry: false,
});
}
export function useAuction(
matchId: string | undefined,
communityId = DEFAULT_COMMUNITY_ID,
) {
return useQuery({
queryKey: ['auction', communityId, matchId ?? null],
queryFn: () =>
apiFetch<ApiAuction>(`/communities/${communityId}/matches/${matchId}/auction`),
enabled: Boolean(communityId && matchId),
retry: false,
});
}
export function useDdragonManifest(locale = 'ko_KR') {
return useQuery({
queryKey: ['ddragon-manifest', locale],
queryFn: () =>
apiFetch<DdragonManifest>(
`/riot/ddragon/manifest?locale=${encodeURIComponent(locale)}`,
),
staleTime: DDRAGON_MANIFEST_STALE_MS,
gcTime: DDRAGON_MANIFEST_STALE_MS,
refetchOnMount: false,
refetchOnReconnect: false,
});
}
export function useMe() {
const authUserId = useAuthUserId();
return useQuery({
queryKey: ['me', authUserId],
queryFn: () => apiFetch<MeResponse>('/me'),
retry: false,
});
}