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.

planner: keep a place's enrichment in the tab for ten minutes, not for good

Mauricec10a698b9f50
diff --git a/client/src/components/Planner/placeEnrichment.test.ts b/client/src/components/Planner/placeEnrichment.test.ts
index 02dd114c7..03fdf8194 100644
--- a/client/src/components/Planner/placeEnrichment.test.ts
+++ b/client/src/components/Planner/placeEnrichment.test.ts
@@ -1,4 +1,4 @@
-// FE-PLANNER-ENRICHCACHE-001 to FE-PLANNER-ENRICHCACHE-005: the session cache the editor's
+// FE-PLANNER-ENRICHCACHE-001 to FE-PLANNER-ENRICHCACHE-007: the session cache the editor's
// details column and the view's photo gallery share, so a place is asked about once.
import type { MapsPlaceEnrichmentResult } from '@trek/shared';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -25,22 +25,22 @@ afterEach(() => {
describe('placeEnrichment', () => {
it('FE-PLANNER-ENRICHCACHE-001: the key names the version, the place (or its coordinates) and the language', () => {
- expect(cacheKeyFor(SELECTION, 'de')).toBe('enrich_v4_way:1_de');
- expect(cacheKeyFor({ lat: 1.5, lng: 2, name: 'Pin' }, 'en')).toBe('enrich_v4_coords:1.5:2_en');
+ expect(cacheKeyFor(SELECTION, 'de')).toBe('enrich_v5_way:1_de');
+ expect(cacheKeyFor({ lat: 1.5, lng: 2, name: 'Pin' }, 'en')).toBe('enrich_v5_coords:1.5:2_en');
});
it('FE-PLANNER-ENRICHCACHE-006: a place of the index served with another community version is another answer', () => {
const gers = { placeId: 'gers:abc', lat: 1, lng: 2, name: 'Cafe', details: { community_version: 3 } };
- expect(cacheKeyFor(gers, 'de')).toBe('enrich_v4_gers:abc_c3_de');
+ expect(cacheKeyFor(gers, 'de')).toBe('enrich_v5_gers:abc_c3_de');
expect(cacheKeyFor({ ...gers, details: { community_version: 4 } }, 'de')).not.toBe(cacheKeyFor(gers, 'de'));
- expect(cacheKeyFor({ ...gers, details: {} }, 'de')).toBe('enrich_v4_gers:abc_de');
+ expect(cacheKeyFor({ ...gers, details: {} }, 'de')).toBe('enrich_v5_gers:abc_de');
});
it('FE-PLANNER-ENRICHCACHE-002: nothing is cached before it is stored, then both caches hold it', () => {
expect(readCachedEnrichment(SELECTION, 'de')).toBeUndefined();
storeEnrichment(SELECTION, 'de', RESULT);
expect(readCachedEnrichment(SELECTION, 'de')).toBe(RESULT);
- expect(JSON.parse(sessionStorage.getItem('enrich_v4_way:1_de') ?? '')).toEqual(RESULT);
+ expect(JSON.parse(sessionStorage.getItem('enrich_v5_way:1_de') ?? '').value).toEqual(RESULT);
// Another language is another answer.
expect(readCachedEnrichment(SELECTION, 'en')).toBeUndefined();
});
@@ -66,4 +66,16 @@ describe('placeEnrichment', () => {
expect(() => storeEnrichment(SELECTION, 'de', RESULT)).not.toThrow();
expect(readCachedEnrichment(SELECTION, 'de')).toBe(RESULT);
});
+
+ it('FE-PLANNER-ENRICHCACHE-007: an answer is reused for ten minutes, then asked again', () => {
+ const now = vi.spyOn(Date, 'now').mockReturnValue(1_000_000);
+ storeEnrichment(SELECTION, 'de', RESULT);
+ now.mockReturnValue(1_000_000 + 9 * 60 * 1000);
+ expect(readCachedEnrichment(SELECTION, 'de')).toBe(RESULT);
+ now.mockReturnValue(1_000_000 + 10 * 60 * 1000);
+ expect(readCachedEnrichment(SELECTION, 'de')).toBeUndefined();
+ // Gone from the session too: a reload does not bring it back.
+ __clearEnrichmentCacheForTests();
+ expect(readCachedEnrichment(SELECTION, 'de')).toBeUndefined();
+ });
});
diff --git a/client/src/components/Planner/placeEnrichment.ts b/client/src/components/Planner/placeEnrichment.ts
index 5a93a295e..e2d1051bf 100644
--- a/client/src/components/Planner/placeEnrichment.ts
+++ b/client/src/components/Planner/placeEnrichment.ts
@@ -19,23 +19,37 @@ export interface PlaceDetailsSelection {
* pay for the provider fan-out twice, and the gallery must reuse the pictures
* the editor already fetched for the same place.
*/
-const enrichmentCache = new Map<string, MapsPlaceEnrichmentResult>();
+const enrichmentCache = new Map<string, Stored>();
+
+/** A kept answer and when it was fetched. */
+interface Stored {
+ at: number;
+ value: MapsPlaceEnrichmentResult;
+}
+
+/**
+ * How long a kept answer is reused. A saved place reopened for editing carries
+ * no community version, so its key never changes: without an end, a picture
+ * another installation contributed would stay out of this tab until it closed.
+ * The server keeps an empty answer for the same ten minutes.
+ */
+const KEEP_MS = 10 * 60 * 1000;
/** Test seam: the module-level cache otherwise leaks between cases. */
export function __clearEnrichmentCacheForTests(): void {
enrichmentCache.clear();
}
-function readSession(key: string): MapsPlaceEnrichmentResult | undefined {
+function readSession(key: string): Stored | undefined {
try {
const raw = sessionStorage.getItem(key);
- return raw ? (JSON.parse(raw) as MapsPlaceEnrichmentResult) : undefined;
+ return raw ? (JSON.parse(raw) as Stored) : undefined;
} catch {
return undefined;
}
}
-function writeSession(key: string, value: MapsPlaceEnrichmentResult): void {
+function writeSession(key: string, value: Stored): void {
try {
sessionStorage.setItem(key, JSON.stringify(value));
} catch {
@@ -48,7 +62,7 @@ function writeSession(key: string, value: MapsPlaceEnrichmentResult): void {
* without it the tab that was open while the fix shipped keeps replaying the
* answer the fix was about — and reports it as still broken.
*/
-const ENRICH_CACHE_V = 4;
+const ENRICH_CACHE_V = 5;
export function cacheKeyFor(selection: PlaceDetailsSelection, language: string): string {
const id = selection.placeId || `coords:${selection.lat}:${selection.lng}`;
@@ -66,8 +80,12 @@ export function readCachedEnrichment(
): MapsPlaceEnrichmentResult | undefined {
const key = cacheKeyFor(selection, language);
const cached = enrichmentCache.get(key) ?? readSession(key);
- if (cached) enrichmentCache.set(key, cached);
- return cached;
+ if (!cached || typeof cached.at !== 'number' || Date.now() - cached.at >= KEEP_MS) {
+ enrichmentCache.delete(key);
+ return undefined;
+ }
+ enrichmentCache.set(key, cached);
+ return cached.value;
}
export function storeEnrichment(
@@ -76,6 +94,7 @@ export function storeEnrichment(
value: MapsPlaceEnrichmentResult
): void {
const key = cacheKeyFor(selection, language);
- enrichmentCache.set(key, value);
- writeSession(key, value);
+ const stored = { at: Date.now(), value };
+ enrichmentCache.set(key, stored);
+ writeSession(key, stored);
}