refactor: optimize crime analytics hook and improve filtering logic
This commit is contained in:
parent
4590e21c39
commit
c3eeb4051e
|
@ -30,9 +30,8 @@ export function useCrimeAnalytics(crimes: ICrimes[]) {
|
|||
availableMonths: [] as string[],
|
||||
};
|
||||
|
||||
let filteredCrimes = [...crimes];
|
||||
|
||||
const crimeIncidents = filteredCrimes.flatMap((crime: ICrimes) =>
|
||||
// Use the already filtered crimes directly, don't filter them again
|
||||
const crimeIncidents = crimes.flatMap((crime: ICrimes) =>
|
||||
crime.crime_incidents.map((incident) => ({
|
||||
id: incident.id,
|
||||
timestamp: incident.timestamp,
|
||||
|
@ -43,6 +42,8 @@ export function useCrimeAnalytics(crimes: ICrimes[]) {
|
|||
address: incident.locations.address,
|
||||
latitude: incident.locations.latitude,
|
||||
longitude: incident.locations.longitude,
|
||||
district_id: crime.district_id,
|
||||
district_name: crime.districts?.name || 'Unknown'
|
||||
}))
|
||||
);
|
||||
|
||||
|
@ -86,11 +87,12 @@ export function useCrimeAnalytics(crimes: ICrimes[]) {
|
|||
{} as Record<string, number>
|
||||
);
|
||||
|
||||
const districts = filteredCrimes.reduce(
|
||||
(acc: Record<string, number>, crime: ICrimes) => {
|
||||
const districtName = crime.districts.name || 'Unknown';
|
||||
acc[districtName] =
|
||||
(acc[districtName] || 0) + (crime.number_of_crime || 0);
|
||||
// Count by district name from the incidents, not from crimes anymore
|
||||
// since crimes are already filtered
|
||||
const districts = crimeIncidents.reduce(
|
||||
(acc: Record<string, number>, incident) => {
|
||||
const districtName = incident.district_name || 'Unknown';
|
||||
acc[districtName] = (acc[districtName] || 0) + 1;
|
||||
return acc;
|
||||
},
|
||||
{} as Record<string, number>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"use client";
|
||||
|
||||
import React, { useEffect, useState } from "react";
|
||||
import React, { useEffect, useMemo, useState } from "react";
|
||||
import {
|
||||
AlertTriangle,
|
||||
Calendar,
|
||||
|
|
|
@ -151,7 +151,7 @@ export default function CrimeMap() {
|
|||
}, [crimes, selectedSourceType]);
|
||||
|
||||
const filteredByYearAndMonth = useMemo(() => {
|
||||
if (!crimesBySourceType) return [];
|
||||
if (!crimesBySourceType || crimesBySourceType.length === 0) return [];
|
||||
|
||||
if (useAllYears) {
|
||||
if (useAllMonths) {
|
||||
|
@ -183,7 +183,8 @@ export default function CrimeMap() {
|
|||
]);
|
||||
|
||||
const filteredCrimes = useMemo(() => {
|
||||
if (!filteredByYearAndMonth) return [];
|
||||
if (!filteredByYearAndMonth || filteredByYearAndMonth.length === 0)
|
||||
return [];
|
||||
if (selectedCategory === "all") return filteredByYearAndMonth;
|
||||
|
||||
return filteredByYearAndMonth.map((crime) => {
|
||||
|
@ -345,7 +346,16 @@ export default function CrimeMap() {
|
|||
setShowEWS(true);
|
||||
};
|
||||
|
||||
// const showTimelineLayer = activeControl === "timeline";
|
||||
useEffect(() => {
|
||||
console.log(`Current source type: ${selectedSourceType}`);
|
||||
console.log(`Total crimes before filtering: ${crimes?.length || 0}`);
|
||||
console.log(
|
||||
`Total crimes after source type filtering: ${crimesBySourceType.length}`,
|
||||
);
|
||||
console.log(
|
||||
`Total crimes after all filtering: ${filteredCrimes.length}`,
|
||||
);
|
||||
}, [crimes, crimesBySourceType, filteredCrimes, selectedSourceType]);
|
||||
|
||||
return (
|
||||
<Card className="w-full p-0 border-none shadow-none h-96">
|
||||
|
@ -475,7 +485,7 @@ export default function CrimeMap() {
|
|||
selectedCategory={selectedCategory}
|
||||
selectedYear={selectedYear}
|
||||
selectedMonth={selectedMonth}
|
||||
sourceType={selectedSourceType} // Pass the sourceType
|
||||
sourceType={selectedSourceType}
|
||||
/>
|
||||
|
||||
<div className="absolute bottom-20 right-0 z-20 p-2">
|
||||
|
|
Loading…
Reference in New Issue