"use client"; import { useState, useEffect, useCallback } from "react"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { InputOTP, InputOTPGroup, InputOTPSlot, } from "@/components/ui/input-otp"; import { Card, CardContent } from "@/components/ui/card"; import { ThemeProvider } from "next-themes"; import { VSCODE_THEMES, getThemeById, applyTheme, saveThemeToCookie, getThemeFromCookie, } from "@/lib/themes"; const Home = () => { const router = useRouter(); const [newRoomCode, setNewRoomCode] = useState(""); const [currentThemeIndex, setCurrentThemeIndex] = useState(0); const [isClient, setIsClient] = useState(false); const nextTheme = useCallback(() => { setCurrentThemeIndex((prevIndex) => { const newIndex = (prevIndex + 1) % VSCODE_THEMES.length; const theme = VSCODE_THEMES[newIndex]; applyTheme(theme); saveThemeToCookie(theme.id); return newIndex; }); }, []); const prevTheme = useCallback(() => { setCurrentThemeIndex((prevIndex) => { const newIndex = prevIndex === 0 ? VSCODE_THEMES.length - 1 : prevIndex - 1; const theme = VSCODE_THEMES[newIndex]; applyTheme(theme); saveThemeToCookie(theme.id); return newIndex; }); }, []); useEffect(() => { setIsClient(true); // Initialize theme from cookie const savedThemeId = getThemeFromCookie(); if (savedThemeId) { const themeIndex = VSCODE_THEMES.findIndex( (theme) => theme.id === savedThemeId ); if (themeIndex !== -1) { setCurrentThemeIndex(themeIndex); const theme = getThemeById(savedThemeId); if (theme) { applyTheme(theme); } } } else { // Apply default theme (first in array) const defaultTheme = VSCODE_THEMES[0]; applyTheme(defaultTheme); } // Simple keyboard navigation const handleKeyPress = (e: KeyboardEvent) => { if (e.key === "ArrowLeft") { e.preventDefault(); prevTheme(); } else if (e.key === "ArrowRight") { e.preventDefault(); nextTheme(); } }; window.addEventListener("keydown", handleKeyPress); return () => { window.removeEventListener("keydown", handleKeyPress); }; }, [nextTheme, prevTheme]); useEffect(() => { const joinRoom = () => { if (newRoomCode) { router.push(`/room?code=${newRoomCode.toUpperCase()}`); } }; if (newRoomCode.length === 6) { joinRoom(); } }, [newRoomCode, router]); const createNewRoom = () => { const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; let code = ""; for (let i = 0; i < 6; i++) { code += chars.charAt(Math.floor(Math.random() * chars.length)); } router.push(`/room?code=${code}`); }; if (!isClient) { return null; } const currentTheme = VSCODE_THEMES[currentThemeIndex]; return (