import React, { useState, useRef } from 'react'; import { useCreateLiveBlogUpdate, useUpdateLiveBlogUpdate, useLiveBlogUpdates } from '@/queries/live-blogs'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Textarea } from '@/components/ui/textarea'; import { Switch } from '@/components/ui/switch'; import { Label } from '@/components/ui/label'; import type { LiveBlogUpdate, CreateLiveBlogUpdateDto } from '@/lib/api'; import { cn } from '@/lib/utils'; import { Send, Pin, Calendar, Edit } from 'lucide-react'; interface UpdatePublisherProps { liveBlogId: string; authorId?: string; className?: string; } export function UpdatePublisher({ liveBlogId, authorId, className }: UpdatePublisherProps) { const [content, setContent] = useState(''); const [isPinned, setIsPinned] = useState(false); const [scheduledAt, setScheduledAt] = useState(''); const [isScheduleMode, setIsScheduleMode] = useState(false); const textareaRef = useRef(null); const createMutation = useCreateLiveBlogUpdate(); const updateMutation = useUpdateLiveBlogUpdate(); const [editingUpdate, setEditingUpdate] = useState(null); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!content.trim()) return; const updateData: CreateLiveBlogUpdateDto = { content: content.trim(), isPinned, authorId, ...(isScheduleMode && scheduledAt ? { scheduledAt } : {}), }; try { if (editingUpdate) { // Update existing update await updateMutation.mutateAsync({ liveBlogId, updateId: editingUpdate.id, dto: { content: content.trim(), isPinned, }, }); setEditingUpdate(null); } else { // Create new update await createMutation.mutateAsync({ liveBlogId, dto: updateData, }); } // Reset form setContent(''); setIsPinned(false); setScheduledAt(''); setIsScheduleMode(false); textareaRef.current?.focus(); } catch (error) { console.error('Failed to save update:', error); } }; const handleEdit = (update: LiveBlogUpdate) => { setContent(update.content); setIsPinned(update.isPinned); setEditingUpdate(update); setIsScheduleMode(false); textareaRef.current?.focus(); }; const handleCancelEdit = () => { setEditingUpdate(null); setContent(''); setIsPinned(false); setIsScheduleMode(false); }; const isLoading = createMutation.isPending || updateMutation.isPending; // Get minimum date for scheduling (now + 5 minutes) const minScheduleDate = new Date().toISOString().slice(0, 16); return ( {editingUpdate ? 'Edit Update' : 'Publish Update'} {editingUpdate && ( ID: {editingUpdate.id.slice(0, 8)} )}
{/* Content textarea */}