Reorganize web folder structurally
This commit is contained in:
@@ -0,0 +1,262 @@
|
||||
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';
|
||||
|
||||
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<HTMLInputElement>(null);
|
||||
|
||||
const [step, setStep] = useState<1 | 2 | 3>(1); // 1: upload, 2: map, 3: loading/done
|
||||
const [file, setFile] = useState<File | null>(null);
|
||||
const [token, setToken] = useState<string | null>(null);
|
||||
const [names, setNames] = useState<string[]>([]);
|
||||
const [mapping, setMapping] = useState<Record<string, string>>({});
|
||||
const [groupName, setGroupName] = useState('');
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [importedState, setImportedState] = useState<{ count: number; text: string } | null>(null);
|
||||
|
||||
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
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<string, string> = {};
|
||||
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: res.messagesImported, text: 'Успешно импортировано' });
|
||||
setStep(3);
|
||||
} catch (err: any) {
|
||||
console.error(err);
|
||||
setError(err.message || 'Ошибка импорта');
|
||||
setStep(1);
|
||||
} 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 (
|
||||
<AnimatePresence>
|
||||
{isOpen && (
|
||||
<div className="fixed inset-0 z-[100] flex items-center justify-center p-4">
|
||||
<motion.div
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
exit={{ opacity: 0 }}
|
||||
className="absolute inset-0 bg-black/60 backdrop-blur-sm"
|
||||
onClick={step === 3 && importedState ? handleClose : undefined}
|
||||
/>
|
||||
<motion.div
|
||||
initial={{ opacity: 0, scale: 0.95, y: 20 }}
|
||||
animate={{ opacity: 1, scale: 1, y: 0 }}
|
||||
exit={{ opacity: 0, scale: 0.95, y: 20 }}
|
||||
className="relative w-full max-w-lg bg-surface-secondary border border-border shadow-2xl rounded-2xl overflow-hidden flex flex-col max-h-[90vh]"
|
||||
>
|
||||
{/* Header */}
|
||||
<div className="h-14 px-4 flex items-center justify-between border-b border-border bg-surface-secondary/50 backdrop-blur-md shrink-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<MessageSquare size={20} className="text-knot-400" />
|
||||
<h3 className="font-semibold text-white">Импорт из Telegram</h3>
|
||||
</div>
|
||||
<button
|
||||
onClick={handleClose}
|
||||
className="p-2 -mr-2 text-zinc-400 hover:text-white hover:bg-white/10 rounded-xl transition-all"
|
||||
>
|
||||
<X size={20} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 overflow-y-auto p-6">
|
||||
{error && (
|
||||
<div className="mb-6 p-4 rounded-xl bg-red-500/10 border border-red-500/20 flex gap-3 text-red-400">
|
||||
<AlertCircle size={20} className="shrink-0" />
|
||||
<p className="text-sm">{error}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{step === 1 && (
|
||||
<div className="text-center space-y-6">
|
||||
<div className="w-20 h-20 mx-auto bg-surface-tertiary rounded-full flex items-center justify-center border border-border">
|
||||
<Upload size={32} className="text-knot-400" />
|
||||
</div>
|
||||
<div>
|
||||
<h4 className="text-lg font-medium text-white mb-2">Загрузите архив с историей</h4>
|
||||
<p className="text-sm text-zinc-400 leading-relaxed max-w-sm mx-auto">
|
||||
Скачайте историю чата из Telegram в формате HTML (сняв галочку с формата JSON). Убедитесь, что медиафайлы тоже скачаны, если хотите перенести их. Загрузите полученный ZIP архив.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<input
|
||||
type="file"
|
||||
ref={fileInputRef}
|
||||
onChange={handleFileChange}
|
||||
accept=".zip"
|
||||
className="hidden"
|
||||
/>
|
||||
|
||||
<button
|
||||
onClick={() => fileInputRef.current?.click()}
|
||||
disabled={loading}
|
||||
className="h-12 px-6 bg-knot-500 hover:bg-knot-600 active:bg-knot-700 text-white font-medium rounded-xl transition-colors inline-flex items-center justify-center gap-2 disabled:opacity-50 disabled:cursor-not-allowed mx-auto"
|
||||
>
|
||||
{loading ? (
|
||||
<Loader2 size={18} className="animate-spin" />
|
||||
) : (
|
||||
<>
|
||||
<Upload size={18} />
|
||||
Выбрать ZIP-архив
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{step === 2 && (
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<h4 className="text-lg font-medium text-white mb-2">Кто есть кто?</h4>
|
||||
<p className="text-sm text-zinc-400">
|
||||
Мы нашли {names.length} имён в архиве. Укажите, какому контакту в Knot они соответствуют. Одно из имён должно принадлежать вам.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="space-y-4">
|
||||
{names.map((name) => (
|
||||
<div key={name} className="flex flex-col gap-2 p-4 rounded-xl border border-border bg-surface-tertiary">
|
||||
<span className="text-sm font-medium text-white">Сообщения от: "{name}"</span>
|
||||
<select
|
||||
value={mapping[name] || ''}
|
||||
onChange={(e) => setMapping({ ...mapping, [name]: e.target.value })}
|
||||
className="w-full h-11 px-3 bg-surface-secondary text-sm text-white rounded-lg border border-border focus:border-knot-500 outline-none transition-colors"
|
||||
>
|
||||
<option value="">-- Выберите пользователя --</option>
|
||||
<option value={user?.id}>Это я ({user?.displayName || user?.username})</option>
|
||||
{friends.map(f => (
|
||||
<option key={f.id} value={f.id}>
|
||||
Контакт: {f.displayName || f.username}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{names.length > 2 && (
|
||||
<div className="pt-2">
|
||||
<h4 className="text-sm font-medium text-white mb-2">Название для группового чата</h4>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Например, Моя группа"
|
||||
value={groupName}
|
||||
onChange={(e) => setGroupName(e.target.value)}
|
||||
className="w-full h-11 px-3 bg-surface-secondary text-sm text-white rounded-lg border border-border focus:border-knot-500 outline-none transition-colors placeholder:text-zinc-600"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="pt-4 flex items-center justify-end gap-3">
|
||||
<button
|
||||
onClick={reset}
|
||||
disabled={loading}
|
||||
className="px-5 py-2.5 text-sm font-medium text-zinc-400 hover:text-white transition-colors"
|
||||
>
|
||||
Отмена
|
||||
</button>
|
||||
<button
|
||||
onClick={handleExecute}
|
||||
disabled={loading || names.some(n => !mapping[n]) || (names.length > 2 && !groupName.trim())}
|
||||
className="px-6 py-2.5 bg-knot-500 hover:bg-knot-600 disabled:bg-surface-tertiary disabled:text-zinc-500 text-white text-sm font-medium rounded-xl transition-colors flex items-center gap-2"
|
||||
>
|
||||
{loading ? <Loader2 size={16} className="animate-spin" /> : <Check size={16} />}
|
||||
Импортировать
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{step === 3 && importedState && (
|
||||
<div className="text-center py-8 space-y-4">
|
||||
<div className="w-16 h-16 mx-auto bg-green-500/20 text-green-400 rounded-full flex items-center justify-center border border-green-500/30">
|
||||
<Check size={32} />
|
||||
</div>
|
||||
<div>
|
||||
<h4 className="text-xl font-medium text-white mb-2">Готово!</h4>
|
||||
<p className="text-sm text-zinc-400">
|
||||
Импорт завершен. Сообщений: <strong className="text-white">{importedState.count}</strong>.
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
onClick={handleClose}
|
||||
className="mt-6 h-11 px-6 bg-surface-tertiary hover:bg-surface-hover active:bg-surface-secondary text-white font-medium rounded-xl transition-colors"
|
||||
>
|
||||
Закрыть
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</motion.div>
|
||||
</div>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user