import { useState, useCallback } from "react"; import { getIncidents, createIncident, updateIncident, deleteIncident, } from "@/lib/db"; import type { Incident, IncidentDraft } from "@/types/incident"; export function useIncidents() { const [incidents, setIncidents] = useState([]); const [loading, setLoading] = useState(false); const refresh = useCallback(async (limit = 10) => { setLoading(true); const data = await getIncidents(limit); setIncidents(data); setLoading(false); }, []); const add = useCallback(async (draft: IncidentDraft) => { const created = await createIncident(draft); setIncidents((prev) => [created, ...prev]); return created; }, []); const update = useCallback( async (id: string, updates: Partial) => { await updateIncident(id, updates); setIncidents((prev) => prev.map((inc) => inc.id === id ? { ...inc, ...updates, updatedAt: new Date().toISOString() } : inc ) ); }, [] ); const remove = useCallback(async (id: string) => { await deleteIncident(id); setIncidents((prev) => prev.filter((inc) => inc.id !== id)); }, []); return { incidents, loading, refresh, add, update, remove }; }