import { useCallback, useEffect, useState } from "react"; import { ActivityIndicator, StyleSheet, Text, View } from "react-native"; import Screen from "../../layout/Screen"; import { Button, ButtonTray } from "../../UI/Button"; import EventList from "../../../entity/event/EventList"; import useGlobalHook from "../../../hooks/useGlobalHook"; import { getSession, setGlobalSession } from "../../../hooks/SessionStore"; const GlobalEventsScreen = ({ navigation }) => { // Initialisations --------------------- const { getPublicEvents, getPlayersByEvent, joinEvent } = useGlobalHook(); const session = getSession(); // State ------------------------------- const [events, setEvents] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(""); const [joining, setJoining] = useState(null); // Handlers ---------------------------- const loadEvents = useCallback(async () => { setIsLoading(true); setError(""); const data = await getPublicEvents(); setEvents(data); setIsLoading(false); }, []); useEffect(() => { loadEvents(); }, [loadEvents]); const handleSelect = async (event) => { setError(""); setJoining(event.EventID); // Check if already a player const players = await getPlayersByEvent(event.EventID); const existing = (players || []).find( (p) => p.PlayerUserID === session.currentUid, ); if (existing) { setGlobalSession(event.EventID, existing.PlayerID); navigation.navigate("GlobalMapScreen", { eventId: event.EventID }); } else { // Join the event const player = await joinEvent({ PlayerUserID: session.currentUid, PlayerEventID: event.EventID, }); if (player) { setGlobalSession(event.EventID, player.PlayerID); navigation.navigate("GlobalMapScreen", { eventId: event.EventID }); } else { setError("Failed to join event. Please try again."); } } setJoining(null); }; // View -------------------------------- return (