diff --git a/client/android/app/src/main/java/com/liketrek/trek/share/ShareItem.kt b/client/android/app/src/main/java/com/liketrek/trek/share/ShareItem.kt index fe4aa1718..0b1628fcf 100644 --- a/client/android/app/src/main/java/com/liketrek/trek/share/ShareItem.kt +++ b/client/android/app/src/main/java/com/liketrek/trek/share/ShareItem.kt @@ -139,7 +139,8 @@ object ShareLoader { /** The link alone, as a text/uri-list the app recognises as nothing to read a booking from. */ internal fun linkOnly(link: String, subject: String?): ShareItem { - val host = runCatching { Uri.parse(link).host }.getOrNull() + // java.net.URI rather than android.net.Uri: the same host, and readable in a JVM unit test. + val host = runCatching { java.net.URI(link).host }.getOrNull() return ShareItem(ShareItem.Kind.PAGE, "link.url", "text/uri-list", link.toByteArray(), subject ?: host ?: link, host, link) } diff --git a/client/src/mobile/components/NativeImportSheet.test.tsx b/client/src/mobile/components/NativeImportSheet.test.tsx index 5cafcd0a5..ec5f6da4c 100644 --- a/client/src/mobile/components/NativeImportSheet.test.tsx +++ b/client/src/mobile/components/NativeImportSheet.test.tsx @@ -67,11 +67,11 @@ describe('NativeImportSheet', () => { it('asks what a shared page is for, and keeps it among the links of the trip picked', async () => { render(); act(() => useSharedImportStore.getState().setFile(page(), link())); - expect(screen.getByText('native.share.linkTitle')).toBeInTheDocument(); + expect(screen.getByText('What should TREK do with this link?')).toBeInTheDocument(); expect(screen.queryByRole('button', { name: /Japan/ })).not.toBeInTheDocument(); - fireEvent.click(screen.getByRole('button', { name: /native\.share\.saveLink/ })); - expect(screen.getByText('native.share.linkTrip')).toBeInTheDocument(); + fireEvent.click(screen.getByRole('button', { name: /Save to the trip's links/ })); + expect(screen.getByText('Save the link to which trip?')).toBeInTheDocument(); fireEvent.click(await screen.findByRole('button', { name: /Japan/ })); await waitFor(() => expect(navigate).toHaveBeenCalledWith('/trips/2?tab=collab')); expect(saveSharedLink).toHaveBeenCalledWith(2, link()); @@ -81,7 +81,7 @@ describe('NativeImportSheet', () => { it('reads a booking from the page when that is asked for', async () => { render(); act(() => useSharedImportStore.getState().setFile(page(), link())); - fireEvent.click(screen.getByRole('button', { name: /native\.share\.readBooking/ })); + fireEvent.click(screen.getByRole('button', { name: /Read a booking from it/ })); fireEvent.click(await screen.findByRole('button', { name: /Japan/ })); expect(useSharedImportStore.getState().tripId).toBe(2); expect(navigate).toHaveBeenCalledWith('/trips/2'); @@ -99,11 +99,11 @@ describe('NativeImportSheet', () => { useAddonStore.setState({ addons: [], loaded: true }); render(); act(() => useSharedImportStore.getState().setFile(page(), link())); - expect(screen.queryByText('native.share.linkTitle')).not.toBeInTheDocument(); + expect(screen.queryByText('What should TREK do with this link?')).not.toBeInTheDocument(); expect(await screen.findAllByRole('button', { name: /Japan|Past trip/ })).toHaveLength(2); act(() => useSharedImportStore.getState().setFile(bareLink(), link())); - expect(screen.getByText('native.share.linkUnsupported')).toBeInTheDocument(); + expect(screen.getByText('Turn on Collab to keep links in a trip.')).toBeInTheDocument(); expect(screen.queryByRole('button', { name: /Japan/ })).not.toBeInTheDocument(); }); diff --git a/client/src/mobile/components/NativeImportSheet.tsx b/client/src/mobile/components/NativeImportSheet.tsx index 751dbe2e1..91350f43f 100644 --- a/client/src/mobile/components/NativeImportSheet.tsx +++ b/client/src/mobile/components/NativeImportSheet.tsx @@ -8,11 +8,11 @@ * could not read, can only be kept; with Collaboration off, a page can only be read. */ import { ChevronRight, FileText, Link2, ScanText } from 'lucide-react'; -import React, { useEffect, useRef, useState } from 'react'; +import React, { useEffect, useEffectEvent, useState } from 'react'; import { useLocation, useNavigate } from 'react-router'; import { useToast } from '../../components/shared/Toast'; import { useTranslation } from '../../i18n'; -import { saveSharedLink, useSharedImportStore } from '../../native/sharedFiles'; +import { saveSharedLink, useSharedImportStore, type SharedLink } from '../../native/sharedFiles'; import { tripRepo } from '../../repo/tripRepo'; import { useAddonStore } from '../../store/addonStore'; import { offlineOr } from '../../utils/needsConnection'; @@ -36,35 +36,21 @@ function linkChoicesOf(file: File, collabOn: boolean): LinkChoice[] { return choices; } -export default function NativeImportSheet(): React.ReactElement | null { - const { t } = useTranslation(); - const toast = useToast(); - const navigate = useNavigate(); - const { pathname } = useLocation(); - const file = useSharedImportStore((s) => s.file); - const tripId = useSharedImportStore((s) => s.tripId); - const link = useSharedImportStore((s) => s.link); - const chooseTrip = useSharedImportStore((s) => s.chooseTrip); - const setFile = useSharedImportStore((s) => s.setFile); - const collabOn = useAddonStore((s) => s.isEnabled('collab')); +/** Where the sheet stands for the shared item: what it asks, and what a tap on a trip does. */ +function stageOf(link: SharedLink | null, choices: LinkChoice[], picked: LinkChoice | null) { + if (!link) return { choice: 'booking' as LinkChoice | null, asking: false, nowhere: false }; + const choice = picked ?? (choices.length === 1 ? choices[0] : null); + return { choice, asking: choice == null && choices.length > 1, nowhere: choices.length === 0 }; +} + +function titleKeyOf(asking: boolean, choice: LinkChoice | null): string { + if (asking) return 'native.share.linkTitle'; + return choice === 'link' ? 'native.share.linkTrip' : 'native.share.title'; +} + +/** The trips to choose from, those still ahead first. */ +function useTripRows(open: boolean): TripRow[] { const [trips, setTrips] = useState([]); - const [picked, setPicked] = useState(null); - const [saving, setSaving] = useState(false); - - // Each shared item is asked about afresh. - useEffect(() => { - setPicked(null); - setSaving(false); - }, [file]); - - const choices = file && link ? linkChoicesOf(file, collabOn) : []; - // With one way to go there is nothing to ask. - const choice: LinkChoice | null = !link ? 'booking' : (picked ?? (choices.length === 1 ? choices[0] : null)); - const asking = !!link && choice == null && choices.length > 1; - const nowhere = !!link && choices.length === 0; - - const open = !!file && tripId == null; - useEffect(() => { if (!open) return; tripRepo.list().then( @@ -80,6 +66,30 @@ export default function NativeImportSheet(): React.ReactElement | null { () => setTrips([]) ); }, [open]); + return trips; +} + +export default function NativeImportSheet(): React.ReactElement | null { + const { t } = useTranslation(); + const toast = useToast(); + const navigate = useNavigate(); + const { pathname } = useLocation(); + const file = useSharedImportStore((s) => s.file); + const tripId = useSharedImportStore((s) => s.tripId); + const link = useSharedImportStore((s) => s.link); + const chooseTrip = useSharedImportStore((s) => s.chooseTrip); + const setFile = useSharedImportStore((s) => s.setFile); + const collabOn = useAddonStore((s) => s.isEnabled('collab')); + // Both belong to the item they were made for, so a newly shared one starts afresh. + const [pick, setPick] = useState<{ file: File; choice: LinkChoice } | null>(null); + const [savingFile, setSavingFile] = useState(null); + const picked = file && pick?.file === file ? pick.choice : null; + const saving = !!file && savingFile === file; + + const choices = file && link ? linkChoicesOf(file, collabOn) : []; + const { choice, asking, nowhere } = stageOf(link, choices, picked); + const open = !!file && tripId == null; + const trips = useTripRows(open); // Once a trip is picked (here, or in the share sheet before the app opened), go to // it; its planner opens the booking import with the file (MTripShell). @@ -89,8 +99,8 @@ export default function NativeImportSheet(): React.ReactElement | null { }, [file, tripId, pathname, navigate]); const saveLink = async (id: number) => { - if (!link || saving) return; - setSaving(true); + if (!link || !file || saving) return; + setSavingFile(file); try { await saveSharedLink(id, link); toast.success(t('native.share.linkSaved')); @@ -98,23 +108,21 @@ export default function NativeImportSheet(): React.ReactElement | null { void navigate(`/trips/${id}?tab=collab`); } catch { toast.error(offlineOr(t, t('common.error'))); - setSaving(false); + setSavingFile(null); } }; // The trip the share sheet picked, used as soon as it is clear what the link is for. - // saveLink is a fresh function on every render, so the effect reaches it through a - // ref and runs on the trip and the choice alone. const preset = link?.presetTripId ?? null; - const saveLinkRef = useRef(saveLink); - saveLinkRef.current = saveLink; + const applyPreset = useEffectEvent((id: number, use: LinkChoice) => { + if (use === 'booking') chooseTrip(id); + else void saveLink(id); + }); useEffect(() => { - if (preset == null || choice == null || !file) return; - if (choice === 'booking') chooseTrip(preset); - else void saveLinkRef.current(preset); - }, [preset, choice, file, chooseTrip]); + if (preset != null && choice != null && file) applyPreset(preset, choice); + }, [preset, choice, file]); - const pick = (id: number) => (choice === 'link' ? void saveLink(id) : chooseTrip(id)); + const pickTrip = (id: number) => (choice === 'link' ? void saveLink(id) : chooseTrip(id)); // While the picked trip takes the link, there is nothing to choose; should it fail, the trips come back. const showTrips = !asking && !nowhere && !(preset != null && choice === 'link' && saving); @@ -124,63 +132,77 @@ export default function NativeImportSheet(): React.ReactElement | null {
: } - title={t( - asking ? 'native.share.linkTitle' : choice === 'link' ? 'native.share.linkTrip' : 'native.share.title' - )} + title={t(titleKeyOf(asking, choice))} sub={link ? link.title : file?.name} onClose={() => setFile(null)} closeLabel={t('common.close')} />
- {asking && ( -
- } - label={t('native.share.saveLink')} - hint={t('native.share.saveLinkHint')} - onClick={() => setPicked('link')} - /> - } - label={t('native.share.readBooking')} - hint={t('native.share.readBookingHint')} - onClick={() => setPicked('booking')} - /> -
- )} + {asking && file && setPick({ file, choice: use })} />} {nowhere && (

{t('native.share.linkUnsupported')}

)} - {showTrips && ( -
- {trips.map((trip) => ( - - ))} - {trips.length === 0 && ( -

- {t('native.share.noTrips')} -

- )} -
- )} + {showTrips && } ); } +function LinkQuestion({ onPick }: { onPick: (choice: LinkChoice) => void }): React.ReactElement { + const { t } = useTranslation(); + return ( +
+ } + label={t('native.share.saveLink')} + hint={t('native.share.saveLinkHint')} + onClick={() => onPick('link')} + /> + } + label={t('native.share.readBooking')} + hint={t('native.share.readBookingHint')} + onClick={() => onPick('booking')} + /> +
+ ); +} + +function TripPicker({ + trips, + disabled, + onPick, +}: { + trips: TripRow[]; + disabled: boolean; + onPick: (id: number) => void; +}): React.ReactElement { + const { t } = useTranslation(); + return ( +
+ {trips.map((trip) => ( + + ))} + {trips.length === 0 && ( +

{t('native.share.noTrips')}

+ )} +
+ ); +} + function ChoiceRow({ icon, label, diff --git a/client/src/mobile/screens/trip/tours/MTourWorkspace.tsx b/client/src/mobile/screens/trip/tours/MTourWorkspace.tsx index 54b12b0ec..732ade12b 100644 --- a/client/src/mobile/screens/trip/tours/MTourWorkspace.tsx +++ b/client/src/mobile/screens/trip/tours/MTourWorkspace.tsx @@ -1,5 +1,5 @@ import { List, Map as MapIcon } from 'lucide-react'; -import { useEffect, useState } from 'react'; +import { useState } from 'react'; import TourGpxDropZone from '../../../../components/Tours/TourGpxDropZone'; import TourWorkspaceRail from '../../../../components/Tours/TourWorkspaceRail'; import { TourPlannerToursRail } from '../../../../components/Tours/planner/TourPlannerPanels'; @@ -43,7 +43,6 @@ export default function MTourWorkspace({ planner }: Pick('tours'); const [mapShown, setMapShown] = useState(false); // The waypoint list and the stage rail reorder by HTML5 drag, which a finger cannot start. useTouchDragBridge(true); @@ -52,10 +51,15 @@ export default function MTourWorkspace({ planner }: Pick { + // change: a tap on the editor half with nothing open stays where it went. Set + // while rendering, as React has it for state that follows a prop, not in an effect. + const [pane, setPane] = useState(editing ? 'editor' : 'tours'); + const opened = `${editing}:${mode}`; + const [seen, setSeen] = useState(opened); + if (seen !== opened) { + setSeen(opened); setPane(editing ? 'editor' : 'tours'); - }, [editing, mode]); + } const showPane = split || !mapShown; const showMap = split || mapShown; diff --git a/client/src/pages/tripPlanner/b4a-wiring.test.ts b/client/src/pages/tripPlanner/b4a-wiring.test.ts index 67bad6a30..98deda047 100644 --- a/client/src/pages/tripPlanner/b4a-wiring.test.ts +++ b/client/src/pages/tripPlanner/b4a-wiring.test.ts @@ -91,16 +91,18 @@ test('B4A Core testability debt: verify source without importing the page, hook, assert.equal(collect(self, node => ts.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword).length, 0) }) -test('B4A Tours tab is readable, desktop-only, and absent with addon OFF', () => { +test('B4A Tours tab is readable, desktop-only off a large screen, and absent with addon OFF', () => { const tabs = initializer(hook, 'TRIP_TABS') const branches = collect(tabs, node => ts.isConditionalExpression(node) && node.whenTrue.getText(hook).includes("id: 'tour-planner'")) assert.equal(branches.length, 1) const branch = branches[0] assert.ok(ts.isConditionalExpression(branch)) - assert.equal(branch.condition.getText(hook), 'enabledAddons.tours && !isMobile') + // Where the tab exists and whether the touch shell offers it: tourPlannerTabOf (tripPlannerModel.test.ts). + assert.equal(branch.condition.getText(hook), 'tourTab') + assert.equal(initializer(hook, 'tourTab').getText(hook), 'tourPlannerTabOf(!!enabledAddons.tours, isMobile, largeTouch)') assert.equal(branch.whenFalse.getText(hook), '[]') - assert.match(branch.whenTrue.getText(hook), /desktopOnly: true/) + assert.match(branch.whenTrue.getText(hook), /desktopOnly: tourTab\.desktopOnly/) assert.doesNotMatch(tabs.getText(hook), /\bcan\b|canPlaceEdit|canDayEdit/) assert.match(hookText, /!validTabIds\.includes\(activeTab\)[\s\S]*?setActiveTab\('plan'\)/) assert.match(pageText, /activeTab === 'tour-planner' && enabledAddons\.tours && !isMobile/) diff --git a/client/src/pages/tripPlanner/useTripPlanner.ts b/client/src/pages/tripPlanner/useTripPlanner.ts index 88d5f54a2..7221e0fdf 100644 --- a/client/src/pages/tripPlanner/useTripPlanner.ts +++ b/client/src/pages/tripPlanner/useTripPlanner.ts @@ -226,7 +226,7 @@ export function useTripPlanner() { const tourTab = tourPlannerTabOf(!!enabledAddons.tours, isMobile, largeTouch) const TRIP_TABS = [ { id: 'plan', label: t(TRIP_TAB_LABEL_KEYS.plan), icon: Map }, - ...(tourTab ? [{ id: 'tour-planner', label: t(TRIP_TAB_LABEL_KEYS['tour-planner']), icon: Mountain, ...tourTab }] : []), + ...(tourTab ? [{ id: 'tour-planner', label: t(TRIP_TAB_LABEL_KEYS['tour-planner']), icon: Mountain, desktopOnly: tourTab.desktopOnly }] : []), { id: 'transports', label: t(TRIP_TAB_LABEL_KEYS.transports), icon: Train }, { id: 'buchungen', label: t(TRIP_TAB_LABEL_KEYS.buchungen), shortLabel: t('trip.tabs.reservationsShort'), icon: Ticket }, // Phone only: the desktop reaches the drive through the mode switch beside the diff --git a/shared/src/i18n/ca/native.ts b/shared/src/i18n/ca/native.ts index c17ba4414..bdf9d7256 100644 --- a/shared/src/i18n/ca/native.ts +++ b/shared/src/i18n/ca/native.ts @@ -107,7 +107,7 @@ const native: TranslationStrings = { 'native.share.saveLinkHint': 'Tothom al viatge el troba a Enllaços', 'native.share.readBooking': 'Llegeix-ne una reserva', 'native.share.readBookingHint': 'Per a una pàgina de confirmació', - 'native.share.linkTrip': 'A quin viatge desar l\'enllaç?', + 'native.share.linkTrip': "A quin viatge desar l'enllaç?", 'native.share.linkSaved': 'Enllaç desat', 'native.share.linkUnsupported': 'Activa Collab per guardar enllaços en un viatge.', 'native.widget.dayOf': 'Dia {n} de {m}', diff --git a/shared/src/i18n/en/native.ts b/shared/src/i18n/en/native.ts index 7620c82ff..ef27bd741 100644 --- a/shared/src/i18n/en/native.ts +++ b/shared/src/i18n/en/native.ts @@ -102,7 +102,7 @@ const native: TranslationStrings = { 'native.share.title': 'Import into which trip?', 'native.share.noTrips': 'No trip yet. Create one first, then share the document again.', 'native.share.linkTitle': 'What should TREK do with this link?', - 'native.share.saveLink': 'Save to the trip\'s links', + 'native.share.saveLink': "Save to the trip's links", 'native.share.saveLinkHint': 'Everyone on the trip finds it under Links', 'native.share.readBooking': 'Read a booking from it', 'native.share.readBookingHint': 'For a confirmation page',