Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 2 additions & 38 deletions src/components/LocationMap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useState, useEffect } from 'react';
import L from 'leaflet';
import { eventBus } from '../core/events';
import { getPollutantColor } from '../services/airQualityService';
import { useCommunityReports } from '../hooks/useCommunityReports';
import PropTypes from "prop-types";

const COMMUNITY_REPORTS_STORAGE_KEY = 'pollution-community-reports';
Expand All @@ -18,24 +19,6 @@ const POLLUTANT_LAYERS = [
{ key: 'carbon_monoxide', label: 'CO', limit: 4000 },
];

function readGeotaggedCommunityReports() {
try {
const raw = localStorage.getItem(COMMUNITY_REPORTS_STORAGE_KEY);
if (!raw) return [];
const parsed = JSON.parse(raw);
if (!Array.isArray(parsed)) return [];
return parsed.filter(
(report) =>
report &&
typeof report.latitude === 'number' &&
typeof report.longitude === 'number' &&
!isNaN(report.latitude) &&
!isNaN(report.longitude)
);
} catch {
return [];
}
}

function readGeotaggedSymptomReports() {
try {
Expand Down Expand Up @@ -70,29 +53,10 @@ function readGeotaggedSymptomReports() {
export default function LocationMap({ center, nearbyPoints, confidenceScore, windData, windError }) {
const [showWind, setShowWind] = useState(false);
const [showCommunityReports, setShowCommunityReports] = useState(false);
const [communityReports, setCommunityReports] = useState(() => readGeotaggedCommunityReports());
const communityReports = useCommunityReports();
const [showSymptomReports, setShowSymptomReports] = useState(false);
const [symptomReports, setSymptomReports] = useState(() => readGeotaggedSymptomReports());
const [selectedLayer, setSelectedLayer] = useState('aqi');
useEffect(() => {
const updateReports = () => {
setCommunityReports(readGeotaggedCommunityReports());
};

eventBus.on('COMMUNITY_REPORT_SUBMITTED', updateReports);

const handleStorage = (e) => {
if (!e.key || e.key === COMMUNITY_REPORTS_STORAGE_KEY) {
updateReports();
}
};
window.addEventListener('storage', handleStorage);

return () => {
eventBus.off('COMMUNITY_REPORT_SUBMITTED', updateReports);
window.removeEventListener('storage', handleStorage);
};
}, []);

useEffect(() => {
const updateSymptomReports = () => {
Expand Down
58 changes: 58 additions & 0 deletions src/hooks/useCommunityReports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useState, useEffect } from 'react';
import { eventBus } from '../core/events';

const COMMUNITY_REPORTS_STORAGE_KEY = 'pollution-community-reports';

function readGeotaggedCommunityReports() {
try {
const raw = localStorage.getItem(COMMUNITY_REPORTS_STORAGE_KEY);
if (!raw) return [];
const parsed = JSON.parse(raw);
if (!Array.isArray(parsed)) return [];
return parsed.filter(
(report) =>
report &&
typeof report.latitude === 'number' &&
typeof report.longitude === 'number' &&
!isNaN(report.latitude) &&
!isNaN(report.longitude)
);
} catch {
return [];
}
}

/**
* Reads geotagged community reports from localStorage and keeps them in sync with
* COMMUNITY_REPORT_SUBMITTED events and cross-tab storage changes.
*
* Extracted out of LocationMap so the component stays presentational and the
* localStorage side effect can be mocked in tests instead of touching real storage.
*
* @returns {Array<Object>} Geotagged community reports.
*/
export function useCommunityReports() {
const [communityReports, setCommunityReports] = useState(() => readGeotaggedCommunityReports());

useEffect(() => {
const updateReports = () => {
setCommunityReports(readGeotaggedCommunityReports());
};

eventBus.on('COMMUNITY_REPORT_SUBMITTED', updateReports);

const handleStorage = (e) => {
if (!e.key || e.key === COMMUNITY_REPORTS_STORAGE_KEY) {
updateReports();
}
};
window.addEventListener('storage', handleStorage);

return () => {
eventBus.off('COMMUNITY_REPORT_SUBMITTED', updateReports);
window.removeEventListener('storage', handleStorage);
};
}, []);

return communityReports;
}
Loading