api: add Person and StreamingService contexts

This commit is contained in:
Cron1cle
2025-10-07 20:08:51 +02:00
parent de2778d6db
commit 2dacb9fa80
12 changed files with 390 additions and 111 deletions

View File

@@ -1,5 +1,11 @@
import { getSeason, SeasonParticipant } from "@/apis/seasonApi";
import React, { createContext, useContext, useState, useCallback } from "react";
import React, {
createContext,
useContext,
useState,
useCallback,
ReactNode,
} from "react";
type SeasonContextType = {
fetchSeasonParticipants: (
@@ -11,11 +17,10 @@ type SeasonContextType = {
const SeasonContext = createContext<SeasonContextType | null>(null);
export const SeasonProvider = ({ children }: { children: React.ReactNode }) => {
export const SeasonProvider = ({ children }: { children: ReactNode }) => {
const [seasonCache, setSeasonCache] = useState<
Record<string, SeasonParticipant[]>
>({});
const [seasonCountCache, setSeasonCountCache] = useState<
Record<number, number>
>({});
@@ -24,7 +29,6 @@ export const SeasonProvider = ({ children }: { children: React.ReactNode }) => {
async (showId: number, seasonNumber: number) => {
const key = `${showId}-${seasonNumber}`;
if (seasonCache[key]) return seasonCache[key];
try {
const season = await getSeason(showId, seasonNumber);
const participants = season?.participants ?? [];
@@ -67,8 +71,8 @@ export const SeasonProvider = ({ children }: { children: React.ReactNode }) => {
};
export const useSeasonContext = () => {
const context = useContext(SeasonContext);
if (!context)
const ctx = useContext(SeasonContext);
if (!ctx)
throw new Error("useSeasonContext must be used within a SeasonProvider");
return context;
return ctx;
};