feat(maps): show where an Amap hit came from, and keep its id

An Amap row now carries the same source mark the TREK index, OpenStreetMap
and Google rows get, named per locale because 高德地图 is the name it goes
by where it matters. The "search is on OpenStreetMap" notices and the
key hint in the details column are gone with the keys dev had already
removed, and the client-side provider mirror with them.

amap_poi_id travels with a picked result into the form, the details
column and the shadow log, so a saved place keeps the id it was found
by. A pasted Amap link goes to the resolver like a Google one.
This commit is contained in:
Maurice
2026-09-12 13:56:48 +02:00
parent 43a6c30325
commit 84c277993c
35 changed files with 148 additions and 459 deletions
+18 -7
View File
@@ -11,10 +11,11 @@
* nothing on the other is worse than either alone.
*/
import type { TranslationFn } from '../types'
/**
* The three names are proper nouns, so they are not translated, and that is
* also why there is no fourth: a source without a name people already know
* would need a string in 23 languages to say less than nothing.
* Proper nouns, so they are not translated. A source without a name people
* already know would need a string in 23 languages to say less than nothing.
*/
export const SOURCE_LABELS: Record<string, string> = {
'trek-places': 'TREK',
@@ -23,6 +24,14 @@ export const SOURCE_LABELS: Record<string, string> = {
google: 'Google',
}
/**
* The one name that changes with the reader: 高德地图 to the people it exists
* for, Amap to everyone else. Hence a locale string rather than a fourth noun.
*/
const SOURCE_KEYS: Record<string, string> = {
amap: 'places.source.amap',
}
/**
* The label for one row.
*
@@ -31,9 +40,11 @@ export const SOURCE_LABELS: Record<string, string> = {
* answered the call: Google never marks its places, and a merged list marks only
* the index side, so an unmarked row in one is OpenStreetMap by elimination.
*/
export function sourceLabelFor(place: unknown, listSource: string): string | null {
export function sourceLabelFor(place: unknown, listSource: string, t: TranslationFn): string | null {
const own = (place as { source?: unknown } | null)?.source
if (typeof own === 'string' && own) return SOURCE_LABELS[own] ?? null
if (listSource.includes('openstreetmap')) return SOURCE_LABELS.openstreetmap
return SOURCE_LABELS[listSource] ?? null
const source = typeof own === 'string' && own
? own
: listSource.includes('openstreetmap') ? 'openstreetmap' : listSource
if (SOURCE_KEYS[source]) return t(SOURCE_KEYS[source])
return SOURCE_LABELS[source] ?? null
}
-63
View File
@@ -1,63 +0,0 @@
import { describe, expect, it } from 'vitest'
import { effectivePlacesProvider, osmKeySuggestion } from './placesProvider'
/**
* These mirror MapsService.resolvePlacesProvider() on the server, case for case.
* The point of the mirror is honest notices, so every case where the notices
* would lie — "add a Google key" on an install whose admin picked Amap — is
* pinned here.
*/
describe('effectivePlacesProvider', () => {
it('auto keeps Google when a Google key is configured, Amap key or not', () => {
// The incumbent wins: an existing install must not move providers because
// a new one became available.
expect(effectivePlacesProvider({ placesProvider: 'auto', hasMapsKey: true, hasAmapKey: true })).toBe('google')
expect(effectivePlacesProvider({ placesProvider: 'auto', hasMapsKey: true, hasAmapKey: false })).toBe('google')
})
it('auto falls to Amap only when there is no Google key', () => {
expect(effectivePlacesProvider({ placesProvider: 'auto', hasMapsKey: false, hasAmapKey: true })).toBe('amap')
})
it('auto with neither key means OpenStreetMap', () => {
expect(effectivePlacesProvider({ placesProvider: 'auto', hasMapsKey: false, hasAmapKey: false })).toBe('openstreetmap')
})
it('an explicit amap choice wins even with a Google key configured', () => {
expect(effectivePlacesProvider({ placesProvider: 'amap', hasMapsKey: true, hasAmapKey: true })).toBe('amap')
})
it('an explicit google choice never silently uses Amap instead', () => {
// Misconfigured means "answer with OSM", matching the server — not "bill
// somebody else's provider".
expect(effectivePlacesProvider({ placesProvider: 'google', hasMapsKey: false, hasAmapKey: true })).toBe('openstreetmap')
})
it('openstreetmap ignores both keys', () => {
expect(effectivePlacesProvider({ placesProvider: 'openstreetmap', hasMapsKey: true, hasAmapKey: true })).toBe('openstreetmap')
})
it('a nonsense stored value degrades to auto rather than throwing', () => {
expect(effectivePlacesProvider({ placesProvider: 'not-a-provider', hasMapsKey: false, hasAmapKey: true })).toBe('amap')
expect(effectivePlacesProvider({ placesProvider: undefined, hasMapsKey: true, hasAmapKey: false })).toBe('google')
})
})
describe('osmKeySuggestion', () => {
it('a chosen provider names its own key', () => {
expect(osmKeySuggestion('google')).toBe('google')
expect(osmKeySuggestion('amap')).toBe('amap')
})
it('auto names both, because landing on OSM means neither key is set', () => {
expect(osmKeySuggestion('auto')).toBe('any')
})
it('an explicit OpenStreetMap choice gets no upsell — it is a decision, not a gap', () => {
expect(osmKeySuggestion('openstreetmap')).toBeNull()
// An unrecognised value behaves like auto: the reader degrades it to auto,
// so the notice must not claim the admin chose OSM when they chose nothing.
expect(osmKeySuggestion('garbage')).toBe('any')
expect(osmKeySuggestion(undefined)).toBe('any')
})
})
-69
View File
@@ -1,69 +0,0 @@
/**
* Which provider place search will actually use, as far as the client can tell.
*
* This mirrors the server's MapsService.resolvePlacesProvider(): the instance's
* places_provider choice, the keys the server reported for this caller, and the
* same precedence. It exists so a notice can say the truth — "search is running
* on OpenStreetMap", not "you lack a Google key" on an install whose admin
* deliberately picked Amap.
*
* A mirror and not the source of truth on purpose: the server resolves keys per
* request against the database, and only it knows for sure. If the two ever
* disagree, the client shows a slightly wrong hint under a notice and nothing
* else — search itself keeps asking the server.
*/
export type PlacesProviderChoice = 'auto' | 'google' | 'amap' | 'openstreetmap'
export type EffectiveSearchProvider = 'google' | 'amap' | 'openstreetmap'
export interface PlacesProviderState {
/** The admin's places_provider choice; anything unrecognised means 'auto'. */
placesProvider?: string | null
/** Whether a Google key resolves for this caller (operator env, instance, own row). */
hasMapsKey: boolean
hasAmapKey: boolean
}
export function effectivePlacesProvider(state: PlacesProviderState): EffectiveSearchProvider {
const choice: PlacesProviderChoice =
state.placesProvider === 'google' || state.placesProvider === 'amap' || state.placesProvider === 'openstreetmap'
? state.placesProvider
: 'auto'
if (choice === 'openstreetmap') return 'openstreetmap'
if (choice === 'google') return state.hasMapsKey ? 'google' : 'openstreetmap'
if (choice === 'amap') return state.hasAmapKey ? 'amap' : 'openstreetmap'
// auto prefers Google, the incumbent: an existing install must not move to a
// different provider, with a different bill, just because Amap appeared.
if (state.hasMapsKey) return 'google'
if (state.hasAmapKey) return 'amap'
return 'openstreetmap'
}
/**
* Which key the "still on OpenStreetMap" upsell should suggest.
*
* A chosen provider names the key that belongs to it — telling an admin who
* picked Amap to add a Google key is advice they already declined. `auto` with
* no key at all is the only case where both are worth naming.
*
* Null when the admin explicitly chose OpenStreetMap: that is a decision, not a
* gap to fill, and the notices stay quiet rather than nag.
*/
export type KeySuggestion = 'google' | 'amap' | 'any'
export function osmKeySuggestion(placesProvider?: string | null): KeySuggestion | null {
switch (placesProvider) {
case 'google':
return 'google'
case 'amap':
return 'amap'
case 'openstreetmap':
return null
// auto resolving to OSM means neither key is configured (a Google key would
// have won), so both are honest suggestions.
default:
return 'any'
}
}