From 44ce641d6a8d674d9c4a4e3a2b764d22080675f4 Mon Sep 17 00:00:00 2001 From: evlist Date: Tue, 3 Jan 2023 18:17:12 +0100 Subject: [PATCH] Improving RteViewer. --- src/components/infos/Infos.tsx | 14 ----------- src/components/rte/RteViewer.tsx | 43 ++++++++++++++++++++++++++++++-- src/components/trk/TrkViewer.tsx | 7 +++++- src/i18n/en.ts | 3 +++ src/i18n/fr.ts | 3 +++ src/lib/osm.ts | 24 ++++++++++++++++++ 6 files changed, 77 insertions(+), 17 deletions(-) create mode 100644 src/lib/osm.ts diff --git a/src/components/infos/Infos.tsx b/src/components/infos/Infos.tsx index 28f6cb4..8caed75 100644 --- a/src/components/infos/Infos.tsx +++ b/src/components/infos/Infos.tsx @@ -130,20 +130,6 @@ const Infos: Component<{}> = (props) => { setOpen(false); }; - const formatLength = (length: number) => { - if (length > 1000) { - const l = length / 1000; - return ` ${l.toLocaleString(undefined, { - minimumFractionDigits: 2, - maximumFractionDigits: 2, - })} km`; - } - return ` ${length.toLocaleString(undefined, { - minimumFractionDigits: 0, - maximumFractionDigits: 0, - })} m`; - }; - return ( = ({ rteId }) => { + const [t, { add, locale, dict }] = useI18n(); + const rte = peekCachedSignal({ id: rteId, method: 'getRte' }); console.log({ caller: 'RteViewer', rteId, rte: rte() }); const title = () => { return rte().name; }; + const [startAddress] = createResource(rte, (r: Rte) => + findAddress(r.rtept[0].$.lon, r.rtept[0].$.lat, locale) + ); + const [endAddress] = createResource(rte, (r: Rte) => + findAddress(r.rtept.at(-1).$.lon, r.rtept.at(-1).$.lat, locale) + ); + + const formatLength = (length: number) => { + if (length > 1000) { + const l = length / 1000; + return ` ${l.toLocaleString(undefined, { + minimumFractionDigits: 2, + maximumFractionDigits: 2, + })} km`; + } + return ` ${length.toLocaleString(undefined, { + minimumFractionDigits: 0, + maximumFractionDigits: 0, + })} m`; + }; + + const geometry = new LineString( + rte().rtept.map((rtept: any) => [rtept.$.lon, rtept.$.lat]) + ); + + const length = getLength(geometry, { projection: 'EPSG:4326' }); + return ( = ({ rteId }) => { {title()} } - content={undefined} + content={ + <> +
{`${formatLength(length)} ${t('from')} ${getVillageOrTown( + startAddress() + )} ${t('to')} ${getVillageOrTown(endAddress())} `}
+ + } subTree={undefined} /> ); diff --git a/src/components/trk/TrkViewer.tsx b/src/components/trk/TrkViewer.tsx index 4ed7b69..ebd9c57 100644 --- a/src/components/trk/TrkViewer.tsx +++ b/src/components/trk/TrkViewer.tsx @@ -3,6 +3,7 @@ import { peekCachedSignal } from '../../workers/cached-signals'; import Tree from '../tree'; import TrkIcon from '../../icons/human-footprints-svgrepo-com.svg?component-solid'; import { TrksegViewer } from '../trkseg'; +import { findAddress } from '../../lib/osm'; interface Props { trkId: string; @@ -23,7 +24,11 @@ const TrkViewer: Component = ({ trkId, restrictToHierarchy }) => { {title()} } - content={undefined} + content={ +
+ +
+ } subTree={ diff --git a/src/i18n/en.ts b/src/i18n/en.ts index 0abad74..1ade80e 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -62,6 +62,9 @@ const dict = { remoteDbUser: 'Remote database user', remoteDbPassword: 'Remote database password', accountSave: 'Save', + + from: 'from ', + to: 'to ', }; export default dict; diff --git a/src/i18n/fr.ts b/src/i18n/fr.ts index 8083bea..62a3346 100644 --- a/src/i18n/fr.ts +++ b/src/i18n/fr.ts @@ -68,6 +68,9 @@ const dict = { remoteDbPassword: 'Mot de passe de la base de données distante', accountSave: 'Sauvegarder', accountCancel: 'Annuler', + + from: 'de ', + to: 'à ', }; export default dict; diff --git a/src/lib/osm.ts b/src/lib/osm.ts new file mode 100644 index 0000000..51a6580 --- /dev/null +++ b/src/lib/osm.ts @@ -0,0 +1,24 @@ +import memoize from 'memoizee'; + +const _findAddress = async (lon: number, lat: number, locale: () => string) => { + const response = await fetch( + `https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=jsonv2&addressdetails=1&extratags=1&namedetails=1&accept-language=${locale()}`, + {} + ); + const data = await response.json(); + console.log({ caller: 'findAddress', lon, lat, data }); + return data; +}; + +export const findAddress = memoize(_findAddress, { + promise: true, + max: 1000, + maxAge: 360000000, +}); + +export const getVillageOrTown = (address: any) => { + if (address?.address.village) { + return address?.address.village; + } + return address?.address.town; +};