Incident started_at is an isoformat() string (incident_correlator.py, routers/incidents.py), not a Firestore timestamp like calls. The range bounds were Dates, which Firestore compares by type, so any date range returned zero incidents. Bounds are now UTC ISO strings in the same "+00:00" shape, which order lexicographically by time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
163 lines
5.3 KiB
TypeScript
163 lines
5.3 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { collection, doc, onSnapshot, query, orderBy, limit, where, FirestoreError } from "firebase/firestore";
|
|
import { onAuthStateChanged } from "firebase/auth";
|
|
import { db, auth } from "@/lib/firebase";
|
|
import { useAuth } from "@/components/AuthProvider";
|
|
import type { IncidentRecord } from "@/lib/types";
|
|
|
|
const toISO = (v: unknown): string =>
|
|
(v as { toDate?: () => Date })?.toDate?.()?.toISOString?.() ??
|
|
(typeof v === "string" ? v : new Date().toISOString());
|
|
|
|
export function useIncidents(limitCount = 100, dateFrom?: Date, dateTo?: Date) {
|
|
const [incidents, setIncidents] = useState<IncidentRecord[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
// A full page means there may be older incidents past the limit; a short
|
|
// page means the query reached the end of the collection.
|
|
const [hasMore, setHasMore] = useState(false);
|
|
const { orgId } = useAuth();
|
|
|
|
// Stable ms values so the effect dependency doesn't fire on every render
|
|
const dateFromMs = dateFrom?.getTime();
|
|
const dateToMs = dateTo?.getTime();
|
|
|
|
useEffect(() => {
|
|
let unsubFirestore: (() => void) | undefined;
|
|
|
|
const unsubAuth = onAuthStateChanged(auth, (user) => {
|
|
if (unsubFirestore) { unsubFirestore(); unsubFirestore = undefined; }
|
|
|
|
if (!user) {
|
|
setIncidents([]);
|
|
setLoading(false);
|
|
return;
|
|
}
|
|
if (!orgId) {
|
|
setIncidents([]);
|
|
setLoading(false);
|
|
return;
|
|
}
|
|
|
|
// A range on the ordered field rides the existing org_id/started_at index.
|
|
// Incident started_at is stored as a Python isoformat() STRING
|
|
// ("2026-09-20T12:00:00.123456+00:00", incident_correlator.py), not a
|
|
// Firestore timestamp — unlike calls. A Date bound compares by type and
|
|
// matches nothing, so the bounds go in as UTC ISO strings in the same
|
|
// shape, which then compare lexicographically in time order.
|
|
const isoBound = (ms: number) => new Date(ms).toISOString().replace("Z", "+00:00");
|
|
const q = query(
|
|
collection(db, "incidents"),
|
|
where("org_id", "==", orgId),
|
|
...(dateFromMs != null ? [where("started_at", ">=", isoBound(dateFromMs))] : []),
|
|
...(dateToMs != null ? [where("started_at", "<=", isoBound(dateToMs))] : []),
|
|
orderBy("started_at", "desc"),
|
|
limit(limitCount)
|
|
);
|
|
unsubFirestore = onSnapshot(q, (snap) => {
|
|
setIncidents(snap.docs.map((d) => {
|
|
const data = d.data();
|
|
return {
|
|
...data,
|
|
started_at: toISO(data.started_at),
|
|
updated_at: toISO(data.updated_at),
|
|
} as IncidentRecord;
|
|
}));
|
|
setHasMore(snap.size >= limitCount);
|
|
setLoading(false);
|
|
}, (err: FirestoreError) => {
|
|
console.error("useIncidents:", err);
|
|
setError(err.message);
|
|
setLoading(false);
|
|
});
|
|
});
|
|
|
|
return () => {
|
|
unsubAuth();
|
|
if (unsubFirestore) unsubFirestore();
|
|
};
|
|
}, [limitCount, dateFromMs, dateToMs, orgId]);
|
|
|
|
return { incidents, loading, error, hasMore };
|
|
}
|
|
|
|
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[]>([]);
|
|
const { orgId } = useAuth();
|
|
|
|
useEffect(() => {
|
|
let unsubFirestore: (() => void) | undefined;
|
|
|
|
const unsubAuth = onAuthStateChanged(auth, (user) => {
|
|
if (unsubFirestore) { unsubFirestore(); unsubFirestore = undefined; }
|
|
|
|
if (!user) {
|
|
setIncidents([]);
|
|
return;
|
|
}
|
|
if (!orgId) {
|
|
setIncidents([]);
|
|
return;
|
|
}
|
|
|
|
const q = query(collection(db, "incidents"), where("org_id", "==", orgId), where("status", "==", "active"));
|
|
unsubFirestore = onSnapshot(q, (snap) => {
|
|
setIncidents(snap.docs.map((d) => {
|
|
const data = d.data();
|
|
return {
|
|
...data,
|
|
started_at: toISO(data.started_at),
|
|
updated_at: toISO(data.updated_at),
|
|
} as IncidentRecord;
|
|
}));
|
|
}, (err: FirestoreError) => { console.error("useActiveIncidents:", err); });
|
|
});
|
|
|
|
return () => {
|
|
unsubAuth();
|
|
if (unsubFirestore) unsubFirestore();
|
|
};
|
|
}, [orgId]);
|
|
|
|
return incidents;
|
|
}
|