import { useState, useEffect } from 'react'; import { X, Search, MessageCircle, Users, Radio } from 'lucide-react'; import api from '../api/client'; import { useStore } from '../store'; import Avatar from './Avatar'; interface Props { onClose: () => void; onOpen: (chatId: string) => void; } type Step = 'type' | 'user' | 'group'; export default function NewChatModal({ onClose, onOpen }: Props) { const [step, setStep] = useState('type'); const [chatType, setChatType] = useState<'private' | 'group' | 'channel'>('private'); const [users, setUsers] = useState([]); const [search, setSearch] = useState(''); const [selected, setSelected] = useState([]); const [groupTitle, setGroupTitle] = useState(''); const [loading, setLoading] = useState(false); const { setChats, chats } = useStore(); useEffect(() => { api.get('/api/users').then(r => setUsers(r.data)); }, []); const filtered = users.filter(u => u.displayName.toLowerCase().includes(search.toLowerCase()) || u.username.toLowerCase().includes(search.toLowerCase()) ); function toggleSelect(id: string) { setSelected(s => s.includes(id) ? s.filter(x => x !== id) : [...s, id]); } async function createPrivate(userId: string) { setLoading(true); try { const { data } = await api.post(`/api/chats/private/${userId}`); const { data: chat } = await api.get(`/api/chats/${data.id}`); // Refresh chats const { data: allChats } = await api.get('/api/chats'); setChats(allChats); onOpen(data.id); onClose(); } finally { setLoading(false); } } async function createGroup() { if (!groupTitle.trim()) return; setLoading(true); try { const { data } = await api.post('/api/chats', { type: chatType, title: groupTitle, memberIds: selected, }); const { data: allChats } = await api.get('/api/chats'); setChats(allChats); onOpen(data.id); onClose(); } finally { setLoading(false); } } return (
{/* Header */}

{step === 'type' ? 'Новый чат' : step === 'user' ? 'Выбор пользователя' : 'Создание ' + (chatType === 'channel' ? 'канала' : 'группы')}

{/* Step 1: Type selection */} {step === 'type' && (
)} {/* Step 2: Select user (private) */} {step === 'user' && ( <>
setSearch(e.target.value)} placeholder="Поиск..." className="flex-1 bg-transparent text-sm outline-none" autoFocus />
{filtered.map(u => ( ))}
)} {/* Step 3: Create group/channel */} {step === 'group' && ( <>
setGroupTitle(e.target.value)} placeholder={chatType === 'channel' ? 'Название канала' : 'Название группы'} className="w-full px-4 py-2.5 border border-gray-200 rounded-xl text-sm focus:outline-none focus:ring-2 focus:ring-blue-200" autoFocus />
setSearch(e.target.value)} placeholder="Добавить участников..." className="flex-1 bg-transparent text-sm outline-none" />
{selected.length > 0 && (
{selected.map(id => { const u = users.find(x => x.id === id); return u ? ( toggleSelect(id)} className="flex items-center gap-1 bg-blue-100 text-blue-700 text-xs px-2 py-1 rounded-full cursor-pointer"> {u.displayName} × ) : null; })}
)}
{filtered.map(u => ( ))}
)}
); }