mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 02:34:21 +00:00
feat(plugins): search plugins can answer the place search as you type
A searchProvider plugin can now implement an optional suggest() beside search(). Its places then show up in the place search's typed-ahead list, after the core suggestions and marked with the plugin's name, on desktop and phone. An index that cannot take a request per keystroke leaves it out and keeps answering the full search only. The child reports which functions each hook object carries at load, so GET /api/plugin-search/suggest only asks the plugins that implement suggest: from the second character, three rows at most across all of them, 800 ms to answer. The feed marks plugins holding hook:search-provider and the client only asks the route while one is installed. A plugin row carries its whole place, so picking it skips the details lookup, which knows no plugin id. The phone's search results now name their source as the desktop ones do, and the desktop dropdown scrolls instead of running past the dialog. Addresses discussion #2221.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { googleHoldsSlot, offersGoogleRetry, placesGoogleOnlyHint, selectGoogleHoldsSlot, sourceLabelFor } from './placeSource'
|
||||
import { corePickRank, googleHoldsSlot, offersGoogleRetry, placesGoogleOnlyHint, selectGoogleHoldsSlot, sourceLabelFor } from './placeSource'
|
||||
|
||||
describe('offersGoogleRetry', () => {
|
||||
it('FE-PLACESOURCE-001: offers Google for a list the index or OpenStreetMap answered, only where Google answers', () => {
|
||||
@@ -55,4 +55,30 @@ describe('sourceLabelFor', () => {
|
||||
expect(sourceLabelFor({}, 'google', t)).toBe('Google')
|
||||
expect(sourceLabelFor({}, 'amap', t)).toBe('places.source.amap')
|
||||
})
|
||||
|
||||
it('FE-PLACESOURCE-007: a plugin row names its plugin, by the installed name when there is one', () => {
|
||||
const t = (k: string) => k
|
||||
const names: Record<string, string> = { 'all-the-places': 'All the Places' }
|
||||
const pluginName = (id: string) => names[id]
|
||||
expect(sourceLabelFor({ source: 'plugin:all-the-places' }, 'trek-places', t, pluginName)).toBe('All the Places')
|
||||
// Uninstalled since the list was drawn, or no lookup at hand: the id still says where it came from.
|
||||
expect(sourceLabelFor({ source: 'plugin:gone' }, 'trek-places', t, pluginName)).toBe('gone')
|
||||
expect(sourceLabelFor({ source: 'plugin:all-the-places' }, 'trek-places', t)).toBe('all-the-places')
|
||||
// A merged list's own source names no single row, so an unmarked core row stays unlabelled.
|
||||
expect(sourceLabelFor({}, 'trek-places+plugin:all-the-places', t, pluginName)).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('corePickRank', () => {
|
||||
it('FE-PLACESOURCE-008: ranks a core row among the core rows only, and gives a plugin row no rank at all', () => {
|
||||
const rows = [
|
||||
{ placeId: 'gers:1' },
|
||||
{ placeId: 'node:2' },
|
||||
{ placeId: 'plugin:atp:3', place: { osm_id: 'plugin:atp:3' } },
|
||||
{ placeId: 'plugin:atp:4', place: { osm_id: 'plugin:atp:4' } },
|
||||
]
|
||||
expect(corePickRank(rows, rows[1])).toEqual({ rank: 1, count: 2 })
|
||||
expect(corePickRank(rows, rows[2])).toBeUndefined()
|
||||
expect(corePickRank(rows, { placeId: 'gone' })).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -78,6 +78,28 @@ export function placesGoogleOnlyHint(hasMapsKey: boolean, placesProvider: string
|
||||
return 'admin.placesGoogleOnly.subtitle'
|
||||
}
|
||||
|
||||
/**
|
||||
* Where a picked typed-ahead row stood among the core rows, for the place search log,
|
||||
* or undefined for a row that log has no business ranking.
|
||||
*
|
||||
* A plugin's row (#2221) carries its place and was never ranked by the core index, so
|
||||
* counting it would credit the index with a hit it did not make. Plugin rows follow
|
||||
* the core ones, so a core row's rank is the same with or without them, and the count
|
||||
* leaves them out.
|
||||
*/
|
||||
export function corePickRank(
|
||||
rows: readonly { placeId: string; place?: unknown }[],
|
||||
picked: { placeId: string; place?: unknown },
|
||||
): { rank: number; count: number } | undefined {
|
||||
if (picked.place) return undefined
|
||||
const core = rows.filter(row => !row.place)
|
||||
const rank = core.findIndex(row => row.placeId === picked.placeId)
|
||||
return rank >= 0 ? { rank, count: core.length } : undefined
|
||||
}
|
||||
|
||||
/** How a plugin index marks its rows: `plugin:<pluginId>` (#2221). */
|
||||
const PLUGIN_SOURCE = 'plugin:'
|
||||
|
||||
/**
|
||||
* The label for one row.
|
||||
*
|
||||
@@ -85,12 +107,24 @@ export function placesGoogleOnlyHint(hasMapsKey: boolean, placesProvider: string
|
||||
* is what makes an interleaved list readable. Everything else falls back to what
|
||||
* 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.
|
||||
*
|
||||
* A plugin's row is named after the plugin, the name the admin installed it under,
|
||||
* which `pluginName` looks up; the plugin id stands in when it cannot.
|
||||
*/
|
||||
export function sourceLabelFor(place: unknown, listSource: string, t: TranslationFn): string | null {
|
||||
export function sourceLabelFor(
|
||||
place: unknown,
|
||||
listSource: string,
|
||||
t: TranslationFn,
|
||||
pluginName?: (pluginId: string) => string | undefined,
|
||||
): string | null {
|
||||
const own = (place as { source?: unknown } | null)?.source
|
||||
const source = typeof own === 'string' && own
|
||||
? own
|
||||
: listSource.includes('openstreetmap') ? 'openstreetmap' : listSource
|
||||
if (source.startsWith(PLUGIN_SOURCE)) {
|
||||
const pluginId = source.slice(PLUGIN_SOURCE.length)
|
||||
return pluginName?.(pluginId) || pluginId || null
|
||||
}
|
||||
if (SOURCE_KEYS[source]) return t(SOURCE_KEYS[source])
|
||||
return SOURCE_LABELS[source] ?? null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user