mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 09:54:14 +00:00
feat(places): export a trip as GPX (#1907)
* feat(places): hand a trip back as GPX TREK reads GPX but never wrote it, so a planned trip could not reach the offline map or the handheld it was planned for. The importer already fixes the mapping: a wpt is a place, a rte or trk is a place with route_geometry, so writing is that reversed. Days have no import counterpart and become routes of their stops in order, which is the part that puts a plan on a device. XMLBuilder comes from the same fast-xml-parser the importer parses with, so nothing new is pulled in. * feat(dayplan): a GPX button beside PDF and ICS Scopes are worded by what someone wants on their device rather than by the GPX element: the whole trip, just the places for an offline map, just the days to follow a plan. An empty trip says so instead of reporting a failed download. * feat(mobile): GPX in the export sheet, which is no longer calendar-only The sheet held the PDF export next to the calendar entries and was still called Export Calendar; with GPX beside them the name was simply wrong. * i18n(dayplan): translate the GPX export into all locales * docs(map): document the GPX export * fix(dayplan): drop the GPX tooltip and centre both export menus The tooltip sat under the cursor exactly where the menu opens, so hovering the button produced both at once. The label lives on as an aria-label. Both menus now open centred under their button instead of hanging off its right edge.
This commit is contained in:
@@ -372,4 +372,83 @@ describe('DayPlanSidebarToolbar', () => {
|
||||
await user.click(screen.getByText('dayplan.pdf'))
|
||||
await waitFor(() => expect(toast.error).toHaveBeenCalledWith('dayplan.pdfError: boom'))
|
||||
})
|
||||
|
||||
// ── GPX export (#1442) ────────────────────────────────────────────────────
|
||||
describe('GPX menu', () => {
|
||||
let clickedHref: string | null
|
||||
|
||||
beforeEach(() => {
|
||||
clickedHref = null
|
||||
// jsdom has neither of these, and an anchor click would navigate.
|
||||
globalThis.URL.createObjectURL = vi.fn(() => 'blob:gpx')
|
||||
globalThis.URL.revokeObjectURL = vi.fn()
|
||||
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function (this: HTMLAnchorElement) {
|
||||
clickedHref = this.href
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => vi.restoreAllMocks())
|
||||
|
||||
const okResponse = () => ({ ok: true, status: 200, blob: async () => new Blob(['<gpx/>']) }) as unknown as Response
|
||||
|
||||
it('FE-PLANNER-DPTOOLBAR-025: the menu offers the three scopes on hover', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<DayPlanSidebarToolbar {...makeProps()} />)
|
||||
expect(screen.queryByText('dayplan.gpxAll')).not.toBeInTheDocument()
|
||||
await user.hover(screen.getByText('GPX'))
|
||||
expect(screen.getByText('dayplan.gpxAll')).toBeInTheDocument()
|
||||
expect(screen.getByText('dayplan.gpxPlaces')).toBeInTheDocument()
|
||||
expect(screen.getByText('dayplan.gpxDays')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('FE-PLANNER-DPTOOLBAR-026: each scope asks the server for exactly its own selection', async () => {
|
||||
const user = userEvent.setup()
|
||||
const fetchMock = vi.fn(async () => okResponse())
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
render(<DayPlanSidebarToolbar {...makeProps()} />)
|
||||
|
||||
for (const [label, expected] of [
|
||||
['dayplan.gpxAll', '/api/trips/1/places/export.gpx'],
|
||||
['dayplan.gpxPlaces', '/api/trips/1/places/export.gpx?dayRoutes=false'],
|
||||
['dayplan.gpxDays', '/api/trips/1/places/export.gpx?waypoints=false&tracks=false'],
|
||||
] as const) {
|
||||
await user.hover(screen.getByText('GPX'))
|
||||
await user.click(screen.getByText(label))
|
||||
await waitFor(() => expect(fetchMock).toHaveBeenCalledWith(expected, { credentials: 'include' }))
|
||||
fetchMock.mockClear()
|
||||
}
|
||||
})
|
||||
|
||||
it('FE-PLANNER-DPTOOLBAR-027: a download names the file after the trip', async () => {
|
||||
const user = userEvent.setup()
|
||||
vi.stubGlobal('fetch', vi.fn(async () => okResponse()))
|
||||
render(<DayPlanSidebarToolbar {...makeProps()} />)
|
||||
await user.hover(screen.getByText('GPX'))
|
||||
await user.click(screen.getByText('dayplan.gpxAll'))
|
||||
await waitFor(() => expect(clickedHref).toBe('blob:gpx'))
|
||||
expect(globalThis.URL.revokeObjectURL).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('FE-PLANNER-DPTOOLBAR-028: an empty trip says so instead of reporting a failure', async () => {
|
||||
const user = userEvent.setup()
|
||||
const toast = makeToast()
|
||||
vi.stubGlobal('fetch', vi.fn(async () => ({ ok: false, status: 404 }) as unknown as Response))
|
||||
render(<DayPlanSidebarToolbar {...makeProps({ toast })} />)
|
||||
await user.hover(screen.getByText('GPX'))
|
||||
await user.click(screen.getByText('dayplan.gpxAll'))
|
||||
await waitFor(() => expect(toast.info).toHaveBeenCalledWith('dayplan.gpxEmpty'))
|
||||
expect(toast.error).not.toHaveBeenCalled()
|
||||
expect(clickedHref).toBeNull()
|
||||
})
|
||||
|
||||
it('FE-PLANNER-DPTOOLBAR-029: a real failure toasts the error', async () => {
|
||||
const user = userEvent.setup()
|
||||
const toast = makeToast()
|
||||
vi.stubGlobal('fetch', vi.fn(async () => ({ ok: false, status: 500 }) as unknown as Response))
|
||||
render(<DayPlanSidebarToolbar {...makeProps({ toast })} />)
|
||||
await user.hover(screen.getByText('GPX'))
|
||||
await user.click(screen.getByText('dayplan.gpxAll'))
|
||||
await waitFor(() => expect(toast.error).toHaveBeenCalledWith('dayplan.gpxFailed'))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import React, { useState, useRef } from 'react'
|
||||
import { ChevronsDownUp, ChevronsUpDown, FileDown, Undo2, ArrowUpDown, CalendarPlus, Route as RouteIcon } from 'lucide-react'
|
||||
import { ChevronsDownUp, ChevronsUpDown, FileDown, Undo2, ArrowUpDown, CalendarPlus, MapPin, Route as RouteIcon } from 'lucide-react'
|
||||
import { DayReorderPopup } from './DayReorderPopup'
|
||||
import Tooltip from '../shared/Tooltip'
|
||||
import { useToast } from '../shared/Toast'
|
||||
@@ -7,6 +7,18 @@ import { IcsSubscribeModal } from './IcsSubscribeModal'
|
||||
import { isRoutableReservation } from '../../utils/reservationRoutes'
|
||||
import type { Trip, Day, Place, Category, AssignmentsMap, Reservation, DayNote } from '../../types'
|
||||
|
||||
/**
|
||||
* What a GPX download can carry. Worded by what someone wants on their device
|
||||
* rather than by the GPX element it maps to: "everything", "just the places" for
|
||||
* an offline map, "just the days" for following a plan. An omitted flag defaults
|
||||
* to true server-side, so the first entry needs no query at all.
|
||||
*/
|
||||
const GPX_SCOPES = [
|
||||
{ key: 'all', query: '', labelKey: 'dayplan.gpxAll', icon: FileDown },
|
||||
{ key: 'places', query: '?dayRoutes=false', labelKey: 'dayplan.gpxPlaces', icon: MapPin },
|
||||
{ key: 'days', query: '?waypoints=false&tracks=false', labelKey: 'dayplan.gpxDays', icon: RouteIcon },
|
||||
] as const
|
||||
|
||||
interface DayPlanSidebarToolbarProps {
|
||||
tripId: number
|
||||
trip: Trip
|
||||
@@ -54,6 +66,8 @@ export function DayPlanSidebarToolbar({
|
||||
const [subscribeOpen, setSubscribeOpen] = useState(false)
|
||||
const icsMenuTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
const [icsMenuVisible, setIcsMenuVisible] = useState(false)
|
||||
const gpxMenuTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
const [gpxMenuVisible, setGpxMenuVisible] = useState(false)
|
||||
|
||||
const showIcsMenu = () => {
|
||||
if (icsMenuTimeoutRef.current) clearTimeout(icsMenuTimeoutRef.current)
|
||||
@@ -67,6 +81,32 @@ export function DayPlanSidebarToolbar({
|
||||
}, 120)
|
||||
}
|
||||
|
||||
const showGpxMenu = () => {
|
||||
if (gpxMenuTimeoutRef.current) clearTimeout(gpxMenuTimeoutRef.current)
|
||||
setGpxMenuVisible(true)
|
||||
}
|
||||
const hideGpxMenu = () => {
|
||||
gpxMenuTimeoutRef.current = setTimeout(() => setGpxMenuVisible(false), 120)
|
||||
}
|
||||
|
||||
const downloadGpx = async (query: string) => {
|
||||
setGpxMenuVisible(false)
|
||||
try {
|
||||
const res = await fetch(`/api/trips/${tripId}/places/export.gpx${query}`, { credentials: 'include' })
|
||||
// 404 here means the selection is empty, which is worth its own message:
|
||||
// "nothing happened" and "the download broke" look identical otherwise.
|
||||
if (res.status === 404) { toast.info(t('dayplan.gpxEmpty')); return }
|
||||
if (!res.ok) throw new Error()
|
||||
const blob = await res.blob()
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = `${trip?.title || 'trip'}.gpx`
|
||||
a.click()
|
||||
URL.revokeObjectURL(url)
|
||||
} catch { toast.error(t('dayplan.gpxFailed')) }
|
||||
}
|
||||
|
||||
const menuItemStyle: React.CSSProperties = {
|
||||
display: 'flex', alignItems: 'center', gap: 7,
|
||||
width: '100%', padding: '7px 12px', border: 'none',
|
||||
@@ -132,7 +172,7 @@ export function DayPlanSidebarToolbar({
|
||||
onMouseEnter={showIcsMenu}
|
||||
onMouseLeave={hideIcsMenu}
|
||||
style={{
|
||||
position: 'absolute', top: 'calc(100% + 4px)', right: 0,
|
||||
position: 'absolute', top: 'calc(100% + 4px)', left: '50%', transform: 'translateX(-50%)',
|
||||
zIndex: 200, minWidth: 160,
|
||||
background: 'var(--bg-card, white)',
|
||||
borderRadius: 8, boxShadow: '0 4px 16px rgba(0,0,0,0.15)',
|
||||
@@ -181,6 +221,59 @@ export function DayPlanSidebarToolbar({
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{/* GPX — the counterpart to the GPX import in the places sidebar, sitting
|
||||
with the other exports rather than with the importer. Same hover-menu
|
||||
shape as ICS beside it. */}
|
||||
<div
|
||||
style={{ position: 'relative', flexShrink: 0 }}
|
||||
onMouseEnter={showGpxMenu}
|
||||
onMouseLeave={hideGpxMenu}
|
||||
>
|
||||
<button
|
||||
aria-haspopup="menu"
|
||||
aria-expanded={gpxMenuVisible}
|
||||
aria-label={t('dayplan.gpxTooltip')}
|
||||
style={{
|
||||
display: 'flex', alignItems: 'center', gap: 5,
|
||||
padding: '5px 10px', borderRadius: 8,
|
||||
border: '1px solid var(--border-primary)', background: 'none',
|
||||
color: 'var(--text-muted)', fontSize: 'calc(11px * var(--fs-scale-caption, 1))', fontWeight: 500,
|
||||
cursor: 'pointer', fontFamily: 'inherit',
|
||||
}}
|
||||
>
|
||||
<FileDown size={13} strokeWidth={2} />
|
||||
GPX
|
||||
</button>
|
||||
{gpxMenuVisible && (
|
||||
<div
|
||||
role="menu"
|
||||
onMouseEnter={showGpxMenu}
|
||||
onMouseLeave={hideGpxMenu}
|
||||
style={{
|
||||
position: 'absolute', top: 'calc(100% + 4px)', left: '50%', transform: 'translateX(-50%)',
|
||||
zIndex: 200, minWidth: 190,
|
||||
background: 'var(--bg-card, white)',
|
||||
borderRadius: 8, boxShadow: '0 4px 16px rgba(0,0,0,0.15)',
|
||||
border: '1px solid var(--border-faint, #e5e7eb)',
|
||||
overflow: 'hidden',
|
||||
}}
|
||||
>
|
||||
{GPX_SCOPES.map(scope => (
|
||||
<button
|
||||
key={scope.key}
|
||||
role="menuitem"
|
||||
onClick={() => downloadGpx(scope.query)}
|
||||
style={menuItemStyle}
|
||||
onMouseEnter={e => { (e.currentTarget as HTMLButtonElement).style.background = 'var(--bg-hover, #f3f4f6)' }}
|
||||
onMouseLeave={e => { (e.currentTarget as HTMLButtonElement).style.background = 'transparent' }}
|
||||
>
|
||||
<scope.icon size={12} strokeWidth={2} />
|
||||
{t(scope.labelKey)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{subscribeOpen && canManageShare && (
|
||||
<IcsSubscribeModal
|
||||
endpoint={`/api/trips/${tripId}/feed`}
|
||||
|
||||
Reference in New Issue
Block a user