Skip to content

Commit 184ac2f

Browse files
committed
fix: weather update consistency
1 parent d6769c7 commit 184ac2f

4 files changed

Lines changed: 28 additions & 41 deletions

File tree

src/features/weather/ActiveWeather.jsx

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { useTranslation } from 'react-i18next'
1010

1111
import { useMemory } from '@store/useMemory'
1212
import { useStorage } from '@store/useStorage'
13-
import { apolloClient } from '@services/apollo'
1413
import { Header } from '@components/dialogs/Header'
1514
import { Footer } from '@components/dialogs/Footer'
1615
import { Img } from '@components/Img'
@@ -89,42 +88,27 @@ function Weather({ gameplay_condition, ...props }) {
8988
)
9089
}
9190

92-
const WeatherMemo = React.memo(
93-
Weather,
94-
(prev, next) => prev.gameplay_condition === next.gameplay_condition,
95-
)
96-
97-
export function ActiveWeather() {
91+
/**
92+
* @param {{ weatherData: import('@rm/types').Weather[] }} props
93+
*/
94+
export function ActiveWeather({ weatherData }) {
9895
const weatherEnabled = useStorage((s) => s.filters?.weather?.enabled ?? false)
9996
const location = useStorage((s) => s.location)
10097
const zoom = useStorage((s) => s.zoom)
10198
const allowedZoom = useMemory((s) => s.config.general.activeWeatherZoom)
10299

103-
const [active, setActive] = React.useState(
104-
/** @type {import('@rm/types').Weather | null} */ (null),
100+
const active = React.useMemo(
101+
() =>
102+
zoom > allowedZoom
103+
? weatherData.find(
104+
(cell) =>
105+
Array.isArray(cell?.polygon) &&
106+
booleanPointInPolygon(point(location), polygon([cell.polygon])),
107+
) || null
108+
: null,
109+
[allowedZoom, location, weatherData, zoom],
105110
)
106111

107-
React.useEffect(() => {
108-
if (zoom > allowedZoom) {
109-
const weatherCache = Object.values(apolloClient.cache.extract()).find(
110-
(x) =>
111-
x.__typename === 'Weather' &&
112-
// @ts-ignore
113-
booleanPointInPolygon(point(location), polygon([x.polygon])),
114-
)
115-
if (
116-
weatherCache &&
117-
'gameplay_condition' in weatherCache &&
118-
weatherCache?.gameplay_condition !== active?.gameplay_condition
119-
) {
120-
// @ts-ignore
121-
setActive(weatherCache)
122-
}
123-
} else {
124-
setActive(null)
125-
}
126-
}, [location, zoom, allowedZoom])
127-
128112
if (!weatherEnabled || !active) return null
129-
return <WeatherMemo {...active} />
113+
return <Weather {...active} />
130114
}

src/features/weather/WeatherPopup.jsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ const Timer = ({ updated, ts = Date.now() / 1000 }) => {
8989
: 'error.main'
9090

9191
React.useEffect(() => {
92-
const timer2 = setTimeout(() => {
92+
setTimer(getTimeUntil(updated * 1000))
93+
const timerId = setInterval(() => {
9394
setTimer(getTimeUntil(updated * 1000))
9495
}, 1000)
95-
return () => clearTimeout(timer2)
96-
})
96+
return () => clearInterval(timerId)
97+
}, [updated])
9798

9899
return (
99100
<>

src/pages/map/components/Container.jsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { useStorage } from '@store/useStorage'
77
import { useMapStore } from '@store/useMapStore'
88
import { ScanOnDemand } from '@features/scanner'
99
import { WebhookMarker, WebhookAreaSelection } from '@features/webhooks'
10-
import { ActiveWeather } from '@features/weather'
1110
import { timeCheck } from '@utils/timeCheck'
1211

1312
import { Effects } from './Effects'
@@ -66,7 +65,6 @@ export function Container() {
6665
<WebhookMarker />
6766
<WebhookAreaSelection />
6867
<Nav />
69-
<ActiveWeather />
7068
</MapContainer>
7169
)
7270
}

src/pages/map/components/QueryData.jsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { FILTER_SKIP_LIST } from '@assets/constants'
1212
import { Notification } from '@components/Notification'
1313
import { GenerateCells } from '@features/s2cell'
1414
import { RouteLayer } from '@features/route'
15+
import { ActiveWeather } from '@features/weather'
1516
import { useAnalytics } from '@hooks/useAnalytics'
1617
import { useProcessError } from '@hooks/useProcessError'
1718
import { normalizeCategory } from '@utils/normalizeCategory'
@@ -230,10 +231,13 @@ function QueryData({ category, timeout }) {
230231
}
231232

232233
return (
233-
<Clustering category={category}>
234-
{filteredData.map((each) => (
235-
<Component key={each.id || category} {...each} />
236-
))}
237-
</Clustering>
234+
<>
235+
<Clustering category={category}>
236+
{filteredData.map((each) => (
237+
<Component key={each.id || category} {...each} />
238+
))}
239+
</Clustering>
240+
{category === 'weather' && <ActiveWeather weatherData={filteredData} />}
241+
</>
238242
)
239243
}

0 commit comments

Comments
 (0)