Refactored relevant methods to SearchHistory

This commit is contained in:
Simon Cambier
2024-10-13 17:49:06 +02:00
parent 973b632763
commit e2962ec021
6 changed files with 46 additions and 37 deletions

View File

@@ -0,0 +1,38 @@
import type OmnisearchPlugin from '../main'
export class SearchHistory {
/**
* Show an empty input field next time the user opens Omnisearch modal
*/
private nextQueryIsEmpty = false
constructor(private plugin: OmnisearchPlugin) {}
public async addToHistory(query: string): Promise<void> {
if (!query) {
this.nextQueryIsEmpty = true
return
}
this.nextQueryIsEmpty = false
const database = this.plugin.database
let history = await database.searchHistory.toArray()
history = history.filter(s => s.query !== query).reverse()
history.unshift({ query })
history = history.slice(0, 10)
await database.searchHistory.clear()
await database.searchHistory.bulkAdd(history)
}
/**
* @returns The search history, in reverse chronological order
*/
public async getHistory(): Promise<ReadonlyArray<string>> {
const data = (await this.plugin.database.searchHistory.toArray())
.reverse()
.map(o => o.query)
if (this.nextQueryIsEmpty) {
data.unshift('')
}
return data
}
}