import React from 'react'; import { useTranslation } from 'react-i18next'; import { toast } from 'react-hot-toast'; import { AlertCircle, Send, RefreshCw } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { Badge } from '@/components/ui/badge'; import type { GuestNotificationSummary, SendGuestNotificationPayload } from '../api'; import { listGuestNotifications, sendGuestNotification } from '../api'; const TYPE_OPTIONS = [ { value: 'broadcast', label: 'Allgemein' }, { value: 'support_tip', label: 'Support-Hinweis' }, { value: 'upload_alert', label: 'Upload-Status' }, { value: 'feedback_request', label: 'Feedback' }, ]; const AUDIENCE_OPTIONS = [ { value: 'all', label: 'Alle Gäste' }, { value: 'guest', label: 'Einzelne Geräte-ID' }, ]; type GuestBroadcastCardProps = { eventSlug: string; eventName?: string | null; }; export function GuestBroadcastCard({ eventSlug, eventName }: GuestBroadcastCardProps) { const { t } = useTranslation('management'); const [form, setForm] = React.useState({ title: '', message: '', type: 'broadcast', audience: 'all', guest_identifier: '', cta_label: '', cta_url: '', expires_in_minutes: 120, }); const [history, setHistory] = React.useState([]); const [loadingHistory, setLoadingHistory] = React.useState(true); const [submitting, setSubmitting] = React.useState(false); const [error, setError] = React.useState(null); const loadHistory = React.useCallback(async () => { setLoadingHistory(true); try { const data = await listGuestNotifications(eventSlug); setHistory(data.slice(0, 5)); } catch (err) { console.error(err); } finally { setLoadingHistory(false); } }, [eventSlug]); React.useEffect(() => { void loadHistory(); }, [loadHistory]); function updateField(field: string, value: string): void { setForm((prev) => ({ ...prev, [field]: value })); } async function handleSubmit(event: React.FormEvent): Promise { event.preventDefault(); setSubmitting(true); setError(null); const payload: SendGuestNotificationPayload = { title: form.title.trim(), message: form.message.trim(), type: form.type, audience: form.audience as 'all' | 'guest', guest_identifier: form.audience === 'guest' ? form.guest_identifier.trim() : undefined, expires_in_minutes: Number(form.expires_in_minutes) || undefined, cta: form.cta_label.trim() && form.cta_url.trim() ? { label: form.cta_label.trim(), url: form.cta_url.trim() } : undefined, }; try { await sendGuestNotification(eventSlug, payload); toast.success(t('events.notifications.toastSuccess', 'Nachricht gesendet.')); setForm((prev) => ({ ...prev, title: '', message: '', guest_identifier: '', cta_label: '', cta_url: '', })); void loadHistory(); } catch (err) { console.error(err); setError(t('events.notifications.toastError', 'Nachricht konnte nicht gesendet werden.')); } finally { setSubmitting(false); } } return (

{t('events.notifications.description', 'Sende kurze Hinweise direkt an deine Gäste. Ideal für Programmpunkte, Upload-Hilfe oder Feedback-Aufrufe.')} {eventName && {eventName}}

{form.audience === 'guest' && (
updateField('guest_identifier', event.target.value)} placeholder="z. B. device-123" required />
)}
updateField('title', event.target.value)} placeholder={t('events.notifications.titlePlaceholder', 'Buffet schließt in 10 Minuten')} required />