Skip to content

Commit

Permalink
fix: active weather
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Jan 11, 2024
1 parent 7def04b commit 6541aa8
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions src/components/layout/general/ActiveWeather.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import DialogContent from '@mui/material/DialogContent'
import Box from '@mui/material/Box'
import booleanPointInPolygon from '@turf/boolean-point-in-polygon'
import { point, polygon } from '@turf/helpers'
import { useQuery } from '@apollo/client'
import { useTranslation } from 'react-i18next'

import WeatherPopup from '@components/popups/Weather'
import { useMemory } from '@hooks/useMemory'
import { useStorage } from '@hooks/useStorage'
import getAllWeather from '@services/queries/weather'
import { getQueryArgs } from '@services/functions/getQueryArgs'
import { apolloClient } from '@services/apollo'

import Header from './Header'
import Footer from './Footer'
Expand Down Expand Up @@ -48,30 +46,27 @@ export default function ActiveWeather() {
const allowedZoom = useMemory((s) => s.config.general.activeWeatherZoom)
const { t } = useTranslation()

const { data, previousData } = useQuery(getAllWeather, {
fetchPolicy: 'cache-only',
skip: !weatherEnabled,
variables: {
...getQueryArgs(),
filters: {
onlyAreas: useStorage.getState().filters.scanAreas?.filter?.areas || [],
},
},
})

const [active, setActive] = React.useState(
/** @type {import('@rm/types').Weather | null} */ (null),
)
const [open, setOpen] = React.useState(false)

if (!weatherEnabled || !Icons) return null

/** @type {import('@rm/types').Weather | undefined} */
const active = (data || previousData)?.weather?.find(
(cell) =>
cell && booleanPointInPolygon(point(location), polygon([cell.polygon])),
)
React.useEffect(() => {
if (zoom > allowedZoom) {
const weatherCache = Object.values(apolloClient.cache.extract()).find(
(x) =>
x.__typename === 'Weather' &&
// @ts-ignore
booleanPointInPolygon(point(location), polygon([x.polygon])),
)
// @ts-ignore
if (weatherCache) setActive(weatherCache)
}
}, [location, zoom, allowedZoom])

if (!weatherEnabled || !Icons || !active) return null
const [{ disableColorShift = false }] = Icons.getModifiers('weather')

return active?.gameplay_condition && zoom > allowedZoom ? (
return (
<React.Fragment key={active?.gameplay_condition}>
<StyledBox
className="weather-icon flex-center"
Expand Down Expand Up @@ -109,5 +104,5 @@ export default function ActiveWeather() {
/>
</Dialog>
</React.Fragment>
) : null
)
}

0 comments on commit 6541aa8

Please sign in to comment.