mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
fix(map): surface geolocation failures as a localized toast
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
// FE-MAP-LOCBTN-001 to FE-MAP-LOCBTN-004
|
||||
import { render } from '@testing-library/react';
|
||||
import LocationButton from './LocationButton';
|
||||
import type { GeoWatchErrorCode } from '../../hooks/useGeolocation';
|
||||
|
||||
// useToast talks to the mounted ToastContainer through window.__addToast,
|
||||
// so a spy there observes exactly what the user would get shown. Outside a
|
||||
// TranslationProvider t(key) echoes the key, so assertions target the keys.
|
||||
const addToast = vi.fn();
|
||||
|
||||
beforeEach(() => {
|
||||
addToast.mockClear();
|
||||
window.__addToast = addToast;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
delete window.__addToast;
|
||||
});
|
||||
|
||||
const noop = () => {};
|
||||
|
||||
function button(errorCode: GeoWatchErrorCode | null, error: string | null = errorCode && 'raw webkit text') {
|
||||
return <LocationButton mode="off" error={error} errorCode={errorCode} onClick={noop} />;
|
||||
}
|
||||
|
||||
describe('LocationButton', () => {
|
||||
it('FE-MAP-LOCBTN-001: shows the mode title and stays quiet without an error', () => {
|
||||
const { getByRole } = render(button(null));
|
||||
|
||||
expect(getByRole('button').getAttribute('title')).toBe('Show my location');
|
||||
expect(addToast).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-MAP-LOCBTN-002: titles with the localized error text, not the raw browser string', () => {
|
||||
const { getByRole } = render(button('timeout'));
|
||||
|
||||
expect(getByRole('button').getAttribute('title')).toBe('map.location.timeout');
|
||||
expect(getByRole('button').getAttribute('aria-label')).toBe('map.location.timeout');
|
||||
});
|
||||
|
||||
it('FE-MAP-LOCBTN-003: fires exactly one error toast when a code appears', () => {
|
||||
const { rerender } = render(button(null));
|
||||
expect(addToast).not.toHaveBeenCalled();
|
||||
|
||||
rerender(button('permission-denied'));
|
||||
expect(addToast).toHaveBeenCalledTimes(1);
|
||||
expect(addToast).toHaveBeenCalledWith('map.location.denied', 'error', 6000);
|
||||
|
||||
// The same error sticking around must not re-announce itself.
|
||||
rerender(button('permission-denied'));
|
||||
expect(addToast).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('FE-MAP-LOCBTN-004: only the transition from no error to an error toasts', () => {
|
||||
const { rerender } = render(button(null));
|
||||
rerender(button('permission-denied'));
|
||||
expect(addToast).toHaveBeenCalledTimes(1);
|
||||
|
||||
// Code to code without recovering in between stays silent.
|
||||
rerender(button('timeout'));
|
||||
expect(addToast).toHaveBeenCalledTimes(1);
|
||||
|
||||
// After a recovery a fresh failure announces itself again.
|
||||
rerender(button(null));
|
||||
rerender(button('timeout'));
|
||||
expect(addToast).toHaveBeenCalledTimes(2);
|
||||
expect(addToast).toHaveBeenLastCalledWith('map.location.timeout', 'error', 6000);
|
||||
});
|
||||
});
|
||||
@@ -1,24 +1,55 @@
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { Navigation, LocateFixed, Locate } from 'lucide-react'
|
||||
import type { TrackingMode } from '../../hooks/useGeolocation'
|
||||
import type { TrackingMode, GeoWatchErrorCode } from '../../hooks/useGeolocation'
|
||||
import { useTranslation } from '../../i18n'
|
||||
import { useToast } from '../shared/Toast'
|
||||
|
||||
interface Props {
|
||||
mode: TrackingMode
|
||||
error: string | null
|
||||
errorCode: GeoWatchErrorCode | null
|
||||
onClick: () => void
|
||||
// Offset from the bottom edge — callers push this up above the mobile
|
||||
// bottom nav. Defaults to 20px for desktop.
|
||||
bottomOffset?: number
|
||||
}
|
||||
|
||||
// The raw error string from the browser is unlocalized WebKit English, so the
|
||||
// user-facing message is looked up from the typed code instead. 'unsupported'
|
||||
// shares the generic text: a browser without geolocation cannot locate you.
|
||||
const ERROR_KEYS: Record<GeoWatchErrorCode, string> = {
|
||||
'permission-denied': 'map.location.denied',
|
||||
'unavailable': 'map.location.unavailable',
|
||||
'timeout': 'map.location.timeout',
|
||||
'unsupported': 'map.location.unavailable',
|
||||
}
|
||||
|
||||
// Three-state FAB. Matches the Apple/Google Maps pattern:
|
||||
// off → outline locate icon
|
||||
// show → filled locate (blue dot is visible on the map)
|
||||
// follow → filled navigation arrow (map follows + rotates with heading)
|
||||
export default function LocationButton({ mode, error, onClick, bottomOffset = 20 }: Props) {
|
||||
export default function LocationButton({ mode, error, errorCode, onClick, bottomOffset = 20 }: Props) {
|
||||
const { t } = useTranslation()
|
||||
const toast = useToast()
|
||||
// A title tooltip is invisible on touch, which is exactly where geolocation
|
||||
// errors happen (iOS PWA, see discussion #2095), so surface new errors as a
|
||||
// toast. The ref guards against re-fires: only the transition from no error
|
||||
// to an error announces itself.
|
||||
const prevCodeRef = useRef<GeoWatchErrorCode | null>(null)
|
||||
useEffect(() => {
|
||||
const prev = prevCodeRef.current
|
||||
prevCodeRef.current = errorCode
|
||||
if (errorCode && prev === null) {
|
||||
// The denied message is long, so give the toast extra time to be read.
|
||||
toast.error(t(ERROR_KEYS[errorCode]), 6000)
|
||||
}
|
||||
}, [errorCode, t, toast])
|
||||
|
||||
const Icon = mode === 'follow' ? Navigation : mode === 'show' ? LocateFixed : Locate
|
||||
const isActive = mode !== 'off'
|
||||
const title = error
|
||||
? error
|
||||
const errorText = errorCode ? t(ERROR_KEYS[errorCode]) : error
|
||||
const title = errorText
|
||||
? errorText
|
||||
: mode === 'off'
|
||||
? 'Show my location'
|
||||
: mode === 'show'
|
||||
|
||||
@@ -36,6 +36,7 @@ const geoMock = vi.hoisted(() => ({
|
||||
position: null as { lat: number; lng: number; accuracy: number; heading: number | null } | null,
|
||||
mode: 'off' as 'off' | 'show' | 'follow',
|
||||
error: null as string | null,
|
||||
errorCode: null as string | null,
|
||||
cycleMode: vi.fn(),
|
||||
setMode: vi.fn(),
|
||||
}))
|
||||
|
||||
@@ -802,7 +802,7 @@ export const MapView = memo(function MapView({
|
||||
const TooltipOverlay = !hoverDisabled && hoveredPlace && tooltipPos && !isTouchDevice
|
||||
const CatIcon = TooltipOverlay ? getCategoryIcon(hoveredPlace.category_icon) : null
|
||||
|
||||
const { position: userPosition, mode: trackingMode, error: trackingError, cycleMode: cycleTrackingMode } = useGeolocation()
|
||||
const { position: userPosition, mode: trackingMode, error: trackingError, errorCode: trackingErrorCode, cycleMode: cycleTrackingMode } = useGeolocation()
|
||||
// Desktop browsers only get IP-based geolocation (city-level accuracy),
|
||||
// so the button would be misleading. Mobile, where real GPS lives, keeps it.
|
||||
const isMobile = typeof window !== 'undefined' && window.innerWidth < 768
|
||||
@@ -941,6 +941,7 @@ export const MapView = memo(function MapView({
|
||||
{isMobile && <LocationButton
|
||||
mode={trackingMode}
|
||||
error={trackingError}
|
||||
errorCode={trackingErrorCode}
|
||||
onClick={cycleTrackingMode}
|
||||
bottomOffset={locationButtonBottom as unknown as number}
|
||||
/>}
|
||||
|
||||
@@ -195,6 +195,7 @@ const geoStub = vi.hoisted(() => ({
|
||||
position: null as GeoPosition | null,
|
||||
mode: 'off' as TrackingMode,
|
||||
error: null as string | null,
|
||||
errorCode: null as string | null,
|
||||
cycleMode: vi.fn(async () => {}),
|
||||
setMode: vi.fn((_m: TrackingMode | ((prev: TrackingMode) => TrackingMode)) => {}),
|
||||
}))
|
||||
|
||||
@@ -509,7 +509,7 @@ export function MapViewGL({
|
||||
onViewportChangeRef.current = onViewportChange
|
||||
const onMapReadyRef = useRef(onMapReady)
|
||||
onMapReadyRef.current = onMapReady
|
||||
const { position: userPosition, mode: trackingMode, error: trackingError, cycleMode: cycleTrackingMode, setMode: setTrackingMode } = useGeolocation()
|
||||
const { position: userPosition, mode: trackingMode, error: trackingError, errorCode: trackingErrorCode, cycleMode: cycleTrackingMode, setMode: setTrackingMode } = useGeolocation()
|
||||
const onClickRefs = useRef({ marker: onMarkerClick, map: onMapClick, context: onMapContextMenu })
|
||||
onClickRefs.current.marker = onMarkerClick
|
||||
onClickRefs.current.map = onMapClick
|
||||
@@ -1548,6 +1548,7 @@ export function MapViewGL({
|
||||
<LocationButton
|
||||
mode={trackingMode}
|
||||
error={trackingError}
|
||||
errorCode={trackingErrorCode}
|
||||
onClick={cycleTrackingMode}
|
||||
bottomOffset={buttonBottom as unknown as number}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user