big ui and intel updates
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { collection, onSnapshot, query, orderBy, limit, where, FirestoreError } from "firebase/firestore";
|
||||
import { collection, doc, onSnapshot, query, orderBy, limit, where, FirestoreError } from "firebase/firestore";
|
||||
import { onAuthStateChanged } from "firebase/auth";
|
||||
import { db, auth } from "@/lib/firebase";
|
||||
import type { IncidentRecord } from "@/lib/types";
|
||||
@@ -58,6 +58,43 @@ export function useIncidents(limitCount = 100) {
|
||||
return { incidents, loading, error };
|
||||
}
|
||||
|
||||
export function useIncident(incidentId: string | null) {
|
||||
const [incident, setIncident] = useState<IncidentRecord | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
if (!incidentId) { setLoading(false); return; }
|
||||
let unsubFirestore: (() => void) | undefined;
|
||||
|
||||
const unsubAuth = onAuthStateChanged(auth, (user) => {
|
||||
if (unsubFirestore) { unsubFirestore(); unsubFirestore = undefined; }
|
||||
if (!user) { setLoading(false); return; }
|
||||
|
||||
const ref = doc(db, "incidents", incidentId);
|
||||
unsubFirestore = onSnapshot(ref, (snap) => {
|
||||
if (snap.exists()) {
|
||||
const data = snap.data();
|
||||
setIncident({
|
||||
...data,
|
||||
started_at: toISO(data.started_at),
|
||||
updated_at: toISO(data.updated_at),
|
||||
} as IncidentRecord);
|
||||
} else {
|
||||
setIncident(null);
|
||||
}
|
||||
setLoading(false);
|
||||
}, (err: FirestoreError) => {
|
||||
console.error("useIncident:", err);
|
||||
setLoading(false);
|
||||
});
|
||||
});
|
||||
|
||||
return () => { unsubAuth(); if (unsubFirestore) unsubFirestore(); };
|
||||
}, [incidentId]);
|
||||
|
||||
return { incident, loading };
|
||||
}
|
||||
|
||||
export function useActiveIncidents() {
|
||||
const [incidents, setIncidents] = useState<IncidentRecord[]>([]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user