import { useState, useRef, useEffect } from 'react' import Game from './components/Game' import Snowflakes from './components/Snowflakes' import QuestionsModal from './components/QuestionsModal' import { questions as initialQuestions } from './data/questions' import { getCookie, setCookie, deleteCookie } from './utils/cookies' import './App.css' function App() { const [isQuestionsModalOpen, setIsQuestionsModalOpen] = useState(false) const [questions, setQuestions] = useState(() => { const savedQuestions = getCookie('gameQuestions') return savedQuestions || initialQuestions }) const [currentQuestionIndex, setCurrentQuestionIndex] = useState(() => { const savedIndex = getCookie('gameQuestionIndex') return savedIndex !== null ? savedIndex : 0 }) const gameRef = useRef(null) const currentQuestion = questions[currentQuestionIndex] // Сохраняем вопросы в cookies при изменении useEffect(() => { if (questions.length > 0) { setCookie('gameQuestions', questions) } }, [questions]) // Сохраняем индекс вопроса в cookies при изменении useEffect(() => { setCookie('gameQuestionIndex', currentQuestionIndex) }, [currentQuestionIndex]) const handleUpdateQuestions = (updatedQuestions) => { setQuestions(updatedQuestions) // Если текущий вопрос был удален, сбрасываем индекс if (currentQuestionIndex >= updatedQuestions.length) { setCurrentQuestionIndex(0) } } const handleOpenPlayersModal = () => { if (gameRef.current) { gameRef.current.openPlayersModal() } } const handleNewGame = () => { if (window.confirm('Начать новую игру? Текущий прогресс будет потерян.')) { deleteCookie('gameQuestions') deleteCookie('gameQuestionIndex') deleteCookie('gamePlayers') deleteCookie('gamePlayerScores') deleteCookie('gameCurrentPlayerId') deleteCookie('gameRevealedAnswers') deleteCookie('gameOver') setQuestions(initialQuestions) setCurrentQuestionIndex(0) if (gameRef.current) { gameRef.current.newGame() } } } const handleShowAll = () => { if (gameRef.current && gameRef.current.showAllAnswers) { gameRef.current.showAllAnswers() } } return (

100 к 1

{questions.length > 0 && currentQuestion && (
{currentQuestionIndex + 1}/{questions.length}
)}
setIsQuestionsModalOpen(false)} questions={questions} onUpdateQuestions={handleUpdateQuestions} />
) } export default App