Skip to content

Commit

Permalink
update implementation to account for core update
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 committed Oct 17, 2024
1 parent b96a8ff commit 632adcc
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 206 deletions.
8 changes: 4 additions & 4 deletions messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1285,14 +1285,17 @@
"screens.Settings.MapManagement.BackgroundMaps.close": {
"message": "Close"
},
"screens.Settings.MapManagement.BackgroundMaps.customMapAddedDescription": {
"message": "You will see this map when you are offline, but you will not see a map outside the area defined in your custom map."
},
"screens.Settings.MapManagement.BackgroundMaps.customMapAddedTitle": {
"message": "Custom Map Added"
},
"screens.Settings.MapManagement.BackgroundMaps.deleteCustomMapDescription": {
"message": "This will delete the map and its offline areas. No collected observation data will be deleted."
},
"screens.Settings.MapManagement.BackgroundMaps.deleteCustomMapTitle": {
"message": "Delete CUstom Map?"
"message": "Delete Custom Map?"
},
"screens.Settings.MapManagement.BackgroundMaps.deleteMapButtonText": {
"message": "Delete Map"
Expand All @@ -1303,9 +1306,6 @@
"screens.Settings.MapManagement.BackgroundMaps.description2": {
"message": "Your custom map is not shared with other devices in your project."
},
"screens.Settings.MapManagement.BackgroundMaps.offlineMapAddedDescription": {
"message": "You will see this map when you are offline, but you will not see a map outside the area defined in your custom map."
},
"screens.Settings.MapManagement.BackgroundMaps.screenTitle": {
"message": "Background Maps"
},
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"tiny-typed-emitter": "^2.1.0",
"uint8array-extras": "^0.5.0",
"utm": "^1.1.1",
"valibot": "^0.42.1",
"validate-color": "^2.2.4",
"zustand": "^4.4.6"
},
Expand Down
13 changes: 10 additions & 3 deletions src/backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ import debug from 'debug'
import { join } from 'path'
import { mkdirSync } from 'fs'
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
/** @type {import('../types/rn-bridge.js')} */
const rnBridge = require('rn-bridge')
import { MapeoManager, FastifyController } from '@comapeo/core'
import { createMapeoServer } from '@comapeo/ipc'
import Fastify from 'fastify'

import MessagePortLike from './message-port-like.js'
import { ServerStatus } from './status.js'

const require = createRequire(import.meta.url)

/** @type {import('../types/rn-bridge.js')} */
const rnBridge = require('rn-bridge')

// Do not touch these!
const DB_DIR_NAME = 'sqlite-dbs'
const CORE_STORAGE_DIR_NAME = 'core-storage'
const CUSTOM_MAPS_DIR_NAME = 'maps'
const DEFAULT_CUSTOM_MAP_FILE_NAME = 'default.smp'

const MAPBOX_ACCESS_TOKEN =
'pk.eyJ1IjoiZGlnaWRlbSIsImEiOiJjbHRyaGh3cm0wN3l4Mmpsam95NDI3c2xiIn0.daq2iZFZXQ08BD0VZWAGUw'
Expand Down Expand Up @@ -64,9 +68,11 @@ export async function init({
const privateStorageDir = rnBridge.app.datadir()
const dbDir = join(privateStorageDir, DB_DIR_NAME)
const indexDir = join(privateStorageDir, CORE_STORAGE_DIR_NAME)
const customMapsDir = join(privateStorageDir, CUSTOM_MAPS_DIR_NAME)

mkdirSync(dbDir, { recursive: true })
mkdirSync(indexDir, { recursive: true })
mkdirSync(customMapsDir, { recursive: true })

const fastify = Fastify()
const fastifyController = new FastifyController({ fastify })
Expand All @@ -80,6 +86,7 @@ export async function init({
fastify,
defaultConfigPath,
defaultOnlineStyleUrl: DEFAULT_ONLINE_MAP_STYLE_URL,
customMapPath: join(customMapsDir, DEFAULT_CUSTOM_MAP_FILE_NAME),
})

// Don't await, methods that use the server will await this internally
Expand Down
145 changes: 0 additions & 145 deletions src/frontend/hooks/customMaps.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/frontend/hooks/server/mapStyleUrl.ts

This file was deleted.

107 changes: 107 additions & 0 deletions src/frontend/hooks/server/maps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query';
import * as FileSystem from 'expo-file-system';
import * as v from 'valibot';

import {useApi} from '../../contexts/ApiContext';
import {DOCUMENT_DIRECTORY, selectFile} from '../../lib/file-system';

export const MAPS_QUERY_KEY = 'maps';

const CUSTOM_MAPS_DIRECTORY = new URL('maps', DOCUMENT_DIRECTORY).href;
const DEFAULT_CUSTOM_MAP_FILE_PATH = CUSTOM_MAPS_DIRECTORY + '/default.smp';

const CustomMapInfoSchema = v.object({
created: v.pipe(
v.string(),
v.isoTimestamp(),
v.transform(input => new Date(input)),
),
name: v.string(),
size: v.pipe(v.number(), v.minValue(0)),
});

export type CustomMapInfo = v.InferOutput<typeof CustomMapInfoSchema>;

export function useMapStyleJsonUrl() {
const api = useApi();

return useQuery({
queryKey: [MAPS_QUERY_KEY, 'stylejson-url'],
queryFn: () => {
return api.getMapStyleJsonUrl();
},
});
}

export function useSelectCustomMapFile() {
return useMutation({
mutationFn: () => {
return selectFile({
extensionFilters: ['smp'],
});
},
});
}

export function useImportCustomMapFile() {
const queryClient = useQueryClient();

return useMutation({
mutationFn: async (opts: {uri: string}) => {
return FileSystem.moveAsync({
from: opts.uri,
to: DEFAULT_CUSTOM_MAP_FILE_PATH,
});
},
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: [MAPS_QUERY_KEY],
});
},
});
}

export function useRemoveCustomMapFile() {
const queryClient = useQueryClient();

return useMutation({
mutationFn: () => {
return FileSystem.deleteAsync(DEFAULT_CUSTOM_MAP_FILE_PATH, {
idempotent: true,
});
},
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: [MAPS_QUERY_KEY],
});
},
});
}

/**
* Returns `null` if no viable map is found. Throws an error if a detected map is invalid.
*/
export function useGetCustomMapInfo() {
const api = useApi();

return useQuery({
queryKey: [MAPS_QUERY_KEY, 'custom', 'info'],
queryFn: async () => {
const styleUrl = await api.getMapStyleJsonUrl();

const infoUrl = new URL('/maps/custom/info', styleUrl).href;

const response = await fetch(infoUrl);

if (response.status === 404) {
return null;
}

if (!response.ok) {
throw new Error(`Cannot get custom map info: ${response.statusText}`);
}

return v.parse(CustomMapInfoSchema, await response.json());
},
});
}
Loading

0 comments on commit 632adcc

Please sign in to comment.