'use client'; import { useState } from 'react'; import Link from 'next/link'; import { ArrowLeft, Sparkles, Wand2, Copy, Check, Loader2, Settings } from 'lucide-react'; const GOAL_LABELS = { educational: 'Обучение', news: 'Новости', entertainment: 'Развлечение', expert: 'Экспертный', sales: 'Продажи', }; export default function ChannelView({ channel }) { const [topic, setTopic] = useState(''); const [generating, setGenerating] = useState(false); const [post, setPost] = useState(null); const [error, setError] = useState(''); const [copied, setCopied] = useState(false); const [tokens, setTokens] = useState(null); async function generate() { if (!topic.trim()) return; setGenerating(true); setError(''); setPost(null); setTokens(null); try { const createRes = await fetch('/api/generate', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ type: 'post', channelId: channel.id, topic: topic.trim(), useCritique: true, }), }); const job = await createRes.json(); if (!createRes.ok) throw new Error(job.error || 'Ошибка'); // polling let final; for (let i = 0; i < 60; i++) { await new Promise(r => setTimeout(r, 2000)); const r = await fetch(`/api/generate/${job.jobId}`); const j = await r.json(); if (j.status === 'done' || j.status === 'failed') { final = j; break; } } if (!final) throw new Error('Таймаут — попробуй ещё раз'); if (final.status === 'failed') throw new Error(final.error || 'Генерация упала'); setPost(final.result); setTokens({ in: final.tokens_in, out: final.tokens_out }); } catch (err) { setError(err.message); } finally { setGenerating(false); } } async function copy() { await navigator.clipboard.writeText(post); setCopied(true); setTimeout(() => setCopied(false), 2000); } return (
К списку каналов

{channel.name}

{GOAL_LABELS[channel.goal] || channel.goal}
{channel.niche &&

{channel.niche}

}
Настройки
{/* Generator */}

Сгенерировать пост