import { useState } from "react"; import Modal from "./Modal"; import { useAuth } from "../../context/AuthContext"; const NameInputModal = ({ isOpen, onClose }) => { const [username, setUsername] = useState(""); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(""); const { login } = useAuth(); const handleUsernameChange = (e) => { setUsername(e.target.value); if (error) setError(""); }; const handleSubmit = async () => { if (!username.trim()) { setError("Please enter your name."); return; } setIsLoading(true); setError(""); try { await login(username); setUsername(""); onClose(); } catch (error) { setError("An error occurred during login. Please try again."); } finally { setIsLoading(false); } }; const handleKeyPress = (e) => { if (e.key === "Enter" && !isLoading && username.trim()) { handleSubmit(); } }; return (

Please enter your name

{error && (

{error}

)}
); }; export default NameInputModal;