Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Es/example of persisted state #747

Merged
merged 3 commits into from
Sep 12, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions messages/renderer/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,10 @@
"description": "Message stored in text file in export if originals are missing. The message is followed by a list of filenames with missing originals",
"message": "The original size of these files could not be found, only preview size (1,200 pixel) images are included. This can happen because the phone that took the photos has only synced to other phones, and not directly to Mapeo Desktop. To try fixing this, find the phone that took the photos and sync it with Mapeo Desktop before exporting again."
},
"renderer.hooks.store.defaultBackgroundMapName": {
"description": "The name of the default background map",
"message": "Default"
},
"renderer.hooks.useMapStylesQuery.defaultBackgroundMapName": {
"description": "The name of the default background map",
"message": "Default"
Expand Down
11 changes: 4 additions & 7 deletions src/renderer/components/BackgroundMaps/BackgroundMapInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const m = defineMessages({
})

/**
* @typedef {import('../../hooks/useMapServerQuery').MapServerStyleInfo & { isDefault?: boolean }} BackgroundMapInfo
* @typedef {import('../../hooks/store').MapStyle} BackgroundMapInfo
* @typedef BackgroundMapInfoProps
* @prop {BackgroundMapInfo} map
* @prop {()=>void} unsetMapValue
Expand All @@ -52,10 +52,7 @@ const m = defineMessages({
export const BackgroundMapInfo = ({ map, unsetMapValue }) => {
const { formatMessage: t } = useIntl()

const [mapStyle, setMapStyle] = useBackgroundMapStore(store => [
store.mapStyle,
store.setMapStyle
])
const [mapStyle, setMapStyle] = useBackgroundMapStore()

const classes = useStyles()

Expand All @@ -76,7 +73,7 @@ export const BackgroundMapInfo = ({ map, unsetMapValue }) => {
) : (
<>
<MapInfo
name={map.name}
name={typeof map.name === 'string' ? map.name : t(map.name)}
id={map.id}
unsetMapValue={unsetMapValue}
url={map.url}
Expand All @@ -85,7 +82,7 @@ export const BackgroundMapInfo = ({ map, unsetMapValue }) => {
{/* Text */}
<div className={classes.paddedContainer}>
<Typography variant='subtitle2' style={{ fontSize: 18 }}>
{map.name}
{typeof map.name === 'string' ? map.name : t(map.name)}
</Typography>
{!map.isDefault ? (
<Typography variant='body1'>{`${Math.round(
Expand Down
13 changes: 9 additions & 4 deletions src/renderer/components/BackgroundMaps/MapCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ export const MapboxPrevOnly = ReactMapboxGl({
})

/**
* @typedef {import('../../hooks/store').MapStyle} MapStyle
* @typedef MapCardProps
* @prop {import('../SettingsView/BackgroundMaps').MapServerStyleInfo & { isDefault?: boolean }} mapStyle
* @prop {React.Dispatch<React.SetStateAction<import('../SettingsView/BackgroundMaps').MapServerStyleInfo['id'] | null>>} setMap
* @prop {MapStyle} mapStyle
* @prop {React.Dispatch<React.SetStateAction<MapStyle['id'] | null>>} setMap
* @prop {boolean } isBeingViewed
*/

/** @param {MapCardProps} param */
export const MapCard = ({ mapStyle, setMap, isBeingViewed }) => {
const theme = useTheme()
const selectedMapStyle = useBackgroundMapStore(store => store.mapStyle)
const [selectedMapStyle] = useBackgroundMapStore()
const classes = useStyles()
const { formatMessage: t } = useIntl()

Expand Down Expand Up @@ -65,7 +66,11 @@ export const MapCard = ({ mapStyle, setMap, isBeingViewed }) => {
/>
</div>
<div className={classes.text}>
<Typography>{mapStyle.name}</Typography>
<Typography>
{typeof mapStyle.name === 'string'
? mapStyle.name
: t(mapStyle.name)}
</Typography>
{!mapStyle.isDefault && (
<Typography variant='subtitle1'>
{`${Math.round(convertKbToMb(mapStyle.bytesStored))} ${t(m.mb)}`}
Expand Down
5 changes: 2 additions & 3 deletions src/renderer/components/BackgroundMaps/SidePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ export const SidePanel = ({ openSettings, mapValue, setMapValue }) => {
<Loader />
) : data ? (
data.map(mapStyle => (
<>
<React.Fragment key={mapStyle.id}>
<MapCard
setMap={setMapValue}
key={mapStyle.id}
mapStyle={mapStyle}
isBeingViewed={mapStyle.id === mapValue}
/>
</>
</React.Fragment>
))
) : null}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
import * as React from 'react'
import {
Box,
Expand Down Expand Up @@ -33,10 +34,7 @@ const m = defineMessages({
export const BackgroundMapSelector = ({ active, dismiss }) => {
const classes = useStyles()
const { formatMessage: t } = useIntl()
const [mapStyle, setMapStyle] = useBackgroundMapStore(store => [
store.mapStyle,
store.setMapStyle
])
const [mapStyle, setMapStyle] = useBackgroundMapStore()

const setTabIndex = usePersistedUiStore(store => store.setTabIndex)

Expand All @@ -62,7 +60,7 @@ export const BackgroundMapSelector = ({ active, dismiss }) => {
</Typography>
<Link
className={classes.link}
a='#'
href='#'
onClick={navigateToBackgroundMaps}
>
{t(m.manageMapsLink)}
Expand Down Expand Up @@ -91,7 +89,7 @@ export const BackgroundMapSelector = ({ active, dismiss }) => {
}}
selected={isSelected}
styleUrl={map.url}
title={map.name}
title={typeof map.name === 'string' ? map.name : t(map.name)}
key={map.id}
/>
)
Expand Down
11 changes: 2 additions & 9 deletions src/renderer/components/MapFilter/MapView/MapView.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
useBackgroundMapStore,
useExperimentsFlagsStore
} from '../../../hooks/store'
import { useMapStylesQuery } from '../../../hooks/useMapStylesQuery'

const MapView = (
{
Expand All @@ -31,13 +30,7 @@ const MapView = (
const backgroundMapsFlag = useExperimentsFlagsStore(
store => store.backgroundMaps
)
const selectedMapStyle = useBackgroundMapStore(store => store.mapStyle)
const { data: mapStyles } = useMapStylesQuery()

const mapStyleUrl =
backgroundMapsFlag && selectedMapStyle
? selectedMapStyle?.url
: mapStyles && mapStyles[0] && mapStyles[0].url
const [mapStyle] = useBackgroundMapStore()

return (
<>
Expand Down Expand Up @@ -70,7 +63,7 @@ const MapView = (
getMedia={getMedia}
presets={presets}
{...otherProps}
mapStyle={mapStyleUrl}
mapStyle={mapStyle.url}
/>
)}
</ViewWrapper>
Expand Down
49 changes: 41 additions & 8 deletions src/renderer/hooks/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
import { create } from 'zustand'
import { persist, createJSONStorage } from 'zustand/middleware'
import store from '../../persist-store'
import { DEFAULT_MAP } from './useMapStylesQuery'

/**
* @typedef {import('./useMapServerQuery').MapServerStyleInfo} MapStyle
* @typedef { {
* id: string,
* url: string,
* bytesStored: number,
* name: import('react-intl').MessageDescriptor | string,
* isDefault?: boolean,
* isImporting?: boolean
* }} MapStyle
*/

/**
Expand Down Expand Up @@ -56,17 +64,23 @@ const experimentsFlagsStoreSlice = (set, get) => ({

/**
* @typedef {{
* mapStyle: MapStyle | null,
* setMapStyle: (mapStyle: MapStyle) => void
* mapStyleLegacy: MapStyle,
* mapStyleMapServer: MapStyle,
* setMapStyleLegacy: (mapStyle:MapStyle) => void,
* setMapStyleServer: (mapStyle:MapStyle) => void
* }} BackgroundMapStoreSlice
*/
/**
* @type {import('zustand').StateCreator<BackgroundMapStoreSlice>}
*/
const backgroundMapStoreSlice = (set, get) => ({
mapStyle: null,
setMapStyle: mapStyle => set({ mapStyle })
})
const backgroundMapStoreSlice = (set, get) => {
return {
mapStyleLegacy: DEFAULT_MAP,
mapStyleMapServer: DEFAULT_MAP,
setMapStyleLegacy: mapStyle => set({ mapStyleLegacy: mapStyle }),
setMapStyleServer: mapStyle => set({ mapStyleMapServer: mapStyle })
}
}

/**
* @typedef {{
Expand All @@ -86,11 +100,30 @@ export const useExperimentsFlagsStore = createPersistedStore(
experimentsFlagsStoreSlice,
'experiments-flags'
)
export const useBackgroundMapStore = createPersistedStore(

const useBackgroundMapState = createPersistedStore(
backgroundMapStoreSlice,
'background-maps'
)
export const usePersistedUiStore = createPersistedStore(
persistedUiStoreSlice,
'ui'
)

/**
*
* @returns {[MapStyle, (mapStyle: MapStyle) => void]}
*/
export const useBackgroundMapStore = () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now this hooks encapsulates the logic for whether background maps is enable or not. And can only show and set the maps that are relevant to that state. No need to the developer to do a check everytime.

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

const [mapStyle, setMapStyle] = useBackgroundMapState(store =>
backgroundMapsEnabled
? [store.mapStyleMapServer, store.setMapStyleServer]
: [store.mapStyleLegacy, store.setMapStyleLegacy]
)

return [mapStyle, setMapStyle]
}
32 changes: 14 additions & 18 deletions src/renderer/hooks/useMapStylesQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ const m = defineMessages({
offlineBackgroundMapName: 'Offline Map'
})

export const DEFAULT_MAP = {
id: DEFAULT_MAP_ID,
url: ONLINE_STYLE_URL,
bytesStored: 0,
name: m.defaultBackgroundMapName,
isImporting: false,
isDefault: true
}

export const useMapStylesQuery = (enabled = true) => {
const backgroundMapsEnabled = useExperimentsFlagsStore(
store => store.backgroundMaps
Expand All @@ -30,19 +39,20 @@ export const useMapStylesQuery = (enabled = true) => {
'/styles',
backgroundMapsEnabled && enabled
)
const defaultMapStyle = useDefaultMapStyle()

const queryResult = backgroundMapsEnabled
? {
data: [
defaultMapStyle,
DEFAULT_MAP,
...(mapStylesQueryResult?.data ? mapStylesQueryResult?.data : [])
],
isLoading: mapStylesQueryResult.isLoading,
refetch: mapStylesQueryResult.refetch
}
: {
data: legacyStyleQueryResult.data,
data: !legacyStyleQueryResult.data
? [DEFAULT_MAP]
: legacyStyleQueryResult.data,
isLoading: legacyStyleQueryResult.isLoading,
refetch: legacyStyleQueryResult.refetch
}
Expand All @@ -52,7 +62,6 @@ export const useMapStylesQuery = (enabled = true) => {

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

const queryResult = useQuery({
queryKey: ['getLegacyMapStyle'],
Expand All @@ -71,24 +80,11 @@ const useLegacyMapStyleQuery = enabled => {
}
]
} catch {
return [defaultMapStyle]
return [DEFAULT_MAP]
}
},
enabled
})

return queryResult
}

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

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