Search: move limit responsability to the constructor, merge similar results

This commit is contained in:
Pieter Vander Vennet 2024-08-27 23:56:54 +02:00
parent cdc1e05499
commit 6468e33d66
7 changed files with 62 additions and 23 deletions

View file

@ -11,31 +11,33 @@ export default class ThemeSearch implements GeocodingProvider {
private static allThemes: MinimalLayoutInformation[] = (themeOverview["default"] ?? themeOverview)
private readonly _state: SpecialVisualizationState
private readonly _knownHiddenThemes: Store<Set<string>>
private readonly _suggestionLimit: number
constructor(state: SpecialVisualizationState) {
constructor(state: SpecialVisualizationState, suggestionLimit: number) {
this._state = state
this._suggestionLimit = suggestionLimit
this._knownHiddenThemes = MoreScreen.knownHiddenThemes(this._state.osmConnection)
}
async search(query: string, options?: GeocodingOptions): Promise<SearchResult[]> {
return this.searchDirect(query, options)
async search(query: string): Promise<SearchResult[]> {
return this.searchDirect(query, 99)
}
suggest(query: string, options?: GeocodingOptions): Store<SearchResult[]> {
return new ImmutableStore(this.searchDirect(query, options))
return new ImmutableStore(this.searchDirect(query, this._suggestionLimit ?? 4))
}
private searchDirect(query: string, options?: GeocodingOptions): SearchResult[] {
private searchDirect(query: string, limit: number): SearchResult[] {
if(query.length < 1){
return []
}
const limit = options?.limit ?? 4
query = Utils.simplifyStringForSearch(query)
const withMatch = ThemeSearch.allThemes
.filter(th => !th.hideFromOverview || this._knownHiddenThemes.data.has(th.id))
.filter(th => th.id !== this._state.layout.id)
.filter(th => MoreScreen.MatchesLayout(th, query))
.slice(0, limit + 1)
.slice(0, limit)
console.log("Matched", withMatch, limit)
return withMatch.map(match => <SearchResult> {
payload: match,