import { createFileRoute, redirect } from "@tanstack/react-router"; import { useState, useCallback } from "react"; import type { GameSession, GameRequest, AnswerResult } from "@lila/shared"; import { QuestionCard } from "../components/game/QuestionCard"; import { ScoreScreen } from "../components/game/ScoreScreen"; import { GameSetup } from "../components/game/GameSetup"; import { authClient } from "../lib/auth-client"; function Play() { const API_URL = import.meta.env["VITE_API_URL"] || ""; const [gameSession, setGameSession] = useState(null); const [isLoading, setIsLoading] = useState(false); const [currentQuestionIndex, setCurrentQuestionIndex] = useState(0); const [results, setResults] = useState([]); const [currentResult, setCurrentResult] = useState(null); const startGame = useCallback(async (settings: GameRequest) => { setIsLoading(true); const response = await fetch(`${API_URL}/api/v1/game/start`, { method: "POST", headers: { "Content-Type": "application/json" }, credentials: "include", body: JSON.stringify(settings), }); const data = await response.json(); setGameSession(data.data); setCurrentQuestionIndex(0); setResults([]); setCurrentResult(null); setIsLoading(false); }, []); const resetToSetup = useCallback(() => { setGameSession(null); setIsLoading(false); setCurrentQuestionIndex(0); setResults([]); setCurrentResult(null); }, []); const handleAnswer = async (optionId: number) => { if (!gameSession || currentResult) return; const question = gameSession.questions[currentQuestionIndex]; if (!question) return; const response = await fetch(`${API_URL}/api/v1/game/answer`, { method: "POST", headers: { "Content-Type": "application/json" }, credentials: "include", body: JSON.stringify({ sessionId: gameSession.sessionId, questionId: question.questionId, selectedOptionId: optionId, }), }); const data = await response.json(); setCurrentResult(data.data); }; const handleNext = () => { if (!currentResult) return; setResults((prev) => [...prev, currentResult]); setCurrentQuestionIndex((prev) => prev + 1); setCurrentResult(null); }; // Phase: setup if (!gameSession && !isLoading) { return (
); } // Phase: loading if (isLoading || !gameSession) { return (

Loading...

); } // Phase: finished if (currentQuestionIndex >= gameSession.questions.length) { return (
); } // Phase: playing const question = gameSession.questions[currentQuestionIndex]!; return (
); } export const Route = createFileRoute("/play")({ component: Play, beforeLoad: async () => { const { data: session } = await authClient.getSession(); if (!session) { throw redirect({ to: "/login" }); } }, });