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

Ej/ useMapStylesQuery hook (refreshed PR) #745

Merged
merged 26 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
108cf02
Revert "add SettingsMenu tabs component (WIP)"
lightlii Jun 28, 2023
8fbdd3f
Revert "add settings menu tab"
lightlii Jun 28, 2023
5f7d37d
Merge branch 'master' of github.com:digidem/mapeo-desktop
lightlii Jul 3, 2023
2b00668
feat: add persisted zustand store
lightlii Jul 17, 2023
950701f
WIP struggling a little with jsdoc types ...
lightlii Jul 17, 2023
9a347ab
chore: added types to store slices
lightlii Jul 18, 2023
779be3d
chore: install electron-store / remove conf
lightlii Jul 20, 2023
033b0d4
chore: convert conf-store to electron-store (and rename store.js -> p…
lightlii Jul 20, 2023
d9be73e
chore: remove store version, add persisted UI store
lightlii Jul 20, 2023
dc23a5c
chore: replace createPersistedState hook with zustand store / remove …
lightlii Jul 20, 2023
d450ddb
chore: move store.js to /hooks
lightlii Jul 24, 2023
47fe57c
chore: remove redundant `async` and better types
lightlii Jul 25, 2023
23bfa6c
chore: cast return type for `getItem` to string | null
lightlii Jul 25, 2023
e106b80
test commit
lightlii Jul 25, 2023
5cf09fc
chore: force lint-staged to format files
lightlii Jul 25, 2023
632a55f
fix: fix UI store broken import
lightlii Jul 26, 2023
d93994f
fix: zustand types
lightlii Jul 26, 2023
7fed094
Merge branch 'master' of github.com:digidem/mapeo-desktop
lightlii Jul 27, 2023
5ca7002
feat: add useMapStylesQuery and useLegacyMapStyleQuery hooks
lightlii Jul 27, 2023
5a0dc7b
fix: always return string[] from useMapStylesQuery
lightlii Aug 7, 2023
7753f87
Merge branch 'master' of github.com:digidem/mapeo-desktop
lightlii Aug 8, 2023
cf876de
chore: modify useMapServerQuery to return only url as data
lightlii Aug 14, 2023
8690abe
fix: useMapServerQuery return url array
lightlii Aug 14, 2023
837657d
chore: return full map object
lightlii Aug 14, 2023
3b81bb7
fix: remove console.log
lightlii Aug 14, 2023
ff92d80
Merge branch 'master' into ej/useMapStyles
lightlii Aug 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions messages/renderer/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,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: 4 additions & 1 deletion src/renderer/hooks/useMapServerQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ 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: () => ky.get(MAP_SERVER_URL + resourcePath).json(),
queryFn: () => {
return ky.get(MAP_SERVER_URL + resourcePath).json()
},

enabled
})
}
69 changes: 69 additions & 0 deletions src/renderer/hooks/useMapStylesQuery.js
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
}
Loading