This commit is contained in:
Pieter Vander Vennet 2025-08-26 00:43:08 +02:00
parent 634d4a7186
commit aed6defa16
2 changed files with 19 additions and 15 deletions

View file

@ -13,16 +13,22 @@ import { Lists } from "../../Utils/Lists"
export class ThemeSearchIndex {
private readonly themeIndex: Fuse<MinimalThemeInformation>
private readonly layerIndex: Fuse<{ id: string; description }>
/**
* A 'search' will also search matching layers from the official themes.
* This might cause themes to show up which weren't passed in 'themesToSearch', so we create a whitelist
*/
private readonly themeWhitelist: Set<string>
constructor(
language: string,
themesToSearch?: MinimalThemeInformation[],
themesToSearch: MinimalThemeInformation[],
layersToIgnore: string[] = []
) {
const themes = Utils.noNull(themesToSearch ?? ThemeSearch.officialThemes?.themes)
const themes = Utils.noNull(themesToSearch)
if (!themes) {
throw "No themes loaded. Did generate:layeroverview fail?"
}
this.themeWhitelist = new Set(themes.map(th => th.id))
const fuseOptions: IFuseOptions<MinimalThemeInformation> = {
ignoreLocation: true,
threshold: 0.2,
@ -82,6 +88,10 @@ export class ThemeSearchIndex {
const matchingThemes = ThemeSearch.layersToThemes.get(layer.item.id)
const score = layer.score
matchingThemes?.forEach((th) => {
if (!this.themeWhitelist.has(th.id)) {
// This theme was not in the 'themesToSearch'
return
}
const previous = result.get(th.id) ?? 10000
result.set(th.id, Math.min(previous, score * 5))
})

View file

@ -61,7 +61,7 @@
let userLanguages = osmConnection.userDetails.map((ud) => ud?.languages ?? [])
let search: UIEventSource<string | undefined> = new UIEventSource<string>("")
let searchStable = search.stabilized(100)
let searchStable: Store<string | undefined> = search.stabilized(100)
const officialThemes: MinimalThemeInformation[] = ThemeSearch.officialThemes.themes.filter(
(th) => th.hideFromOverview === false
@ -83,13 +83,12 @@
).mapD((stableIds) => Lists.noNullInplace(stableIds.map((id) => state.getUnofficialTheme(id))))
function filtered(themes: Store<MinimalThemeInformation[]>): Store<MinimalThemeInformation[]> {
const searchIndex = Locale.language.map(
(language) => {
return new ThemeSearchIndex(language, themes.data)
},
[themes]
const searchIndex = themes.mapD(
(themes) => new ThemeSearchIndex(Locale.language.data, themes),
[Locale.language]
)
return searchStable.map(
(searchTerm) => {
if (!themes.data) {
@ -99,11 +98,9 @@
return themes.data
}
const index = searchIndex.data
return index.search(searchTerm)
return searchIndex.data?.search(searchTerm)
},
[searchIndex]
[searchIndex, themes]
)
}
@ -134,9 +131,6 @@
{ returnToIndex: new ImmutableStore(false) }
)
const topSPace = AndroidPolyfill.getInsetSizes().top
const bottom = AndroidPolyfill.getInsetSizes().bottom
/**
* Opens the first search candidate
*/