Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(journey): the location search suggests as you type, like the planner

Mauricee0c3642874b3
diff --git a/client/src/components/Journey/useJourneyEntryForm.test.ts b/client/src/components/Journey/useJourneyEntryForm.test.ts
index 6e31c753c..6f5ae0840 100644
--- a/client/src/components/Journey/useJourneyEntryForm.test.ts
+++ b/client/src/components/Journey/useJourneyEntryForm.test.ts
@@ -306,6 +306,35 @@ describe('useJourneyEntryForm', () => {
expect(result.current.showLocationResults).toBe(false);
});
+ it('FE-JRN-ENTRYFORM-013b: suggestions come first, as in the planner, and a pick fetches its point (#3003)', async () => {
+ vi.useFakeTimers();
+ const suggest = vi.spyOn(mapsApi, 'autocomplete').mockResolvedValue({
+ suggestions: [{ placeId: 'g1', mainText: 'Hotel Keihan Temmabashi', secondaryText: 'Osaka, Japan' }],
+ source: 'google',
+ } as never);
+ const search = vi.spyOn(mapsApi, 'search');
+ const details = vi.spyOn(mapsApi, 'details').mockResolvedValue({ place: { lat: 34.69, lng: 135.51 } } as never);
+ const { result } = setup();
+ act(() => result.current.searchLocation('Hotel Keihan'));
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(400);
+ });
+ expect(suggest).toHaveBeenCalledWith('Hotel Keihan', 'en', undefined, expect.any(AbortSignal), expect.any(String));
+ expect(search).not.toHaveBeenCalled();
+ expect(result.current.locationResults).toEqual([
+ { name: 'Hotel Keihan Temmabashi', address: 'Osaka, Japan', lat: null, lng: null, placeId: 'g1' },
+ ]);
+
+ await act(async () => {
+ result.current.pickLocation(result.current.locationResults[0]);
+ await vi.advanceTimersByTimeAsync(0);
+ });
+ expect(details).toHaveBeenCalledWith('g1', 'en', expect.any(String));
+ expect(result.current.locationName).toBe('Hotel Keihan Temmabashi');
+ expect(result.current.locationLat).toBe(34.69);
+ expect(result.current.locationLng).toBe(135.51);
+ });
+
it('FE-JRN-ENTRYFORM-014: "use my location" fills the point, then the place name', async () => {
locate.mockResolvedValue({ lat: 1.234567, lng: 2.345678 });
vi.spyOn(mapsApi, 'reverse').mockResolvedValue({ name: 'Here' } as never);
diff --git a/client/src/components/Journey/useJourneyEntryForm.ts b/client/src/components/Journey/useJourneyEntryForm.ts
index 1b93993cc..a03af5447 100644
--- a/client/src/components/Journey/useJourneyEntryForm.ts
+++ b/client/src/components/Journey/useJourneyEntryForm.ts
@@ -4,11 +4,13 @@ import { useEffect, useMemo, useRef, useState } from 'react';
import { journeyApi, mapsApi, weatherApi } from '../../api/client';
import { getCurrentPositionOnce } from '../../hooks/useGeolocation';
import { usePlaceLanguage } from '../../hooks/usePlaceLanguage';
+import { usePlaceSuggestions } from '../../hooks/usePlaceSuggestions';
import { useTranslation } from '../../i18n';
import type { GalleryPhoto, JourneyEntry, JourneyPhoto, JourneyTrip } from '../../store/journeyStore';
import { getApiErrorMessage } from '../../types';
import { normalizeImageFiles } from '../../utils/convertHeic';
import { localIsoDate } from '../../utils/localDate';
+import { PlacesSession } from '../../utils/placesSession';
import type { ResilientResult, UploadProgress } from '../../utils/uploadQueue';
import { useToast } from '../shared/Toast';
import type { ProviderPhotoGroup } from './JourneyDetailPageProviderPicker';
@@ -22,8 +24,10 @@ export type PendingProviderGroup = ProviderPhotoGroup & { provider: string };
export interface LocationResult {
name: string;
address?: string;
- lat: number;
- lng: number;
+ /** Null for a suggestion while typing: its point comes with the details when it is picked. */
+ lat: number | null;
+ lng: number | null;
+ placeId?: string;
}
export interface JourneyEntryFormOptions {
@@ -104,6 +108,11 @@ export function useJourneyEntryForm({
const locationTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
// Counts location lookups, so an answer that comes back after the field moved on is dropped.
const locationSearchSeqRef = useRef(0);
+ // The planner's typed-ahead search (usePlaceSuggestions): names and their place in the
+ // user's language as the provider has them, billed as one session per search (#3003).
+ const { autocomplete } = usePlaceSuggestions();
+ const placesSessionRef = useRef(new PlacesSession());
+ const suggestAbortRef = useRef<AbortController | null>(null);
const [mood, setMood] = useState(entry.mood || '');
const [weather, setWeather] = useState(entry.weather || '');
// The icon the form filled in by itself (see the weather effect below). It is a
@@ -456,7 +465,33 @@ export function useJourneyEntryForm({
}
locationTimerRef.current = setTimeout(async () => {
setLocationSearching(true);
+ suggestAbortRef.current?.abort();
+ const controller = new AbortController();
+ suggestAbortRef.current = controller;
try {
+ // Suggestions first, as the planner does; the full search when they bring nothing.
+ const suggested = await autocomplete(
+ query,
+ placeLang,
+ undefined,
+ controller.signal,
+ placesSessionRef.current.current()
+ )
+ .then((r) => r.suggestions ?? [])
+ .catch(() => []);
+ if (seq !== locationSearchSeqRef.current) return;
+ if (suggested.length > 0) {
+ setLocationResults(
+ suggested.slice(0, 6).map((sg) => ({
+ name: sg.mainText,
+ address: sg.secondaryText || undefined,
+ lat: sg.lat ?? null,
+ lng: sg.lng ?? null,
+ placeId: sg.placeId,
+ }))
+ );
+ return;
+ }
const res = await mapsApi.search(query, placeLang);
if (seq !== locationSearchSeqRef.current) return;
setLocationResults(
@@ -478,7 +513,7 @@ export function useJourneyEntryForm({
};
const pickLocation = (r: LocationResult) => {
- locationSearchSeqRef.current++;
+ const seq = ++locationSearchSeqRef.current;
setLocationSearching(false);
setLocationName(r.name);
setLocationLat(r.lat);
@@ -486,6 +521,26 @@ export function useJourneyEntryForm({
setLocationQuery('');
setShowLocationResults(false);
setLocationResults([]);
+ if (r.lat != null && r.lng != null) {
+ placesSessionRef.current.end();
+ return;
+ }
+ // A suggestion names the place; its point comes with the details, which close the session.
+ if (!r.placeId) return;
+ void mapsApi
+ .details(r.placeId, placeLang, placesSessionRef.current.peek())
+ .then((res) => {
+ if (seq !== locationSearchSeqRef.current) return;
+ const place = res.place;
+ if (place?.lat == null || place?.lng == null) return;
+ setLocationLat(Number(place.lat));
+ setLocationLng(Number(place.lng));
+ })
+ .catch(() => {
+ // The name stays without a point, as a typed one would; the map simply has no pin for it.
+ if (seq === locationSearchSeqRef.current) setLocationLat(null);
+ })
+ .finally(() => placesSessionRef.current.end());
};
const handleUseCurrentLocation = async () => {