-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ej/ useMapStylesQuery hook (refreshed PR) (#745)
* Revert "add SettingsMenu tabs component (WIP)" This reverts commit 9a478ed. * Revert "add settings menu tab" This reverts commit fd8a0ba. * feat: add persisted zustand store * WIP struggling a little with jsdoc types ... * chore: added types to store slices * chore: install electron-store / remove conf * chore: convert conf-store to electron-store (and rename store.js -> persist-store.js) * chore: remove store version, add persisted UI store * chore: replace createPersistedState hook with zustand store / remove createPersistedState * chore: move store.js to /hooks * chore: remove redundant `async` and better types * chore: cast return type for `getItem` to string | null * test commit * chore: force lint-staged to format files * fix: fix UI store broken import * fix: zustand types * feat: add useMapStylesQuery and useLegacyMapStyleQuery hooks * fix: always return string[] from useMapStylesQuery * chore: modify useMapServerQuery to return only url as data * fix: useMapServerQuery return url array * chore: return full map object * fix: remove console.log
- Loading branch information
Showing
3 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
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,69 @@ | ||
// @ts-check | ||
import { useExperimentsFlagsStore } from './store' | ||
import { useMapServerQuery } from './useMapServerQuery' | ||
import { useQuery } from '@tanstack/react-query' | ||
import api from '../new-api' | ||
import { defineMessages, useIntl } from 'react-intl' | ||
|
||
// Randomly generated, but should not change, since this is stored in settings | ||
// if the user selects one of these "legacy" map styles | ||
const DEFAULT_MAP_ID = '487x2pc8ws801avhs5hw58qnxc' | ||
const CUSTOM_MAP_ID = 'vg4ft8yvzwfedzgz1dz7ntneb8' | ||
const ONLINE_STYLE_URL = 'mapbox://styles/mapbox/outdoors-v10' | ||
|
||
const m = defineMessages({ | ||
// The name of the default background map | ||
defaultBackgroundMapName: 'Default', | ||
// The name of the legacy offline background map | ||
offlineBackgroundMapName: 'Offline Map' | ||
}) | ||
|
||
export const useMapStylesQuery = () => { | ||
const backgroundMapsEnabled = useExperimentsFlagsStore( | ||
store => store.backgroundMaps | ||
) | ||
|
||
const legacyStyleQueryResult = useLegacyMapStyleQuery(!backgroundMapsEnabled) | ||
const mapStylesQueryResult = useMapServerQuery( | ||
'/styles', | ||
backgroundMapsEnabled | ||
) | ||
|
||
return backgroundMapsEnabled ? mapStylesQueryResult : legacyStyleQueryResult | ||
} | ||
|
||
export const useLegacyMapStyleQuery = enabled => { | ||
const { formatMessage: t } = useIntl() | ||
|
||
const queryResult = useQuery({ | ||
queryKey: ['getLegacyMapStyle'], | ||
queryFn: () => { | ||
try { | ||
// This checks whether an offline style is available | ||
api.getMapStyle('default') | ||
return [ | ||
{ | ||
id: CUSTOM_MAP_ID, | ||
url: api.getMapStyleUrl('default'), | ||
bytesStored: 0, | ||
name: t(m.offlineBackgroundMapName), | ||
isImporting: false | ||
} | ||
] | ||
} catch { | ||
return [ | ||
{ | ||
id: DEFAULT_MAP_ID, | ||
url: ONLINE_STYLE_URL, | ||
bytesStored: 0, | ||
name: t(m.defaultBackgroundMapName), | ||
isImporting: false | ||
} | ||
] | ||
} | ||
}, | ||
enabled | ||
}) | ||
|
||
return queryResult | ||
} |