import { useEffect, useMemo, useState } from 'react'; import { BACKGROUND_AUTH_REQUEST_OPTIONS, getStoredAccessToken, } from '../apiClient'; import { ensureRuntimeGuestToken } from '../authService'; import { jumpHopClient, type JumpHopLeaderboardResponse, type JumpHopRuntimeRequestOptions, } from './jumpHopClient'; type JumpHopLeaderboardState = { leaderboard: JumpHopLeaderboardResponse | null; isLoading: boolean; error: string | null; refresh: () => Promise; }; export function useJumpHopLeaderboard( profileId: string | null | undefined, runtimeRequestOptions?: JumpHopRuntimeRequestOptions, ): JumpHopLeaderboardState { const normalizedProfileId = profileId?.trim() ?? ''; const [leaderboard, setLeaderboard] = useState(null); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const refresh = useMemo( () => async () => { if (!normalizedProfileId) { setLeaderboard(null); setError(null); setIsLoading(false); return; } setIsLoading(true); setError(null); try { if (runtimeRequestOptions) { const response = await jumpHopClient.getLeaderboard( normalizedProfileId, runtimeRequestOptions, ); setLeaderboard(response); return; } if (getStoredAccessToken()) { const response = await jumpHopClient.getLeaderboard( normalizedProfileId, BACKGROUND_AUTH_REQUEST_OPTIONS, ); setLeaderboard(response); return; } const runtimeGuest = await ensureRuntimeGuestToken(); const response = await jumpHopClient.getLeaderboard( normalizedProfileId, { runtimeGuestToken: runtimeGuest.token }, ); setLeaderboard(response); } catch (caughtError) { setError( caughtError instanceof Error ? caughtError.message : '读取排行榜失败。', ); } finally { setIsLoading(false); } }, [normalizedProfileId, runtimeRequestOptions], ); useEffect(() => { void refresh(); }, [refresh]); return { leaderboard, isLoading, error, refresh }; }