api: add Person and StreamingService contexts
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user