import { useState, useEffect } from 'react'; import toast from 'react-hot-toast'; import { eventsApi, ticketsApi, emailsApi, doorApi, Event, Ticket, EmailTemplate, DoorSummary } from '@/lib/api'; /** * Loads the core data for the admin event detail page (event, tickets, active * email templates, door takings) and exposes a reload function used after * mutations. */ export function useEventDetailData(eventId: string) { const [loading, setLoading] = useState(true); const [event, setEvent] = useState(null); const [tickets, setTickets] = useState([]); const [templates, setTemplates] = useState([]); const [doorSummary, setDoorSummary] = useState(null); const loadEventData = async () => { try { const [eventRes, ticketsRes, templatesRes, doorRes] = await Promise.all([ eventsApi.getById(eventId), ticketsApi.getAll({ eventId }), emailsApi.getTemplates(), // Door takings split pre-sale from cash/bitcoin/transfer taken on the // night. It is supporting detail, so a failure here must not blank the page. doorApi.summary(eventId).catch(() => null), ]); setEvent(eventRes.event); setTickets(ticketsRes.tickets); setTemplates(templatesRes.templates.filter(t => t.isActive)); setDoorSummary(doorRes); } catch (error) { toast.error('Failed to load event data'); } finally { setLoading(false); } }; useEffect(() => { loadEventData(); }, [eventId]); return { loading, event, tickets, templates, doorSummary, loadEventData }; }