MapComplete/src/Logic/Geocoding/RecentSearch.ts

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

90 lines
3 KiB
TypeScript
Raw Normal View History

2024-08-21 14:06:42 +02:00
import { Store, UIEventSource } from "../UIEventSource"
import { Feature } from "geojson"
import { OsmConnection } from "../Osm/OsmConnection"
2024-08-26 13:09:46 +02:00
import { GeocodeResult } from "./GeocodingProvider"
2024-08-21 14:06:42 +02:00
import { GeoOperations } from "../GeoOperations"
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"
export class RecentSearch {
2024-08-26 13:09:46 +02:00
private readonly _seenThisSession: UIEventSource<GeocodeResult[]>
public readonly seenThisSession: Store<GeocodeResult[]>
2024-08-21 14:06:42 +02:00
constructor(state: { layout: LayoutConfig, osmConnection: OsmConnection, selectedElement: Store<Feature> }) {
2024-08-23 02:16:24 +02:00
const prefs = state.osmConnection.preferencesHandler.GetLongPreference("previous-searches")
prefs.set(null)
2024-08-26 13:09:46 +02:00
this._seenThisSession = new UIEventSource<GeocodeResult[]>([])//UIEventSource.asObject<GeoCodeResult[]>(prefs, [])
this.seenThisSession = this._seenThisSession
2024-08-26 13:09:46 +02:00
prefs.addCallbackAndRunD(pref => {
if (pref === "") {
2024-08-23 02:16:24 +02:00
return
}
2024-08-26 13:09:46 +02:00
try {
const simpleArr = <GeocodeResult[]>JSON.parse(pref)
if (simpleArr.length > 0) {
this._seenThisSession.set(simpleArr)
return true
}
} catch (e) {
console.error(e, pref)
prefs.setData("")
2024-08-23 02:16:24 +02:00
}
})
this.seenThisSession.stabilized(2500).addCallbackAndRunD(seen => {
2024-08-26 13:09:46 +02:00
const results = []
2024-08-23 02:16:24 +02:00
for (let i = 0; i < Math.min(3, seen.length); i++) {
const gc = seen[i]
const simple = {
category: gc.category,
description: gc.description,
display_name: gc.display_name,
lat: gc.lat, lon: gc.lon,
osm_id: gc.osm_id,
2024-08-26 13:09:46 +02:00
osm_type: gc.osm_type,
2024-08-23 02:16:24 +02:00
}
results.push(simple)
}
prefs.setData(JSON.stringify(results))
})
2024-08-21 14:06:42 +02:00
state.selectedElement.addCallbackAndRunD(selected => {
2024-08-22 22:50:37 +02:00
2024-08-21 14:06:42 +02:00
const [osm_type, osm_id] = selected.properties.id.split("/")
2024-08-26 13:09:46 +02:00
if (!osm_id) {
2024-08-22 22:50:37 +02:00
return
}
2024-08-26 13:09:46 +02:00
if (["node", "way", "relation"].indexOf(osm_type) < 0) {
2024-08-23 02:16:24 +02:00
return
}
2024-08-21 14:06:42 +02:00
const [lon, lat] = GeoOperations.centerpointCoordinates(selected)
2024-08-26 13:09:46 +02:00
const entry = <GeocodeResult>{
2024-08-21 14:06:42 +02:00
feature: selected,
osm_id, osm_type,
2024-08-26 13:09:46 +02:00
lon, lat,
2024-08-21 14:06:42 +02:00
}
this.addSelected(entry)
})
}
2024-08-26 13:09:46 +02:00
addSelected(entry: GeocodeResult) {
const arr = [...(this.seenThisSession.data ?? []).slice(0, 20), entry]
2024-08-21 14:06:42 +02:00
const seenIds = new Set<string>()
for (let i = arr.length - 1; i >= 0; i--) {
const id = arr[i].osm_type + arr[i].osm_id
if (seenIds.has(id)) {
arr.splice(i, 1)
} else {
seenIds.add(id)
}
}
this._seenThisSession.set(arr)
}
}