-
-
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
Ej/ useMapStylesQuery hook #741
Closed
Closed
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
108cf02
Revert "add SettingsMenu tabs component (WIP)"
lightlii 8fbdd3f
Revert "add settings menu tab"
lightlii 5f7d37d
Merge branch 'master' of github.com:digidem/mapeo-desktop
lightlii 2b00668
feat: add persisted zustand store
lightlii 950701f
WIP struggling a little with jsdoc types ...
lightlii 9a347ab
chore: added types to store slices
lightlii 779be3d
chore: install electron-store / remove conf
lightlii 033b0d4
chore: convert conf-store to electron-store (and rename store.js -> p…
lightlii d9be73e
chore: remove store version, add persisted UI store
lightlii dc23a5c
chore: replace createPersistedState hook with zustand store / remove …
lightlii d450ddb
chore: move store.js to /hooks
lightlii 47fe57c
chore: remove redundant `async` and better types
lightlii 23bfa6c
chore: cast return type for `getItem` to string | null
lightlii e106b80
test commit
lightlii 5cf09fc
chore: force lint-staged to format files
lightlii 632a55f
fix: fix UI store broken import
lightlii d93994f
fix: zustand types
lightlii 5ca7002
feat: add useMapStylesQuery and useLegacyMapStyleQuery hooks
lightlii 5a0dc7b
fix: always return string[] from useMapStylesQuery
lightlii cf876de
chore: modify useMapServerQuery to return only url as data
lightlii 8690abe
fix: useMapServerQuery return url array
lightlii 837657d
chore: return full map object
lightlii 3b81bb7
fix: remove console.log
lightlii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const Store = require('electron-store') | ||
|
||
const persistedStore = new Store() | ||
|
||
module.exports = persistedStore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// @ts-check | ||
import { create } from 'zustand' | ||
import { persist, createJSONStorage } from 'zustand/middleware' | ||
import store from '../../persist-store' | ||
|
||
/** | ||
* @type {import('zustand/middleware').StateStorage} | ||
*/ | ||
const storage = { | ||
/** | ||
* @param {string} key - A string key to reference the stored item by. | ||
* @returns {string | null} | ||
*/ | ||
getItem: key => { | ||
return /** @type {string | null} */ (store.get(key)) || null | ||
}, | ||
/** | ||
* @param {string} key - A string key to reference the stored item by. | ||
* @param {string} state - The state to be persisted - stringified JSON. | ||
* @returns {void} | ||
*/ | ||
setItem: (key, state) => { | ||
store.set(key, JSON.parse(state)) | ||
}, | ||
/** | ||
* @param {string} key - A string key to reference the stored item to be removed. | ||
*/ | ||
removeItem: key => { | ||
store.delete(key) | ||
} | ||
} | ||
|
||
/** | ||
* @template T | ||
* @param {import('zustand').StateCreator<T>} slice | ||
* @param {string} storeName | ||
* @returns {import('zustand').UseBoundStore<import('zustand').StoreApi<T>>} | ||
*/ | ||
const createPersistedStore = (slice, storeName) => | ||
create( | ||
persist(slice, { | ||
name: storeName, | ||
storage: createJSONStorage(() => storage) | ||
}) | ||
) | ||
|
||
/** | ||
* @typedef {{ | ||
* backgroundMaps: boolean, | ||
* setBackgroundMapsFlag: (backgroundMaps:boolean) => void, | ||
* }} ExperimentsFlagsStoreSlice | ||
*/ | ||
|
||
/** | ||
* @type {import('zustand').StateCreator<ExperimentsFlagsStoreSlice>} | ||
*/ | ||
const experimentsFlagsStoreSlice = (set, get) => ({ | ||
backgroundMaps: false, | ||
setBackgroundMapsFlag: backgroundMaps => set({ backgroundMaps }) | ||
}) | ||
|
||
/** | ||
* @typedef {{ | ||
* mapStyle: string, | ||
* setMapStyle: (mapStyle: string) => void | ||
* }} BackgroundMapStoreSlice | ||
*/ | ||
/** | ||
* @type {import('zustand').StateCreator<BackgroundMapStoreSlice>} | ||
*/ | ||
const backgroundMapStoreSlice = (set, get) => ({ | ||
mapStyle: '', | ||
setMapStyle: mapStyle => set({ mapStyle }) | ||
}) | ||
|
||
/** | ||
* @typedef {{ | ||
* tabIndex: number, | ||
* setTabIndex: (tabIndex: number) => void | ||
* }} PersistedUiStoreSlice | ||
*/ | ||
/** | ||
* @type {import('zustand').StateCreator<PersistedUiStoreSlice>} | ||
*/ | ||
const persistedUiStoreSlice = (set, get) => ({ | ||
tabIndex: 0, | ||
setTabIndex: tabIndex => set({ tabIndex }) | ||
}) | ||
|
||
export const useExperimentsFlagsStore = createPersistedStore( | ||
experimentsFlagsStoreSlice, | ||
'experiments-flags' | ||
) | ||
export const useBackgroundMapStore = createPersistedStore( | ||
backgroundMapStoreSlice, | ||
'background-maps' | ||
) | ||
export const usePersistedUiStore = createPersistedStore( | ||
persistedUiStoreSlice, | ||
'ui' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// @ts-check | ||
import { useExperimentsFlagsStore } from './store' | ||
import { useMapServerQuery } from './useMapServerQuery' | ||
import { useQuery } from '@tanstack/react-query' | ||
import api from '../new-api' | ||
|
||
export const useMapStylesQuery = () => { | ||
const backgroundMapsEnabled = useExperimentsFlagsStore( | ||
store => store.backgroundMaps | ||
) | ||
const legacyStyleQueryResult = useLegacyMapStyleQuery(!backgroundMapsEnabled) | ||
const mapStylesQueryResult = useMapServerQuery( | ||
'/styles', | ||
backgroundMapsEnabled | ||
) | ||
ErikSin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return backgroundMapsEnabled ? mapStylesQueryResult : legacyStyleQueryResult | ||
} | ||
|
||
export const useLegacyMapStyleQuery = enabled => { | ||
const ONLINE_STYLE_URL = 'mapbox://styles/mapbox/outdoors-v10' | ||
const offlineStyleURL = api.getMapStyleUrl('default') | ||
|
||
const queryResult = useQuery({ | ||
queryKey: ['getLegacyMapStyle'], | ||
queryFn: async () => { | ||
try { | ||
// This checks whether an offline style is available | ||
await api.getMapStyle('default') | ||
// and if it is, it will use that map style (retrieved above). | ||
return [offlineStyleURL] | ||
} catch { | ||
// If no offline style is available we use the default online style | ||
return [ONLINE_STYLE_URL] | ||
} | ||
}, | ||
enabled | ||
}) | ||
|
||
return queryResult | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
can you enforce the typing to `UseQueryResult<MapServerStyleInfo[]>. Both functions are returning that, but since one is not ecplicitly that, 2 different types are coming up in the ts definitions