Skip to content

Commit

Permalink
chore: return full map object
Browse files Browse the repository at this point in the history
  • Loading branch information
lightlii committed Aug 14, 2023
1 parent 8690abe commit 837657d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
8 changes: 8 additions & 0 deletions messages/renderer/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,14 @@
"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.useMapStylesQuery.defaultBackgroundMapName": {
"description": "The name of the default background map",
"message": "Default"
},
"renderer.hooks.useMapStylesQuery.offlineBackgroundMapName": {
"description": "The name of the legacy offline background map",
"message": "Offline Map"
},
"screens.Observation.ObservationView.noAnswer": {
"description": "Placeholder text for fields on an observation which are not answered",
"message": "No answer"
Expand Down
5 changes: 2 additions & 3 deletions src/renderer/hooks/useMapServerQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ const MAP_SERVER_URL = 'http://127.0.0.1:' + window.mapServerPort
export function useMapServerQuery (resourcePath, enabled) {
return useQuery({
queryKey: [resourcePath, resourcePath.split('/')[2] || undefined],
queryFn: async () => {
const result = await ky.get(MAP_SERVER_URL + resourcePath).json()
return result.map(({ url }) => url)
queryFn: () => {
return ky.get(MAP_SERVER_URL + resourcePath).json()
},

enabled
Expand Down
45 changes: 37 additions & 8 deletions src/renderer/hooks/useMapStylesQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,28 @@ 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
)

console.log({ backgroundMapsEnabled })

const legacyStyleQueryResult = useLegacyMapStyleQuery(!backgroundMapsEnabled)
const mapStylesQueryResult = useMapServerQuery(
'/styles',
Expand All @@ -19,20 +35,33 @@ export const useMapStylesQuery = () => {
}

export const useLegacyMapStyleQuery = enabled => {
const ONLINE_STYLE_URL = 'mapbox://styles/mapbox/outdoors-v10'
const offlineStyleURL = api.getMapStyleUrl('default')
const { formatMessage: t } = useIntl()

const queryResult = useQuery({
queryKey: ['getLegacyMapStyle'],
queryFn: async () => {
queryFn: () => {
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]
api.getMapStyle('default')
return [
{
id: CUSTOM_MAP_ID,
url: api.getMapStyleUrl('default'),
bytesStored: 0,
name: t(m.offlineBackgroundMapName),
isImporting: false
}
]
} catch {
// If no offline style is available we use the default online style
return [ONLINE_STYLE_URL]
return [
{
id: DEFAULT_MAP_ID,
url: ONLINE_STYLE_URL,
bytesStored: 0,
name: t(m.defaultBackgroundMapName),
isImporting: false
}
]
}
},
enabled
Expand Down

0 comments on commit 837657d

Please sign in to comment.