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
+6 -9
View File
@@ -14,8 +14,6 @@ const inputCls =
function filterCalls(calls: CallRecord[], filters: Filters): CallRecord[] { function filterCalls(calls: CallRecord[], filters: Filters): CallRecord[] {
const q = filters.query.trim().toLowerCase(); const q = filters.query.trim().toLowerCase();
const tgid = filters.tgid.trim(); const tgid = filters.tgid.trim();
const fromMs = filters.dateFrom ? new Date(filters.dateFrom + "T00:00:00").getTime() : null;
const toMs = filters.dateTo ? new Date(filters.dateTo + "T23:59:59").getTime() : null;
return calls.filter((c) => { return calls.filter((c) => {
// System filter // System filter
@@ -24,11 +22,6 @@ function filterCalls(calls: CallRecord[], filters: Filters): CallRecord[] {
// TGID filter (exact match on the number) // TGID filter (exact match on the number)
if (tgid && String(c.talkgroup_id ?? "") !== tgid) return false; if (tgid && String(c.talkgroup_id ?? "") !== tgid) return false;
// Date range
const ts = new Date(c.started_at).getTime();
if (fromMs !== null && ts < fromMs) return false;
if (toMs !== null && ts > toMs) return false;
// Free-text: talkgroup name, node_id, transcript, tags // Free-text: talkgroup name, node_id, transcript, tags
if (q) { if (q) {
const hay = [ const hay = [
@@ -67,12 +60,16 @@ function isActive(f: Filters) {
export default function CallsPage() { export default function CallsPage() {
const [limitCount, setLimitCount] = useState(100); const [limitCount, setLimitCount] = useState(100);
const { calls, loading } = useCalls(limitCount); const [filters, setFilters] = useState<Filters>(DEFAULT_FILTERS);
const dateFrom = filters.dateFrom ? new Date(filters.dateFrom + "T00:00:00") : undefined;
const dateTo = filters.dateTo ? new Date(filters.dateTo + "T23:59:59") : undefined;
const { calls, loading } = useCalls(limitCount, dateFrom, dateTo);
const { systems } = useSystems(); const { systems } = useSystems();
const { isAdmin } = useAuth(); const { isAdmin } = useAuth();
const systemMap = Object.fromEntries(systems.map((s) => [s.system_id, s])); const systemMap = Object.fromEntries(systems.map((s) => [s.system_id, s]));
const [filters, setFilters] = useState<Filters>(DEFAULT_FILTERS);
const [showFilters, setShowFilters] = useState(false); const [showFilters, setShowFilters] = useState(false);
function set<K extends keyof Filters>(key: K, value: string) { function set<K extends keyof Filters>(key: K, value: string) {
+15 -6
View File
@@ -6,11 +6,15 @@ import { onAuthStateChanged } from "firebase/auth";
import { db, auth } from "@/lib/firebase"; import { db, auth } from "@/lib/firebase";
import type { CallRecord } from "@/lib/types"; 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 [calls, setCalls] = useState<CallRecord[]>([]);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null); 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(() => { useEffect(() => {
let unsubFirestore: (() => void) | undefined; let unsubFirestore: (() => void) | undefined;
@@ -23,11 +27,16 @@ export function useCalls(limitCount = 50) {
return; return;
} }
const q = query( const from = dateFromMs != null ? new Date(dateFromMs) : undefined;
collection(db, "calls"), const to = dateToMs != null ? new Date(dateToMs) : undefined;
const constraints = [
...(from ? [where("started_at", ">=", from)] : []),
...(to ? [where("started_at", "<=", to)] : []),
orderBy("started_at", "desc"), orderBy("started_at", "desc"),
limit(limitCount) limit(limitCount),
); ];
const q = query(collection(db, "calls"), ...constraints);
const toISO = (v: any): string | null => const toISO = (v: any): string | null =>
v?.toDate?.()?.toISOString?.() ?? (typeof v === "string" ? v : null); v?.toDate?.()?.toISOString?.() ?? (typeof v === "string" ? v : null);
unsubFirestore = onSnapshot(q, (snap) => { unsubFirestore = onSnapshot(q, (snap) => {
@@ -43,7 +52,7 @@ export function useCalls(limitCount = 50) {
unsubAuth(); unsubAuth();
if (unsubFirestore) unsubFirestore(); if (unsubFirestore) unsubFirestore();
}; };
}, [limitCount]); }, [limitCount, dateFromMs, dateToMs]);
return { calls, loading, error }; return { calls, loading, error };
} }