2024-09-05 02:25:03 +02:00
|
|
|
import GeocodingProvider, { GeocodingOptions, SearchResult } from "./GeocodingProvider"
|
2024-08-22 02:54:46 +02:00
|
|
|
import { MinimalLayoutInformation } from "../../Models/ThemeConfig/LayoutConfig"
|
|
|
|
|
import { SpecialVisualizationState } from "../../UI/SpecialVisualization"
|
|
|
|
|
import MoreScreen from "../../UI/BigComponents/MoreScreen"
|
2024-08-22 22:50:37 +02:00
|
|
|
import { ImmutableStore, Store } from "../UIEventSource"
|
2024-09-10 02:19:55 +02:00
|
|
|
import UserRelatedState from "../State/UserRelatedState"
|
2024-08-22 02:54:46 +02:00
|
|
|
|
|
|
|
|
export default class ThemeSearch implements GeocodingProvider {
|
|
|
|
|
|
|
|
|
|
private readonly _state: SpecialVisualizationState
|
|
|
|
|
private readonly _knownHiddenThemes: Store<Set<string>>
|
2024-08-27 23:56:54 +02:00
|
|
|
private readonly _suggestionLimit: number
|
2024-09-05 02:25:03 +02:00
|
|
|
private readonly _layersToIgnore: string[]
|
|
|
|
|
private readonly _otherThemes: MinimalLayoutInformation[]
|
2024-08-22 02:54:46 +02:00
|
|
|
|
2024-08-27 23:56:54 +02:00
|
|
|
constructor(state: SpecialVisualizationState, suggestionLimit: number) {
|
2024-08-22 02:54:46 +02:00
|
|
|
this._state = state
|
2024-09-05 02:25:03 +02:00
|
|
|
this._layersToIgnore = state.layout.layers.map(l => l.id)
|
2024-08-27 23:56:54 +02:00
|
|
|
this._suggestionLimit = suggestionLimit
|
2024-09-10 02:19:55 +02:00
|
|
|
this._knownHiddenThemes = UserRelatedState.initDiscoveredHiddenThemes(this._state.osmConnection).map(list => new Set(list))
|
2024-09-05 02:25:03 +02:00
|
|
|
this._otherThemes = MoreScreen.officialThemes.themes
|
|
|
|
|
.filter(th => th.id !== state.layout.id)
|
2024-08-22 02:54:46 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-27 23:56:54 +02:00
|
|
|
async search(query: string): Promise<SearchResult[]> {
|
2024-08-30 02:18:29 +02:00
|
|
|
return this.searchWrapped(query, 99)
|
2024-08-22 02:54:46 +02:00
|
|
|
}
|
|
|
|
|
|
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) {
|
2024-08-22 02:54:46 +02:00
|
|
|
return []
|
|
|
|
|
}
|
2024-09-05 02:25:03 +02:00
|
|
|
const sorted = MoreScreen.sortedByLowest(query, this._otherThemes, this._layersToIgnore)
|
|
|
|
|
return sorted
|
2024-09-05 02:53:25 +02:00
|
|
|
.filter(sorted => sorted.lowest < 2)
|
2024-09-05 02:25:03 +02:00
|
|
|
.map(th => th.theme)
|
2024-08-22 03:16:43 +02:00
|
|
|
.filter(th => !th.hideFromOverview || this._knownHiddenThemes.data.has(th.id))
|
2024-08-27 23:56:54 +02:00
|
|
|
.slice(0, limit)
|
2024-08-22 02:54:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|