import { getStreamingImages, StreamingServiceRaw, } from "@/apis/streamingServiceApi"; import { createContext, useContext } from "react"; import React from "react"; type StreamingServiceContextType = { streamingServices: Record; loading: boolean; error: string | null; }; const StreamingServiceContext = createContext(null); export const StreamingServiceProvider = ({ children, }: { children: React.ReactNode; }) => { const [streamingServices, setStreamingServices] = React.useState< Record >({}); const [loading, setLoading] = React.useState(true); const [error, setError] = React.useState(null); React.useEffect(() => { (async () => { try { const data: StreamingServiceRaw[] = await getStreamingImages(); const mapped = Object.fromEntries(data.map((s) => [s.key, s.value])); setStreamingServices(mapped); } catch { setError("Failed to fetch streaming services"); } finally { setLoading(false); } })(); }, []); return ( {children} ); }; export const useStreamingServiceContext = () => { const ctx = useContext(StreamingServiceContext); if (!ctx) throw new Error( "useStreamingServiceContext must be used within a StreamingServiceProvider" ); return ctx; };