Merge pull request #1192 from KoalaSat/load-map-json-in-advance

Load map JSON in advance
This commit is contained in:
Reckless_Satoshi 2024-03-19 01:06:38 +00:00 committed by GitHub
commit 10bc21d239
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 16 deletions

View File

@ -1,12 +1,9 @@
import React, { useContext, useEffect, useState } from 'react'; import React, { useContext } from 'react';
import { apiClient } from '../../services/api';
import { MapContainer, GeoJSON, useMapEvents, TileLayer, Tooltip, Marker } from 'react-leaflet'; import { MapContainer, GeoJSON, useMapEvents, TileLayer, Tooltip, Marker } from 'react-leaflet';
import { useTheme, LinearProgress } from '@mui/material'; import { useTheme, LinearProgress } from '@mui/material';
import { type GeoJsonObject } from 'geojson';
import { DivIcon, type LeafletMouseEvent } from 'leaflet'; import { DivIcon, type LeafletMouseEvent } from 'leaflet';
import { type PublicOrder } from '../../models'; import { type PublicOrder } from '../../models';
import OrderTooltip from '../Charts/helpers/OrderTooltip'; import OrderTooltip from '../Charts/helpers/OrderTooltip';
import getWorldmapGeojson from '../../geo/Web';
import MarkerClusterGroup from '@christopherpickering/react-leaflet-markercluster'; import MarkerClusterGroup from '@christopherpickering/react-leaflet-markercluster';
import { AppContext, type UseAppStoreType } from '../../contexts/AppContext'; import { AppContext, type UseAppStoreType } from '../../contexts/AppContext';
@ -44,18 +41,7 @@ const Map = ({
interactive = false, interactive = false,
}: Props): JSX.Element => { }: Props): JSX.Element => {
const theme = useTheme(); const theme = useTheme();
const { hostUrl } = useContext<UseAppStoreType>(AppContext); const { worldmap } = useContext<UseAppStoreType>(AppContext);
const [worldmap, setWorldmap] = useState<GeoJsonObject | undefined>();
useEffect(() => {
getWorldmapGeojson(apiClient, hostUrl)
.then((data) => {
setWorldmap(data);
})
.catch((error) => {
console.error('Error:', error);
});
}, []);
const RobotMarker = ( const RobotMarker = (
key: string | number, key: string | number,

View File

@ -9,6 +9,7 @@ import React, {
import { type Page } from '../basic/NavBar'; import { type Page } from '../basic/NavBar';
import { type OpenDialogs } from '../basic/MainDialogs'; import { type OpenDialogs } from '../basic/MainDialogs';
import { ThemeProvider } from '@mui/material'; import { ThemeProvider } from '@mui/material';
import { type GeoJsonObject } from 'geojson';
import { Settings, type Version, type Origin, type Favorites } from '../models'; import { Settings, type Version, type Origin, type Favorites } from '../models';
@ -17,6 +18,8 @@ import { getClientVersion, getHost } from '../utils';
import defaultFederation from '../../static/federation.json'; import defaultFederation from '../../static/federation.json';
import { createTheme, type Theme } from '@mui/material/styles'; import { createTheme, type Theme } from '@mui/material/styles';
import i18n from '../i18n/Web'; import i18n from '../i18n/Web';
import getWorldmapGeojson from '../geo/Web';
import { apiClient } from '../services/api';
const getWindowSize = function (fontSize: number): { width: number; height: number } { const getWindowSize = function (fontSize: number): { width: number; height: number } {
// returns window size in EM units // returns window size in EM units
@ -131,6 +134,7 @@ export interface UseAppStoreType {
hostUrl: string; hostUrl: string;
fav: Favorites; fav: Favorites;
setFav: Dispatch<SetStateAction<Favorites>>; setFav: Dispatch<SetStateAction<Favorites>>;
worldmap?: GeoJsonObject;
} }
export const initialAppContext: UseAppStoreType = { export const initialAppContext: UseAppStoreType = {
@ -156,6 +160,7 @@ export const initialAppContext: UseAppStoreType = {
acknowledgedWarning: false, acknowledgedWarning: false,
fav: { type: null, currency: 0, mode: 'fiat', coordinator: 'any' }, fav: { type: null, currency: 0, mode: 'fiat', coordinator: 'any' },
setFav: () => {}, setFav: () => {},
worldmap: undefined,
}; };
export const AppContext = createContext<UseAppStoreType>(initialAppContext); export const AppContext = createContext<UseAppStoreType>(initialAppContext);
@ -187,6 +192,7 @@ export const AppContextProvider = ({ children }: AppContextProviderProps): JSX.E
const [acknowledgedWarning, setAcknowledgedWarning] = useState<boolean>( const [acknowledgedWarning, setAcknowledgedWarning] = useState<boolean>(
initialAppContext.acknowledgedWarning, initialAppContext.acknowledgedWarning,
); );
const [worldmap, setWorldmap] = useState<GeoJsonObject>();
useEffect(() => { useEffect(() => {
setTheme(makeTheme(settings)); setTheme(makeTheme(settings));
@ -220,6 +226,18 @@ export const AppContextProvider = ({ children }: AppContextProviderProps): JSX.E
}; };
}, []); }, []);
useEffect(() => {
if (page === 'offers' && !worldmap) {
getWorldmapGeojson(apiClient, hostUrl)
.then((data) => {
setWorldmap(data);
})
.catch((error) => {
console.error('Error:', error);
});
}
}, [page]);
useEffect(() => { useEffect(() => {
setWindowSize(getWindowSize(theme.typography.fontSize)); setWindowSize(getWindowSize(theme.typography.fontSize));
}, [theme.typography.fontSize]); }, [theme.typography.fontSize]);
@ -250,6 +268,7 @@ export const AppContextProvider = ({ children }: AppContextProviderProps): JSX.E
origin, origin,
fav, fav,
setFav, setFav,
worldmap,
}} }}
> >
<ThemeProvider theme={theme}>{children}</ThemeProvider> <ThemeProvider theme={theme}>{children}</ThemeProvider>