Skip to content

Commit

Permalink
fix: locate translations / unit of measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Apr 1, 2024
1 parent f6f3b7d commit 704423b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/features/webhooks/human/Location.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const Location = () => {
lc.stop()
useWebhookStore.setState({ location: [0, 0] })
},
[],
[lc],
)

const fetchedData = data || previousData || { geocoder: [] }
Expand Down
34 changes: 24 additions & 10 deletions src/hooks/useLocation.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useMemo, useState } from 'react'
import { useEffect, useMemo, useState } from 'react'
import { LayerGroup, DomEvent, DomUtil, Control } from 'leaflet'
import { useTranslation } from 'react-i18next'
import { useMap } from 'react-leaflet'
import 'leaflet.locatecontrol'

import { useStorage } from '@store/useStorage'

/**
* Use location hook
* @returns {{ lc: any, color: import('@mui/material').ButtonProps['color'] }}
Expand All @@ -14,6 +16,7 @@ export function useLocation() {
/** @type {import('@mui/material').ButtonProps['color']} */ ('inherit'),
)
const { t } = useTranslation()
const metric = useStorage((s) => s.settings.distanceUnit === 'kilometers')

const lc = useMemo(() => {
const LocateFab = Control.Locate.extend({
Expand All @@ -31,9 +34,8 @@ export function useLocation() {
'react-locate-control leaflet-bar leaflet-control',
)
this._container = container
this._map = map
this._layer = this.options.layer || new LayerGroup()
this._layer.addTo(map)
this._layer.addTo(this._map)
this._event = undefined
this._compassHeading = null
this._prevBounds = null
Expand Down Expand Up @@ -66,18 +68,30 @@ export function useLocation() {
const result = new LocateFab({
keepCurrentZoomLevel: true,
setView: 'untilPan',
options: {
title: t('lc_title'),
metric,
locateOptions: {
maximumAge: 5000,
},
strings: {
metersUnit: t('lc_metersUnit'),
feetUnit: t('lc_feetUnit'),
popup: t('lc_popup'),
outsideMapBoundsMsg: t('lc_outsideMapBoundsMsg'),
locateOptions: {
maximumAge: 5000,
},
title: t('lc_title'),
},
}).addTo(map)
})
return result
}, [map, t])
}, [t, metric])

useEffect(() => {
if (lc) {
lc.addTo(map)
return () => {
lc.stop()
lc.remove()
}
}
}, [lc, map])

return { lc, color }
}
2 changes: 1 addition & 1 deletion src/pages/map/components/FloatingBtn.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function FloatingButtons() {
break
}
},
[map],
[map, lc],
)

React.useEffect(() => {
Expand Down
17 changes: 16 additions & 1 deletion src/pages/map/components/Layers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as React from 'react'
import { TileLayer, useMap } from 'react-leaflet'
import { control } from 'leaflet'
import { useTranslation } from 'react-i18next'

import { useStorage } from '@store/useStorage'

Expand Down Expand Up @@ -32,9 +33,11 @@ export function ControlledZoomLayer() {

export function ControlledLocate() {
const map = useMap()
const { t } = useTranslation()
const navSetting = useStorage(
(s) => s.settings.navigationControls === 'leaflet',
)
const metric = useStorage((s) => s.settings.distanceUnit === 'kilometers')

const lc = React.useMemo(
() =>
Expand All @@ -43,14 +46,26 @@ export function ControlledLocate() {
icon: 'fas fa-crosshairs',
keepCurrentZoomLevel: true,
setView: 'untilPan',
metric,
locateOptions: {
maximumAge: 5000,
},
strings: {
metersUnit: t('lc_metersUnit'),
feetUnit: t('lc_feetUnit'),
popup: t('lc_popup'),
outsideMapBoundsMsg: t('lc_outsideMapBoundsMsg'),
title: t('lc_title'),
},
}),
[],
[t, metric],
)

React.useEffect(() => {
if (navSetting) {
lc.addTo(map)
return () => {
lc.stop()
lc.remove()
}
}
Expand Down

0 comments on commit 704423b

Please sign in to comment.