117 lines
3.9 KiB
TypeScript
117 lines
3.9 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { collection, onSnapshot, query, orderBy, limit, where, FirestoreError } from "firebase/firestore";
|
|
import { onAuthStateChanged } from "firebase/auth";
|
|
import { db, auth } from "@/lib/firebase";
|
|
import type { CallRecord } from "@/lib/types";
|
|
|
|
export function useCalls(limitCount = 50) {
|
|
const [calls, setCalls] = useState<CallRecord[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
let unsubFirestore: (() => void) | undefined;
|
|
|
|
const unsubAuth = onAuthStateChanged(auth, (user) => {
|
|
if (unsubFirestore) { unsubFirestore(); unsubFirestore = undefined; }
|
|
|
|
if (!user) {
|
|
setCalls([]);
|
|
setLoading(false);
|
|
return;
|
|
}
|
|
|
|
const q = query(
|
|
collection(db, "calls"),
|
|
orderBy("started_at", "desc"),
|
|
limit(limitCount)
|
|
);
|
|
const toISO = (v: any): string | null =>
|
|
v?.toDate?.()?.toISOString?.() ?? (typeof v === "string" ? v : null);
|
|
unsubFirestore = onSnapshot(q, (snap) => {
|
|
setCalls(snap.docs.map((d) => {
|
|
const data = d.data();
|
|
return { ...data, started_at: toISO(data.started_at) ?? "", ended_at: toISO(data.ended_at) } as CallRecord;
|
|
}));
|
|
setLoading(false);
|
|
}, (err: FirestoreError) => { console.error("useCalls:", err); setError(err.message); setLoading(false); });
|
|
});
|
|
|
|
return () => {
|
|
unsubAuth();
|
|
if (unsubFirestore) unsubFirestore();
|
|
};
|
|
}, [limitCount]);
|
|
|
|
return { calls, loading, error };
|
|
}
|
|
|
|
export function useCallsByIncident(incidentId: string | null) {
|
|
const [calls, setCalls] = useState<CallRecord[]>([]);
|
|
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 toISO = (v: any): string | null =>
|
|
v?.toDate?.()?.toISOString?.() ?? (typeof v === "string" ? v : null);
|
|
|
|
const q = query(collection(db, "calls"), where("incident_ids", "array-contains", incidentId));
|
|
unsubFirestore = onSnapshot(q, (snap) => {
|
|
const docs = snap.docs.map((d) => {
|
|
const data = d.data();
|
|
return { ...data, started_at: toISO(data.started_at) ?? "", ended_at: toISO(data.ended_at) } as CallRecord;
|
|
});
|
|
docs.sort((a, b) => a.started_at.localeCompare(b.started_at));
|
|
setCalls(docs);
|
|
setLoading(false);
|
|
}, (err: FirestoreError) => { console.error("useCallsByIncident:", err); setLoading(false); });
|
|
});
|
|
|
|
return () => { unsubAuth(); if (unsubFirestore) unsubFirestore(); };
|
|
}, [incidentId]);
|
|
|
|
return { calls, loading };
|
|
}
|
|
|
|
export function useActiveCalls() {
|
|
const [calls, setCalls] = useState<CallRecord[]>([]);
|
|
|
|
useEffect(() => {
|
|
let unsubFirestore: (() => void) | undefined;
|
|
|
|
const unsubAuth = onAuthStateChanged(auth, (user) => {
|
|
if (unsubFirestore) { unsubFirestore(); unsubFirestore = undefined; }
|
|
|
|
if (!user) {
|
|
setCalls([]);
|
|
return;
|
|
}
|
|
|
|
const q = query(collection(db, "calls"), where("status", "==", "active"));
|
|
const toISO = (v: any): string | null =>
|
|
v?.toDate?.()?.toISOString?.() ?? (typeof v === "string" ? v : null);
|
|
unsubFirestore = onSnapshot(q, (snap) => {
|
|
setCalls(snap.docs.map((d) => {
|
|
const data = d.data();
|
|
return { ...data, started_at: toISO(data.started_at) ?? "", ended_at: toISO(data.ended_at) } as CallRecord;
|
|
}));
|
|
}, (err: FirestoreError) => { console.error("useActiveCalls:", err); });
|
|
});
|
|
|
|
return () => {
|
|
unsubAuth();
|
|
if (unsubFirestore) unsubFirestore();
|
|
};
|
|
}, []);
|
|
|
|
return calls;
|
|
}
|