fix date filter

This commit is contained in:
Logan
2026-05-23 13:28:23 -04:00
parent 0ceb0227c8
commit 9cf8fd4221
2 changed files with 23 additions and 17 deletions
+15 -6
View File
@@ -6,11 +6,15 @@ import { onAuthStateChanged } from "firebase/auth";
import { db, auth } from "@/lib/firebase";
import type { CallRecord } from "@/lib/types";
export function useCalls(limitCount = 50) {
export function useCalls(limitCount = 50, dateFrom?: Date, dateTo?: Date) {
const [calls, setCalls] = useState<CallRecord[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
// 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;
@@ -23,11 +27,16 @@ export function useCalls(limitCount = 50) {
return;
}
const q = query(
collection(db, "calls"),
const from = dateFromMs != null ? new Date(dateFromMs) : undefined;
const to = dateToMs != null ? new Date(dateToMs) : undefined;
const constraints = [
...(from ? [where("started_at", ">=", from)] : []),
...(to ? [where("started_at", "<=", to)] : []),
orderBy("started_at", "desc"),
limit(limitCount)
);
limit(limitCount),
];
const q = query(collection(db, "calls"), ...constraints);
const toISO = (v: any): string | null =>
v?.toDate?.()?.toISOString?.() ?? (typeof v === "string" ? v : null);
unsubFirestore = onSnapshot(q, (snap) => {
@@ -43,7 +52,7 @@ export function useCalls(limitCount = 50) {
unsubAuth();
if (unsubFirestore) unsubFirestore();
};
}, [limitCount]);
}, [limitCount, dateFromMs, dateToMs]);
return { calls, loading, error };
}