MapComplete/src/Logic/Geocoding/ThemeSearch.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

53 lines
2 KiB
TypeScript
Raw Normal View History

2024-08-26 13:09:46 +02:00
import GeocodingProvider, { SearchResult, GeocodingOptions } from "./GeocodingProvider"
import * as themeOverview from "../../assets/generated/theme_overview.json"
import { MinimalLayoutInformation } from "../../Models/ThemeConfig/LayoutConfig"
import { SpecialVisualizationState } from "../../UI/SpecialVisualization"
import { Utils } from "../../Utils"
import MoreScreen from "../../UI/BigComponents/MoreScreen"
2024-08-22 22:50:37 +02:00
import { ImmutableStore, Store } from "../UIEventSource"
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, suggestionLimit: number) {
this._state = state
this._suggestionLimit = suggestionLimit
this._knownHiddenThemes = MoreScreen.knownHiddenThemes(this._state.osmConnection)
}
async search(query: string): Promise<SearchResult[]> {
2024-08-30 02:18:29 +02:00
return this.searchWrapped(query, 99)
}
2024-08-26 13:09:46 +02:00
suggest(query: string, options?: GeocodingOptions): Store<SearchResult[]> {
2024-08-30 02:18:29 +02:00
return new ImmutableStore(this.searchWrapped(query, this._suggestionLimit ?? 4))
2024-08-22 22:50:37 +02:00
}
2024-08-30 02:18:29 +02:00
private searchWrapped(query: string, limit: number): SearchResult[] {
return this.searchDirect(query, limit).map(match => <SearchResult>{
payload: match,
category: "theme",
osm_id: match.id
})
}
public searchDirect(query: string, limit: number): MinimalLayoutInformation[] {
if (query.length < 1) {
return []
}
query = Utils.simplifyStringForSearch(query)
2024-08-30 02:18:29 +02:00
return 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)
}
}