import { useState, useEffect } from "react"; import { Container, Paper, Button, Box, Typography, List, ListItem, ListItemText, LinearProgress, } from "@mui/material"; import RestartAltIcon from "@mui/icons-material/RestartAlt"; import axios from "axios"; function App() { const [storySegments, setStorySegments] = useState([]); const [currentChoices, setCurrentChoices] = useState([]); const [isLoading, setIsLoading] = useState(false); const handleStoryAction = async (action, choiceId = null) => { setIsLoading(true); try { const response = await axios.post("http://localhost:8000/api/chat", { message: action, choice_id: choiceId, }); if (action === "restart") { setStorySegments([ { text: response.data.story_text, isChoice: false, isDeath: response.data.is_death, }, ]); } else { setStorySegments((prev) => [ ...prev, { text: response.data.story_text, isChoice: false, isDeath: response.data.is_death, }, ]); } setCurrentChoices(response.data.choices); } catch (error) { console.error("Error:", error); setStorySegments((prev) => [ ...prev, { text: "Connection lost with the storyteller...", isChoice: false }, ]); } finally { setIsLoading(false); } }; // Start the story when the component mounts useEffect(() => { handleStoryAction("restart"); }, []); const handleChoice = async (choiceId) => { // Add the chosen option to the story setStorySegments((prev) => [ ...prev, { text: currentChoices.find((c) => c.id === choiceId).text, isChoice: true, }, ]); // Continue the story with this choice await handleStoryAction("choice", choiceId); }; return ( Echoes of Influence {isLoading && } {storySegments.map((segment, index) => ( ))} {currentChoices.length > 0 && ( {currentChoices.map((choice) => ( ))} )} ); } export default App;