import ParticipantDetails from "@/components/ParticipantDeatails"; import ShowInfo from "@/components/ui/ShowInfo"; import StackHeader from "@/components/ui/StackHeader"; import { useSeasonContext } from "@/contexts/SeasonContext"; import { router, useLocalSearchParams } from "expo-router"; import React from "react"; import { Dimensions, Image, ScrollView, Text, TouchableOpacity, View, } from "react-native"; import styles from "./stackStyles/showDetailStyles"; export default function ShowDetails() { const { bannerUri, description, concept, genres, streamingService, id, endDate, } = useLocalSearchParams(); const [selectedParticipants, setSelectedParticipants] = React.useState(true); const [selectedSeason, setSelectedSeason] = React.useState(1); const showId = Number(id); const { fetchSeasonParticipants, fetchSeasonCount, fetchSeasonDates } = useSeasonContext(); const [seasonCount, setSeasonCount] = React.useState(0); const [participants, setParticipants] = React.useState< { id: number; name: string; imageUri: string }[] >([]); const [startDate, setStartDate] = React.useState( undefined ); const [pLoading, setPLoading] = React.useState(false); const [pError, setPError] = React.useState(null); const sortedParticipants = React.useMemo(() => { return participants.sort((a, b) => a.name.localeCompare(b.name, "de", { sensitivity: "base" }) ); }, [participants]); React.useEffect(() => { if (!showId) return; let active = true; (async () => { const count = await fetchSeasonCount(showId); if (active) { setSeasonCount(count); if (count > 0 && selectedSeason > count) setSelectedSeason(1); } })(); return () => { active = false; }; }, [showId, fetchSeasonCount, selectedSeason]); React.useEffect(() => { if (!showId || !selectedSeason) return; let active = true; (async () => { setPError(null); setPLoading(true); try { const [data, dates] = await Promise.all([ fetchSeasonParticipants(showId, selectedSeason), fetchSeasonDates(showId, selectedSeason), ]); if (active) { setParticipants(data); setStartDate(dates?.startDate); } } catch { if (active) setPError("Fehler beim Laden"); } finally { if (active) setPLoading(false); } })(); return () => { active = false; }; }, [showId, selectedSeason, fetchSeasonParticipants, fetchSeasonDates]); const formattedStartDate = React.useMemo(() => { if (!startDate) return ""; const d = new Date(startDate); if (isNaN(d.getTime())) return ""; return d.toLocaleDateString("de-DE", { day: "2-digit", month: "long", year: "numeric", }); }, [startDate]); return ( {formattedStartDate ? ( {formattedStartDate} ) : null} setSelectedParticipants(true)}> Teilnehmer setSelectedParticipants(false)}> Details {selectedParticipants ? ( <> Staffeln {Array.from({ length: seasonCount }, (_, idx) => idx + 1).map( (season) => ( { setSelectedSeason(season); }} > {season} ) )} {pError && ( {pError} )} {!pLoading && !pError && participants.length === 0 && ( Keine Teilnehmer. )} {sortedParticipants.map((p) => ( router.push({ pathname: "/participant", params: { participantId: p.id, name: p.name, }, }) } > {p.name} ))} ) : ( )} ); }