"use client"; import { useState, useEffect } from 'react'; import { useAuth } from '@/contexts/auth-context'; import { useContacts } from '@/contexts/contacts-context'; import { useGroups } from '@/contexts/groups-context'; import { useSettings } from '@/contexts/settings-context'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Checkbox } from './ui/checkbox'; import { Avatar, AvatarFallback, AvatarImage } from './ui/avatar'; import type { GroupType } from '@/lib/types'; import { RadioGroup, RadioGroupItem } from './ui/radio-group'; import { Book, Gamepad2, Users, Heart, ArrowLeft, X } from 'lucide-react'; import { cn } from '@/lib/utils'; import { ScrollArea } from './ui/scroll-area'; export function CreateGroupModal({ isOpen, onClose }: { isOpen: boolean; onClose: () => void }) { const { currentUser } = useAuth(); const { contacts } = useContacts(); const { createGroup } = useGroups(); const { addToast, t, playSound } = useSettings(); const [groupName, setGroupName] = useState(''); const [photoURL, setPhotoURL] = useState(''); const [selectedContacts, setSelectedContacts] = useState([]); const [groupType, setGroupType] = useState('general'); const [isLoading, setIsLoading] = useState(false); useEffect(() => { if (currentUser && isOpen) { setSelectedContacts([currentUser.uid]); setGroupName(''); setPhotoURL(''); setGroupType('general'); } }, [currentUser, isOpen]); const handleToggleContact = (uid: string) => { if (uid === currentUser?.uid) return; playSound('touch'); setSelectedContacts(prev => prev.includes(uid) ? prev.filter(id => id !== uid) : [...prev, uid] ); }; const handleCreate = async () => { if (!groupName.trim()) { addToast('Please enter a name for the group.', { variant: 'destructive' }); return; } if (!currentUser) return; setIsLoading(true); playSound('send'); const memberUids = selectedContacts.filter(uid => uid !== currentUser.uid); await createGroup(groupName, photoURL, memberUids, groupType); setIsLoading(false); onClose(); }; const groupTypeOptions = [ { id: 'general', label: t('general') }, { id: 'study', label: t('study') }, { id: 'gaming', label: t('gaming') }, { id: 'friends', label: t('friends') }, ]; if (!isOpen || !currentUser) return null; return (

{t('createGroupTitle')}

setGroupName(e.target.value)} />
setPhotoURL(e.target.value)} placeholder="https://example.com/image.png" />
setGroupType(value as GroupType)} className="grid grid-cols-2 lg:grid-cols-4 gap-2"> {groupTypeOptions.map(option => ( ))}
{contacts.length > 0 ? contacts.map(contact => (
handleToggleContact(contact.uid)} />
)) :

{t('noContactsToInvite')}

}
); }