import { useState } from 'react'; import { useLiveBlog, useDeleteLiveBlogUpdate, useUpdateLiveBlog, useDeleteLiveBlog } from '@/queries/live-blogs'; import { UpdatePublisher } from './UpdatePublisher'; import { LiveBlogUpdate } from '@/components/features/live-blog/LiveBlogUpdate'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import type { LiveBlogUpdate as ApiLiveBlogUpdate, UpdateLiveBlogDto } from '@/lib/api'; import { ArrowLeft, Edit, Play, Square, Archive, Eye, MessageSquare, Pin, PinOff } from 'lucide-react'; interface LiveBlogManagerProps { slug: string; onBack?: () => void; className?: string; } export function LiveBlogManager({ slug, onBack, className }: LiveBlogManagerProps) { const [activeTab, setActiveTab] = useState('updates'); const [isEditing, setIsEditing] = useState(false); const [editForm, setEditForm] = useState({}); const { data: liveBlog, isLoading, error } = useLiveBlog(slug); const deleteUpdateMutation = useDeleteLiveBlogUpdate(); const updateLiveBlogMutation = useUpdateLiveBlog(); const deleteLiveBlogMutation = useDeleteLiveBlog(); const handleDeleteUpdate = async (update: ApiLiveBlogUpdate) => { if (!liveBlog) return; if (confirm('Are you sure you want to delete this update? This action cannot be undone.')) { try { await deleteUpdateMutation.mutateAsync({ liveBlogId: liveBlog.id, updateId: update.id, }); } catch (error) { console.error('Failed to delete update:', error); } } }; const handleEditUpdate = (update: ApiLiveBlogUpdate) => { // The UpdatePublisher component handles editing internally console.log('Editing update:', update); }; const handleStartEdit = () => { if (!liveBlog) return; setIsEditing(true); setEditForm({ title: liveBlog.title, slug: liveBlog.slug, description: liveBlog.description || '', status: liveBlog.status, }); }; const handleCancelEdit = () => { setIsEditing(false); setEditForm({}); }; const handleSaveEdit = async () => { if (!liveBlog) return; try { await updateLiveBlogMutation.mutateAsync({ id: liveBlog.id, dto: editForm, }); setIsEditing(false); setEditForm({}); } catch (error) { console.error('Failed to update live blog:', error); } }; const handleStatusChange = async (newStatus: 'draft' | 'live' | 'ended' | 'archived') => { if (!liveBlog) return; try { await updateLiveBlogMutation.mutateAsync({ id: liveBlog.id, dto: { status: newStatus }, }); } catch (error) { console.error('Failed to update status:', error); alert(`Failed to update status: ${error instanceof Error ? error.message : 'Unknown error'}`); } }; const handleTogglePin = async () => { if (!liveBlog) return; try { await updateLiveBlogMutation.mutateAsync({ id: liveBlog.id, dto: { isPinned: !liveBlog.isPinned }, }); } catch (error) { console.error('Failed to toggle pin:', error); alert(`Failed to toggle pin: ${error instanceof Error ? error.message : 'Unknown error'}`); } }; const handleDeleteLiveBlog = async () => { if (!liveBlog) return; if (confirm('Are you sure you want to delete this live blog? This action cannot be undone and will delete all updates.')) { try { await deleteLiveBlogMutation.mutateAsync(liveBlog.id); if (onBack) onBack(); } catch (error) { console.error('Failed to delete live blog:', error); } } }; if (isLoading) { return (
); } if (error || !liveBlog) { return (

Failed to load live blog

{error instanceof Error ? error.message : 'Unknown error occurred'}

{onBack && ( )}
); } const getStatusColor = (status: string) => { switch (status) { case 'live': return 'bg-green-100 text-green-800 border-green-200'; case 'draft': return 'bg-gray-100 text-gray-800 border-gray-200'; case 'ended': return 'bg-red-100 text-red-800 border-red-200'; case 'archived': return 'bg-blue-100 text-blue-800 border-blue-200'; default: return 'bg-gray-100 text-gray-800 border-gray-200'; } }; const getStatusIcon = (status: string) => { switch (status) { case 'live': return ; case 'draft': return ; case 'ended': return ; case 'archived': return ; default: return null; } }; return (
{/* Header */}
{onBack && ( )} {liveBlog.title}
{liveBlog.description && (

{liveBlog.description}

)}
{getStatusIcon(liveBlog.status)} {liveBlog.status}
{liveBlog.viewCount} views
{liveBlog.updates?.length || 0} updates
{liveBlog.author && (
{liveBlog.author.avatar && ( {liveBlog.author.name} )} {liveBlog.author.name}
)}
{/* Main content */} Updates Settings {/* Recent updates preview */} {liveBlog.updates && liveBlog.updates.length > 0 && ( All Updates
{liveBlog.updates.map((update) => ( { // Handle pin toggle console.log('Toggle pin for update:', update); }} /> ))}
)}
Live Blog Settings {!isEditing ? ( ) : (
)}
{isEditing ? (
setEditForm({ ...editForm, title: e.target.value })} />
setEditForm({ ...editForm, slug: e.target.value })} placeholder="URL-friendly identifier" />