"use client"; import { useState, useRef } from "react"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { ArrowLeft, Mic, MicOff } from "lucide-react"; import Link from "next/link"; import { toast } from "@/hooks/use-toast"; import "../styles/background-pattern.css"; export default function CreateGroup() { const router = useRouter(); const [groupName, setGroupName] = useState(""); const [userName, setUserName] = useState(""); const [isRecording, setIsRecording] = useState(false); const [audioBlob, setAudioBlob] = useState(null); const [isLoading, setIsLoading] = useState(false); const mediaRecorder = useRef(null); const audioChunks = useRef([]); const startRecording = async () => { try { console.log("Requesting microphone access..."); const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); console.log("Microphone access granted"); mediaRecorder.current = new MediaRecorder(stream); audioChunks.current = []; mediaRecorder.current.ondataavailable = (event) => { console.log("Audio data received"); audioChunks.current.push(event.data); }; mediaRecorder.current.onstop = () => { console.log("Recording finished, creating blob"); const audioBlob = new Blob(audioChunks.current, { type: "audio/wav" }); setAudioBlob(audioBlob); console.log("Audio blob created:", audioBlob.size, "bytes"); }; mediaRecorder.current.start(); setIsRecording(true); console.log("Recording started"); toast({ title: "Recording Started", description: "Speak into your microphone...", }); } catch (error) { console.error("Error accessing microphone:", error); toast({ title: "Error", description: "Could not access microphone. Check your permissions.", variant: "destructive", }); } }; const stopRecording = () => { if (mediaRecorder.current && isRecording) { console.log("Stopping recording..."); mediaRecorder.current.stop(); setIsRecording(false); mediaRecorder.current.stream.getTracks().forEach((track) => track.stop()); toast({ title: "Recording Complete", description: "Your voice has been recorded successfully.", }); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!groupName.trim() || !userName.trim() || !audioBlob) { toast({ title: "Error", description: "Please fill in all fields and record your voice", variant: "destructive", }); return; } setIsLoading(true); try { // Create user and upload voice first const formData = new FormData(); formData.append("audio", audioBlob); formData.append("name", userName); const voiceResponse = await fetch("/api/voice", { method: "POST", body: formData, }); if (!voiceResponse.ok) throw new Error("Error uploading voice"); const userData = await voiceResponse.json(); // Save user information in cookies const expirationDate = new Date(); expirationDate.setDate(expirationDate.getDate() + 30); document.cookie = `userId=${ userData.id };expires=${expirationDate.toUTCString()};path=/`; document.cookie = `userName=${encodeURIComponent( userName )};expires=${expirationDate.toUTCString()};path=/`; // Create group with existing user console.log("Creating group with:", { name: groupName, userId: userData.id, }); try { const groupResponse = await fetch("/api/group", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ name: groupName, userId: userData.id, }), }); console.log("Response status:", groupResponse.status); const responseText = await groupResponse.text(); console.log("Raw response:", responseText); let groupData; try { groupData = JSON.parse(responseText); console.log("Parsed response:", groupData); } catch (e) { console.error("JSON parsing error:", e); throw new Error("Invalid server response"); } if (!groupResponse.ok) { throw new Error(groupData.error || "Error creating group"); } toast({ title: "Success", description: "Your game has been created successfully!", }); // Redirect to group page router.push(`/group/${groupData.inviteCode}`); } catch (error) { console.error("Complete error:", error); toast({ title: "Error", description: "An error occurred while creating the group", variant: "destructive", }); } } catch (error) { console.error("Error:", error); toast({ title: "Error", description: "An error occurred while creating the group", variant: "destructive", }); } finally { setIsLoading(false); } }; return (

Create Game

setGroupName(e.target.value)} className="bg-orange-50 text-orange-800 placeholder-orange-300 border-orange-200 focus:border-orange-400 focus:ring-orange-400" placeholder="Enter game name" required />
setUserName(e.target.value)} className="bg-orange-50 text-orange-800 placeholder-orange-300 border-orange-200 focus:border-orange-400 focus:ring-orange-400" placeholder="Enter your username" required />
{audioBlob && !isRecording && (
✓ Voice recorded
)}
); }