84 lines
2.6 KiB
TypeScript
84 lines
2.6 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 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;
|
|
}
|