Skip to content

Commit

Permalink
fix: refactor hooks to simplify where styles data is coming from
Browse files Browse the repository at this point in the history
  • Loading branch information
lightlii committed Aug 29, 2023
1 parent 0cc0147 commit d07b385
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/renderer/components/BackgroundMaps/SidePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Button, makeStyles } from '@material-ui/core'
import ChevronLeft from '@material-ui/icons/ChevronLeft'
import * as React from 'react'
import { defineMessages, useIntl } from 'react-intl'
import { useMapServerQuery } from '../../hooks/useMapServerQuery'
import { ImportMapStyleDialog } from '../dialogs/ImportMapStyle'
import Loader from '../Loader'
import { MapCard } from './MapCard'
import { useMapStylesQuery } from '../../hooks/useMapStylesQuery'

const m = defineMessages({
// Button to add map background
Expand All @@ -28,7 +28,7 @@ export const SidePanel = ({ openSettings, mapValue, setMapValue }) => {
const classes = useStyles()
const [open, setOpen] = React.useState(false)

const { data, isLoading, refetch } = useMapServerQuery('/styles', false)
const { data, isLoading, refetch } = useMapStylesQuery(false)

return (
<>
Expand Down
15 changes: 2 additions & 13 deletions src/renderer/components/MapFilter/MapView/BackgroundMapSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ import {
useExperimentsFlagsStore,

Check failure on line 17 in src/renderer/components/MapFilter/MapView/BackgroundMapSelector.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, x64, icca)

'useExperimentsFlagsStore' is defined but never used
usePersistedUiStore
} from '../../../hooks/store'
import {
useDefaultMapStyle,
useMapStylesQuery
} from '../../../hooks/useMapStylesQuery'
import { useMapStylesQuery } from '../../../hooks/useMapStylesQuery'
import Loader from '../../Loader'

const MAPEO_BLUE = '#2469f6'
Expand Down Expand Up @@ -49,15 +46,7 @@ export const BackgroundMapSelector = ({ active, dismiss }) => {
setTabIndex(3) // TODO: Set from an ID rather than hardcoded index
}

const backgroundMapsEnabled = useExperimentsFlagsStore(
store => store.backgroundMaps
)

const defaultMapStyle = useDefaultMapStyle()
const { isLoading, data } = useMapStylesQuery()

const mapStyles =
backgroundMapsEnabled && data ? [defaultMapStyle, ...data] : data
const { isLoading, data: mapStyles } = useMapStylesQuery()

return (
<Dialog
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/components/SettingsView/BackgroundMaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Typography from '@material-ui/core/Typography'
import { BackgroundMapInfo } from '../BackgroundMaps/BackgroundMapInfo'
import { SidePanel } from '../BackgroundMaps/SidePanel'
import { useMapServerQuery } from '../../hooks/useMapServerQuery'

Check failure on line 8 in src/renderer/components/SettingsView/BackgroundMaps.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, x64, icca)

'useMapServerQuery' is defined but never used
import { useMapStylesQuery } from '../../hooks/useMapStylesQuery'

const m = defineMessages({
// Title for description of offline maps
Expand All @@ -28,7 +29,7 @@ export const BackgroundMaps = ({ returnToSettings }) => {

const [mapValue, setMapValue] = React.useState(initialMapId)

const { data } = useMapServerQuery('/styles')
const { data } = useMapStylesQuery()

function unsetMapValue () {
setMapValue(null)
Expand Down
49 changes: 31 additions & 18 deletions src/renderer/hooks/useMapStylesQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,39 @@ const m = defineMessages({
offlineBackgroundMapName: 'Offline Map'
})

export const useMapStylesQuery = () => {
export const useMapStylesQuery = (enabled = true) => {
const backgroundMapsEnabled = useExperimentsFlagsStore(
store => store.backgroundMaps
)

const legacyStyleQueryResult = useLegacyMapStyleQuery(!backgroundMapsEnabled)
const legacyStyleQueryResult = useLegacyMapStyleQuery(
!backgroundMapsEnabled && enabled
)
const mapStylesQueryResult = useMapServerQuery(
'/styles',
backgroundMapsEnabled
backgroundMapsEnabled && enabled
)
const defaultMapStyle = useDefaultMapStyle()

const queryResult = backgroundMapsEnabled
? {
data: [
defaultMapStyle,
...(mapStylesQueryResult?.data ? mapStylesQueryResult?.data : [])
],
isLoading: mapStylesQueryResult.isLoading,
refetch: mapStylesQueryResult.refetch
}
: {
data: legacyStyleQueryResult.data,
isLoading: legacyStyleQueryResult.isLoading,
refetch: legacyStyleQueryResult.refetch
}

return backgroundMapsEnabled ? mapStylesQueryResult : legacyStyleQueryResult
return queryResult
}

export const useLegacyMapStyleQuery = enabled => {
const useLegacyMapStyleQuery = enabled => {
const { formatMessage: t } = useIntl()
const defaultMapStyle = useDefaultMapStyle()

Expand Down Expand Up @@ -61,31 +79,26 @@ export const useLegacyMapStyleQuery = enabled => {
return queryResult
}

export const useDefaultMapStyle = () => {
const useDefaultMapStyle = () => {
const { formatMessage: t } = useIntl()

return {
id: DEFAULT_MAP_ID,
url: ONLINE_STYLE_URL,
bytesStored: 0,
name: t(m.defaultBackgroundMapName),
isImporting: false
isImporting: false,
isDefault: true
}
}

export const useSelectedMapStyle = () => {
const backgroundMapsFlag = useExperimentsFlagsStore(
store => store.backgroundMaps
)
const backgroundMapStyleId = useBackgroundMapStore(store => store.mapStyle)
const defaultMapStyle = useDefaultMapStyle()

const { data: mapStyles, isLoading } = useMapStylesQuery()
const { data: mapStyles } = useMapStylesQuery()

if (isLoading || !mapStyles) return defaultMapStyle

return backgroundMapsFlag
? mapStyles?.find(style => style.id === backgroundMapStyleId) ||
defaultMapStyle
: mapStyles && mapStyles[0]
return (
mapStyles?.find(style => style.id === backgroundMapStyleId) ||
(mapStyles && mapStyles[0])
)
}

0 comments on commit d07b385

Please sign in to comment.