import { useState, useRef } from 'react'; import { Dialog } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Button } from '@/components/ui/button'; import { Select } from '@/components/ui/select'; import { apiClient } from '@/services/api/client'; import { useToast } from '@/hooks/useToast'; import { useChatStore } from '../store/chatStore'; import { parseApiError } from '@/utils/apiErrorHandler'; import { ErrorDisplay } from '@/components/ui/ErrorDisplay'; // FE-PAGE-005: Complete Chat page implementation - Room Management interface CreateRoomDialogProps { open: boolean; onClose: () => void; } export function CreateRoomDialog({ open, onClose }: CreateRoomDialogProps) { const [name, setName] = useState(''); const [type, setType] = useState<'public' | 'private'>('public'); const [isCreating, setIsCreating] = useState(false); const [validationError, setValidationError] = useState(null); const [mutationError, setMutationError] = useState(null); const [retryCount, setRetryCount] = useState(0); const lastMutationRef = useRef<(() => Promise) | null>(null); const toast = useToast(); const { addConversation, setCurrentConversation } = useChatStore(); const handleCreate = async () => { setValidationError(null); setMutationError(null); if (!name.trim()) { setValidationError('Room name is required'); return; } // Action 3.4.1.3: Store mutation for retry const roomName = name.trim(); const roomType = type; const performMutation = async () => { const response = await apiClient.post('/conversations', { name: roomName, type: roomType, }); const newRoom = { id: response.data.id || response.data.conversation?.id, name: response.data.name || response.data.conversation?.name, type: response.data.type || response.data.conversation?.type || roomType, participants: response.data.participants || [], unread_count: 0, }; addConversation(newRoom); setCurrentConversation(newRoom.id); toast.success('Room created successfully'); setName(''); setType('public'); setMutationError(null); setRetryCount(0); lastMutationRef.current = null; onClose(); }; lastMutationRef.current = performMutation; setIsCreating(true); try { await performMutation(); } catch (error: unknown) { const apiError = parseApiError(error); setMutationError(new Error(apiError.message)); } finally { setIsCreating(false); } }; // Action 3.4.1.3: Retry handler for failed mutations const handleRetry = async () => { if (!lastMutationRef.current || retryCount >= 3) return; setRetryCount((prev) => prev + 1); setIsCreating(true); try { await lastMutationRef.current(); } catch (error) { // Error will be handled by the mutation function } finally { setIsCreating(false); } }; return (
{mutationError && ( { setMutationError(null); setRetryCount(0); lastMutationRef.current = null; }} /> )} {validationError && ( )}
{ setName(e.target.value); setValidationError(null); }} placeholder="Enter room name" maxLength={100} />