'use client'; /** * FromUrlModal — модальное окно «URL → черновик». * Props: * open — bool * channelId — number * onClose() * onApply({ content, imageUrl, title }) — вызывается с результатом */ import { useState } from 'react'; import { X, Link2, Loader2, Youtube, Globe, Send, AlertCircle } from 'lucide-react'; function detectSource(url) { try { const u = new URL(url); if (u.hostname.includes('youtube.com') || u.hostname.includes('youtu.be')) return 'youtube'; if (u.hostname === 't.me') return 'telegram'; return 'web'; } catch { return 'web'; } } const SOURCE_ICONS = { youtube: { Icon: Youtube, label: 'YouTube', color: 'text-red-400' }, telegram: { Icon: Send, label: 'Telegram', color: 'text-blue-400' }, web: { Icon: Globe, label: 'Сайт', color: 'text-text-mute' }, }; export default function FromUrlModal({ open, channelId, onClose, onApply }) { const [url, setUrl] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [result, setResult] = useState(null); const [edited, setEdited] = useState(''); if (!open) return null; const source = detectSource(url); const { Icon: SrcIcon, label: srcLabel, color: srcColor } = SOURCE_ICONS[source] || SOURCE_ICONS.web; async function generate() { if (!url.trim()) return; setLoading(true); setError(''); setResult(null); try { const res = await fetch('/api/generate/from-url', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ channelId, url: url.trim() }), }); const data = await res.json(); if (!res.ok) throw new Error(data.error || 'Ошибка генерации'); setResult(data); setEdited(data.content); } catch (e) { setError(e.message); } finally { setLoading(false); } } function apply() { if (!edited.trim()) return; onApply({ content: edited, imageUrl: result?.imageUrl || null, title: result?.title || '', }); handleClose(); } function handleClose() { setUrl(''); setResult(null); setEdited(''); setError(''); onClose(); } return (
{/* Шапка */}

URL → черновик

{/* Контент */}
{/* Инпут URL */}
{ setUrl(e.target.value); setResult(null); setError(''); }} disabled={loading} onKeyDown={e => e.key === 'Enter' && !loading && generate()} />
{url && (

Источник: {srcLabel}

)}
{/* Кнопка генерации */} {!result && ( )} {/* Ошибка */} {error && (
{error}
)} {/* Результат */} {result && ( <> {result.title && (
Источник: {result.title}
)}