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 2 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
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 Down
10 changes: 2 additions & 8 deletions src/renderer/components/MapFilter/MapView/MapView.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
useBackgroundMapStore,
useExperimentsFlagsStore
} from '../../../hooks/store'
import { useMapStylesQuery } from '../../../hooks/useMapStylesQuery'

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

View workflow job for this annotation

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

'useMapStylesQuery' is defined but never used

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

View workflow job for this annotation

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

'useMapStylesQuery' is defined but never used

const MapView = (
{
Expand All @@ -31,13 +31,7 @@
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, setMapStyle } = useBackgroundMapStore()

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

View workflow job for this annotation

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

'setMapStyle' is assigned a value but never used

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

View workflow job for this annotation

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

'setMapStyle' is assigned a value but never used

return (
<>
Expand Down Expand Up @@ -70,7 +64,7 @@
getMedia={getMedia}
presets={presets}
{...otherProps}
mapStyle={mapStyleUrl}
mapStyle={mapStyle.url}
/>
)}
</ViewWrapper>
Expand Down
52 changes: 44 additions & 8 deletions src/renderer/hooks/store.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
// @ts-check
import { create } from 'zustand'
import { persist, createJSONStorage } from 'zustand/middleware'
import { defineMessages } from 'react-intl'
import store from '../../persist-store'

const m = defineMessages({
// The name of the default background map
defaultBackgroundMapName: 'Default'
})

const DEFAULT_MAP = {
id: '487x2pc8ws801avhs5hw58qnxc',
url: 'mapbox://styles/mapbox/outdoors-v10',
bytesStored: 0,
name: m.defaultBackgroundMapName,
isImporting: false,
isDefault: true
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is in 2 places, so ideally should just be moved to one place!


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

/**
Expand Down Expand Up @@ -56,17 +71,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 +107,26 @@ export const useExperimentsFlagsStore = createPersistedStore(
experimentsFlagsStoreSlice,
'experiments-flags'
)
export const useBackgroundMapStore = createPersistedStore(

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

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 }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ideally this would return an array, but I am not too sure how to the equivalent of as const

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I figured out how to return an array
image

}
8 changes: 5 additions & 3 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'

Check failure on line 2 in src/renderer/hooks/useMapStylesQuery.js

View workflow job for this annotation

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

'useBackgroundMapStore' is defined but never used

Check failure on line 2 in src/renderer/hooks/useMapStylesQuery.js

View workflow job for this annotation

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

'useBackgroundMapStore' is defined but never used
import { useMapServerQuery } from './useMapServerQuery'
import { useQuery } from '@tanstack/react-query'
import api from '../new-api'
Expand Down Expand Up @@ -42,7 +42,9 @@
refetch: mapStylesQueryResult.refetch
}
: {
data: legacyStyleQueryResult.data,
data: !legacyStyleQueryResult.data
? [defaultMapStyle]
: legacyStyleQueryResult.data,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this encapsulates the undefined check so the consuming components do not have to do that

isLoading: legacyStyleQueryResult.isLoading,
refetch: legacyStyleQueryResult.refetch
}
Expand Down Expand Up @@ -80,7 +82,7 @@
return queryResult
}

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

return {
Expand Down