-
-
Notifications
You must be signed in to change notification settings - Fork 33
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} | ||
|
||
/** | ||
* @typedef {import('./useMapServerQuery').MapServerStyleInfo} MapStyle | ||
* @typedef { {id: string, url: string,bytesStored: number,name: import('react-intl').MessageDescriptor}} MapStyle | ||
*/ | ||
|
||
/** | ||
|
@@ -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 {{ | ||
|
@@ -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 = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} |
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 GitHub Actions / build (ubuntu-latest, x64, main)
|
||
import { useMapServerQuery } from './useMapServerQuery' | ||
import { useQuery } from '@tanstack/react-query' | ||
import api from '../new-api' | ||
|
@@ -42,7 +42,9 @@ | |
refetch: mapStylesQueryResult.refetch | ||
} | ||
: { | ||
data: legacyStyleQueryResult.data, | ||
data: !legacyStyleQueryResult.data | ||
? [defaultMapStyle] | ||
: legacyStyleQueryResult.data, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
@@ -80,7 +82,7 @@ | |
return queryResult | ||
} | ||
|
||
const useDefaultMapStyle = () => { | ||
export const useDefaultMapStyle = () => { | ||
const { formatMessage: t } = useIntl() | ||
|
||
return { | ||
|
There was a problem hiding this comment.
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!