mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 07:30:04 +00:00
2a8071f642
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.
51 lines
2.4 KiB
TypeScript
51 lines
2.4 KiB
TypeScript
import { useCallback } from 'react'
|
|
import { abortedError, mapsApi } from '../api/client'
|
|
import { useTranslation } from '../i18n'
|
|
import { pluginSearchRepo } from '../repo/pluginSearchRepo'
|
|
import { usePluginStore } from '../store/pluginStore'
|
|
import { sourceLabelFor } from '../utils/placeSource'
|
|
import { pointFromBox, type LocationBiasBox } from './useLocationBias'
|
|
|
|
/**
|
|
* The typed-ahead half of the place search, for the desktop form and the phone sheet
|
|
* alike: the core suggestions with the rows of the plugins that answer as you type
|
|
* appended (#2221), and the mark each row carries.
|
|
*
|
|
* Appended rather than interleaved, as the full search does it: the core list is
|
|
* ranked against the query and has earned its order. The plugin route is only asked
|
|
* while a search plugin is installed, so an instance without one sends exactly the
|
|
* requests it always did, and the route itself only reaches the plugins whose build
|
|
* implements `suggest`, the ones that can take a request per keystroke.
|
|
*/
|
|
export function usePlaceSuggestions() {
|
|
const { t } = useTranslation()
|
|
const plugins = usePluginStore(s => s.plugins)
|
|
const asksPlugins = plugins.some(p => p.searchProvider)
|
|
|
|
const autocomplete = useCallback(
|
|
async (input: string, lang: string, box: LocationBiasBox | undefined, signal: AbortSignal, sessionToken?: string) => {
|
|
// Side by side, and the plugin half never rejects, so only the core call can
|
|
// send the list down its error path.
|
|
const [core, fromPlugins] = await Promise.all([
|
|
mapsApi.autocomplete(input, lang, box, signal, sessionToken),
|
|
asksPlugins ? pluginSearchRepo.suggest(input, lang, pointFromBox(box), signal) : [],
|
|
])
|
|
// Both shells order their lists by aborting the previous keystroke and dropping
|
|
// its rejection. The core half can have answered before the abort while the
|
|
// plugin half was still out, so a superseded answer rejects here as well rather
|
|
// than overwriting the newer list.
|
|
if (signal.aborted) throw abortedError()
|
|
if (fromPlugins.length === 0) return core
|
|
return { ...core, suggestions: [...(core.suggestions || []), ...fromPlugins] }
|
|
},
|
|
[asksPlugins],
|
|
)
|
|
|
|
const sourceLabel = useCallback(
|
|
(row: unknown, listSource: string) => sourceLabelFor(row, listSource, t, id => plugins.find(p => p.id === id)?.name),
|
|
[plugins, t],
|
|
)
|
|
|
|
return { autocomplete, sourceLabel }
|
|
}
|