Skip to content

Commit

Permalink
feat: implement map selection
Browse files Browse the repository at this point in the history
  • Loading branch information
lightlii committed Aug 18, 2023
1 parent cc800ef commit 879bfb3
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 48 deletions.
120 changes: 90 additions & 30 deletions src/renderer/components/MapFilter/MapView/BackgroundMapSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react'
import {
Box,
Button,
Link,
Paper,
Slide,
Typography,
Expand All @@ -10,8 +11,16 @@ import {
import { defineMessages, useIntl } from 'react-intl'
import { Close } from '@material-ui/icons'
import { MapPreviewCard } from './MapPreviewCard'
import { useBackgroundMapStore } from '../../../hooks/store'
import { useMapServerQuery } from '../../../hooks/useMapServerQuery'
import {
useBackgroundMapStore,
useExperimentsFlagsStore,
usePersistedUiStore
} from '../../../hooks/store'
import {
useDefaultMapStyle,
useMapStylesQuery
} from '../../../hooks/useMapStylesQuery'
import Loader from '../../Loader'

const m = defineMessages({
// Title for background maps overlay
Expand All @@ -30,51 +39,87 @@ export const BackgroundMapSelector = ({ active, dismiss }) => {
store.setMapStyle
])

// will use new hook once merged
const { data: mapStyles, isLoading } = useMapServerQuery('/styles', false)
const setTabIndex = usePersistedUiStore(store => store.setTabIndex)

const navigateToBackgroundMaps = () => {
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

return (
<Slide direction='up' in={active} mountOnEnter unmountOnExit>
<Paper elevation={2} className={classes.container}>
{/* HEADER */}
<Box className={classes.header}>
<Typography variant='h1' className={classes.title}>
{t(m.title)}
</Typography>
<Box className={classes.row}>
<Typography variant='h1' className={classes.title}>
{t(m.title)}
</Typography>
<Link
className={classes.link}
a='#'
onClick={navigateToBackgroundMaps}
>
{t(m.manageMapsLink)}
</Link>
</Box>
<Button onClick={dismiss} color='primary'>
{t(m.close)}
<Close />
</Button>
</Box>
{/* MAP STYLES ROW */}
<Box className={classes.styleRow}>
{mapStyles
.filter(({ isImporting }) => !isImporting)
.map(({ id, url, name }) => {
const isSelected = mapStyle === id
return (
<>
<div className={classes.mapCardWrapper} key={id}>
<MapPreviewCard
onClick={() => {
if (isSelected) return
dismiss()
setMapStyle(id)
}}
selected={isSelected}
styleUrl={url}
title={name}
/>
</div>
</>
)
})}
</Box>
{isLoading ? (
<Loading />
) : (
<Box className={classes.styleRow}>
{mapStyles
.filter(({ isImporting }) => !isImporting)
.map(({ id, url, name }) => {
const isSelected = mapStyle === id
return (
<>
<div className={classes.mapCardWrapper} key={id}>
<MapPreviewCard
onClick={() => {
if (isSelected) return
dismiss()
setMapStyle(id)
}}
selected={isSelected}
styleUrl={url}
title={name}
/>
</div>
</>
)
})}
</Box>
)}
</Paper>
</Slide>
)
}

const Loading = () => {
const classes = useStyles()

return (
<Box className={classes.loaderContainer}>
<Loader />
</Box>
)
}

const useStyles = makeStyles(theme => ({
container: {
borderRadius: '10px 10px 0px 0px',
Expand All @@ -98,11 +143,26 @@ const useStyles = makeStyles(theme => ({
fontWeight: 500,
marginRight: 5
},
row: {
display: 'flex',
flexDirection: 'row',
alignItems: 'center'
},
styleRow: {
display: 'flex',
padding: 22
},
link: {
cursor: 'pointer'
},
mapCardWrapper: {
marginRight: 32
},
loaderContainer: {
width: '100%',
marginTop: '10%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center'
}
}))
22 changes: 16 additions & 6 deletions src/renderer/components/MapFilter/MapView/MapView.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import ViewWrapper from '../ViewWrapper'
import { Avatar, makeStyles } from '@material-ui/core'
import { LayersOutlined } from '@material-ui/icons'
import { BackgroundMapSelector } from './BackgroundMapSelector'
import { useSelectedMapStyle } from '../../../hooks/useMapStylesQuery'
import { useExperimentsFlagsStore } from '../../../hooks/store'

const MapView = (
{
Expand All @@ -21,13 +23,20 @@ const MapView = (
const [backgroundMapSelectorOpen, setBackgroundMapSelectorOpen] = useState(
false
)
const backgroundMapsFlag = useExperimentsFlagsStore(
store => store.backgroundMaps
)
const selectedMapStyle = useSelectedMapStyle()

return (
<>
<MapLayerButton
onClick={() =>
setBackgroundMapSelectorOpen(selectorWasOpen => !selectorWasOpen)
}
/>
{backgroundMapsFlag ? (
<MapLayerButton
onClick={() =>
setBackgroundMapSelectorOpen(selectorWasOpen => !selectorWasOpen)
}
/>
) : null}
<ViewWrapper
observations={observations}
onUpdateObservation={onUpdateObservation}
Expand All @@ -50,11 +59,12 @@ const MapView = (
getMedia={getMedia}
presets={presets}
{...otherProps}
mapStyle={selectedMapStyle && selectedMapStyle?.url}
/>
)}
</ViewWrapper>
<BackgroundMapSelector
active={backgroundMapSelectorOpen}
active={backgroundMapsFlag && backgroundMapSelectorOpen}
dismiss={() => setBackgroundMapSelectorOpen(false)}
/>
</>
Expand Down
2 changes: 0 additions & 2 deletions src/renderer/components/SettingsView/ExperimentsMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export const ExperiementsMenu = () => {
store => [store.backgroundMaps, store.setBackgroundMapsFlag]
)

console.log({ backgroundMaps, setBackgroundMaps })

const toggleBackgroundMaps = () => {
setBackgroundMaps(!backgroundMaps)
}
Expand Down
42 changes: 32 additions & 10 deletions src/renderer/hooks/useMapStylesQuery.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-check
import { useExperimentsFlagsStore } from './store'
import { useBackgroundMapStore, useExperimentsFlagsStore } from './store'
import { useMapServerQuery } from './useMapServerQuery'
import { useQuery } from '@tanstack/react-query'
import api from '../new-api'
Expand Down Expand Up @@ -34,6 +34,7 @@ export const useMapStylesQuery = () => {

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

const queryResult = useQuery({
queryKey: ['getLegacyMapStyle'],
Expand All @@ -51,19 +52,40 @@ export const useLegacyMapStyleQuery = enabled => {
}
]
} catch {
return [
{
id: DEFAULT_MAP_ID,
url: ONLINE_STYLE_URL,
bytesStored: 0,
name: t(m.defaultBackgroundMapName),
isImporting: false
}
]
return [defaultMapStyle]
}
},
enabled
})

return queryResult
}

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

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

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

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

if (isLoading || !mapStyles) return defaultMapStyle

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

0 comments on commit 879bfb3

Please sign in to comment.