"use client" import type React from "react" import { useState } from "react" import { Loader2, Search } from "lucide-react" import { GlassModal, fieldClass, labelClass } from "./glass-modal" import * as api from "@/lib/api" import { ApiError } from "@/lib/api" function randomCode() { const chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789" return Array.from({ length: 8 }, () => chars[Math.floor(Math.random() * chars.length)]).join("") } interface CreateRoomDialogProps { onClose: () => void onCreated: (roomId: number) => void } export function CreateRoomDialog({ onClose, onCreated }: CreateRoomDialogProps) { const [name, setName] = useState("") const [code, setCode] = useState(randomCode()) const [error, setError] = useState(null) const [submitting, setSubmitting] = useState(false) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError(null) if (!/^[A-Za-z0-9]{6,32}$/.test(code)) { setError("Invite code must be 6–32 letters or numbers.") return } setSubmitting(true) try { const res = await api.createRoom(name.trim(), code) onCreated(res.room_id) onClose() } catch (err) { setError(err instanceof ApiError ? err.message : "Could not create the room.") } finally { setSubmitting(false) } } return (
setName(e.target.value)} className={fieldClass} placeholder="e.g. Design Team" />
setCode(e.target.value)} className={fieldClass} placeholder="6–32 letters/numbers" />

Share this code so others can join.

{error && (
{error}
)}
) } interface JoinRoomDialogProps { onClose: () => void onJoined: (roomId: number) => void } export function JoinRoomDialog({ onClose, onJoined }: JoinRoomDialogProps) { const [code, setCode] = useState("") const [preview, setPreview] = useState(null) const [error, setError] = useState(null) const [searching, setSearching] = useState(false) const [submitting, setSubmitting] = useState(false) const handleSearch = async () => { setError(null) setPreview(null) if (!code.trim()) return setSearching(true) try { const res = await api.searchRoom(code.trim()) if (res.status === "found" && res.room_name) { setPreview(res.room_name) } else { setError("No room found with that invite code.") } } catch (err) { setError(err instanceof ApiError ? err.message : "Search failed.") } finally { setSearching(false) } } const handleJoin = async (e: React.FormEvent) => { e.preventDefault() setError(null) setSubmitting(true) try { const res = await api.joinRoom(code.trim()) if (res.status === "joined" && res.room_id) { onJoined(res.room_id) onClose() } else { setError("Could not join — the room was not found or you're already a member.") } } catch (err) { setError(err instanceof ApiError ? err.message : "Could not join the room.") } finally { setSubmitting(false) } } return (
{ setCode(e.target.value) setPreview(null) }} className={fieldClass} placeholder="Enter code" />
{preview && (
Found room: {preview}
)} {error && (
{error}
)}
) }