import { useState, useRef } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { X, Upload, Check, Loader2, MessageSquare, AlertCircle } from 'lucide-react'; import { AppApi } from '../../../../core/infrastructure/appApi'; import { useLang } from '../../../../core/infrastructure/i18n'; import type { User as UserType, FriendWithId } from '../../../../core/domain/types'; import { useAuthStore } from '../../../auth/application/authStore'; import { useChatStore } from '../../../chats/application/chatStore'; interface TelegramImportModalProps { isOpen: boolean; onClose: () => void; friends: FriendWithId[]; } export default function TelegramImportModal({ isOpen, onClose, friends }: TelegramImportModalProps) { const { t } = useLang(); const { user } = useAuthStore(); const fileInputRef = useRef(null); const [step, setStep] = useState<1 | 2 | 3>(1); // 1: upload, 2: map, 3: loading/done const [file, setFile] = useState(null); const [token, setToken] = useState(null); const [names, setNames] = useState([]); const [mapping, setMapping] = useState>({}); const [groupName, setGroupName] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [importedState, setImportedState] = useState<{ count: number; text: string } | null>(null); const handleFileChange = async (e: React.ChangeEvent) => { const selectedFile = e.target.files?.[0]; if (!selectedFile) return; if (!selectedFile.name.endsWith('.zip')) { setError('Пожалуйста, выберите ZIP-архив экспорта Telegram.'); return; } setFile(selectedFile); setError(null); setLoading(true); try { const data = await AppApi.analyzeTelegramImport(selectedFile) as any; setToken(data.token); setNames(data.names); // Auto-map if possible const initialMap: Record = {}; data.names.forEach((name: string) => { initialMap[name] = ''; }); setMapping(initialMap); setStep(2); } catch (err: any) { console.error(err); setError(err.message || 'Ошибка анализа файла'); } finally { setLoading(false); } }; const handleExecute = async () => { if (!token) return; setLoading(true); setError(null); try { const res = await AppApi.executeTelegramImport({ token, mapping, groupName }) as any; setImportedState({ count: 0, text: 'Импорт запущен в фоновом режиме' }); setStep(3); // Обновляем список чатов и выбираем новый await useChatStore.getState().loadChats(); if (res.chatId) { useChatStore.getState().setActiveChat(res.chatId); } } catch (err: any) { console.error(err); setError(err.message || 'Ошибка импорта'); } finally { setLoading(false); } }; const reset = () => { setStep(1); setFile(null); setToken(null); setNames([]); setMapping({}); setGroupName(''); setError(null); setImportedState(null); if (fileInputRef.current) fileInputRef.current.value = ''; }; const handleClose = () => { reset(); onClose(); }; return ( {isOpen && (
{/* Header */}

{t('importTelegram')}

{error && (
{t('error')}

{error}

)} {step === 1 && (
fileInputRef.current?.click()} className="w-24 h-24 mx-auto bg-surface-container-highest/20 rounded-[2rem] flex items-center justify-center border border-white/5 shadow-2xl cursor-pointer hover:bg-surface-container-highest/30 active:scale-95 transition-all group" >

{t('importSelectArchive')}

{t('importSelectArchiveDesc')}
{t('importLimit')}
)} {step === 2 && (

{t('importParticipants')}

{t('importParticipantsDesc')}

{names.map((name) => (
{t('importInArchive')} {name}
))}
{names.length > 2 && (
{t('importGroupName')}
setGroupName(e.target.value)} className="w-full h-14 px-5 bg-black/40 text-sm text-white/90 rounded-2xl border border-white/5 focus:border-knot-400 outline-none transition-all placeholder:text-zinc-600 font-medium" />
)}
)} {step === 3 && importedState && (

{t('importSuccess')}

{t('importCompletedDesc')}
{t('importStarted')}
)}
)}
); }