"use client"; import { useEffect, useState } from "react"; import type { CardType } from "@/lib/cards"; import { InternationalRatingsView } from "./InternationalRatings"; import { RatingExplainedView } from "./RatingExplained"; interface CardContentProps { movieId: number; cardType: CardType; } export function CardContent({ movieId, cardType }: CardContentProps) { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [retryToken, setRetryToken] = useState(0); useEffect(() => { const controller = new AbortController(); let cancelled = false; async function fetchCard() { try { const response = await fetch( `/api/cards/${cardType}?movieId=${movieId}`, { signal: controller.signal }, ); const payload = await response.json(); if (!response.ok) { throw new Error(payload.error ?? "Something went wrong. Try again."); } if (!cancelled) { setData(payload.data); setError(null); } } catch (err) { if ((err as Error).name === "AbortError" || cancelled) { return; } setError( err instanceof Error ? err.message : "Something went wrong. Try again.", ); setData(null); } finally { if (!cancelled) { setLoading(false); } } } void fetchCard(); return () => { cancelled = true; controller.abort(); }; }, [cardType, movieId, retryToken]); if (loading) { return (

Generating insight...

); } if (error) { return (

{error}

); } if (!data) { return null; } switch (cardType) { case "rating_explained": return ; case "international_ratings": return ; default: return null; } }