forked from MapComplete/MapComplete
Huge refactoring: split readonly and writable stores
This commit is contained in:
parent
0946d8ac9c
commit
4283b76f36
95 changed files with 819 additions and 625 deletions
|
@ -91,7 +91,7 @@ export default class WikidataPreviewBox extends VariableUiElement {
|
|||
let link = new Link(
|
||||
new Combine([
|
||||
wikidata.id,
|
||||
options.noImages ? wikidata.id : Svg.wikidata_svg().SetStyle("width: 2.5rem").SetClass("block")
|
||||
options?.noImages ? wikidata.id : Svg.wikidata_svg().SetStyle("width: 2.5rem").SetClass("block")
|
||||
]).SetClass("flex"),
|
||||
Wikidata.IdToArticle(wikidata.id), true)?.SetClass("must-link")
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ import Combine from "../Base/Combine";
|
|||
import {InputElement} from "../Input/InputElement";
|
||||
import {TextField} from "../Input/TextField";
|
||||
import Translations from "../i18n/Translations";
|
||||
import {UIEventSource} from "../../Logic/UIEventSource";
|
||||
import {ImmutableStore, Store, Stores, UIEventSource} from "../../Logic/UIEventSource";
|
||||
import Wikidata, {WikidataResponse} from "../../Logic/Web/Wikidata";
|
||||
import Locale from "../i18n/Locale";
|
||||
import {VariableUiElement} from "../Base/VariableUIElement";
|
||||
|
@ -10,6 +10,7 @@ import WikidataPreviewBox from "./WikidataPreviewBox";
|
|||
import Title from "../Base/Title";
|
||||
import WikipediaBox from "./WikipediaBox";
|
||||
import Svg from "../../Svg";
|
||||
import Loading from "../Base/Loading";
|
||||
|
||||
export default class WikidataSearchBox extends InputElement<string> {
|
||||
|
||||
|
@ -51,48 +52,55 @@ export default class WikidataSearchBox extends InputElement<string> {
|
|||
})
|
||||
const selectedWikidataId = this.wikidataId
|
||||
|
||||
const lastSearchResults = new UIEventSource<WikidataResponse[]>([])
|
||||
const searchFailMessage = new UIEventSource(undefined)
|
||||
searchField.GetValue().addCallbackAndRunD(searchText => {
|
||||
if (searchText.length < 3) {
|
||||
return;
|
||||
const tooShort = new ImmutableStore<{success: WikidataResponse[]}>({success: undefined})
|
||||
const searchResult: Store<{success?: WikidataResponse[], error?: any}> = searchField.GetValue().bind(
|
||||
searchText => {
|
||||
if (searchText.length < 3) {
|
||||
return tooShort;
|
||||
}
|
||||
const lang = Locale.language.data
|
||||
const key = lang + ":" + searchText
|
||||
let promise = WikidataSearchBox._searchCache.get(key)
|
||||
if (promise === undefined) {
|
||||
promise = Wikidata.searchAndFetch(searchText, {
|
||||
lang,
|
||||
maxCount: 5,
|
||||
notInstanceOf: this.notInstanceOf,
|
||||
instanceOf: this.instanceOf
|
||||
}
|
||||
)
|
||||
WikidataSearchBox._searchCache.set(key, promise)
|
||||
}
|
||||
return Stores.FromPromiseWithErr(promise)
|
||||
}
|
||||
searchFailMessage.setData(undefined)
|
||||
)
|
||||
|
||||
|
||||
const lang = Locale.language.data
|
||||
const key = lang + ":" + searchText
|
||||
let promise = WikidataSearchBox._searchCache.get(key)
|
||||
if (promise === undefined) {
|
||||
promise = Wikidata.searchAndFetch(searchText, {
|
||||
lang,
|
||||
maxCount: 5,
|
||||
notInstanceOf: this.notInstanceOf,
|
||||
instanceOf: this.instanceOf
|
||||
}
|
||||
)
|
||||
WikidataSearchBox._searchCache.set(key, promise)
|
||||
}
|
||||
|
||||
lastSearchResults.WaitForPromise(promise, err => searchFailMessage.setData(err))
|
||||
|
||||
})
|
||||
|
||||
|
||||
const previews = new VariableUiElement(lastSearchResults.map(searchResults => {
|
||||
if (searchFailMessage.data !== undefined) {
|
||||
return new Combine([Translations.t.general.wikipedia.failed.Clone().SetClass("alert"), searchFailMessage.data])
|
||||
}
|
||||
const previews = new VariableUiElement(searchResult.map(searchResultsOrFail => {
|
||||
|
||||
if (searchField.GetValue().data.length === 0) {
|
||||
return Translations.t.general.wikipedia.doSearch
|
||||
}
|
||||
|
||||
if (searchField.GetValue().data.length < 3) {
|
||||
return Translations.t.general.wikipedia.searchToShort
|
||||
}
|
||||
|
||||
if( searchResultsOrFail === undefined) {
|
||||
return new Loading(Translations.t.general.loading)
|
||||
}
|
||||
|
||||
if (searchResultsOrFail.error !== undefined) {
|
||||
return new Combine([Translations.t.general.wikipedia.failed.Clone().SetClass("alert"), searchResultsOrFail.error])
|
||||
}
|
||||
|
||||
|
||||
const searchResults = searchResultsOrFail.success;
|
||||
if (searchResults.length === 0) {
|
||||
return Translations.t.general.wikipedia.noResults.Subs({search: searchField.GetValue().data ?? ""})
|
||||
}
|
||||
|
||||
|
||||
|
||||
return new Combine(searchResults.map(wikidataresponse => {
|
||||
const el = WikidataPreviewBox.WikidataResponsePreview(wikidataresponse).SetClass("rounded-xl p-1 sm:p-2 md:p-3 m-px border-2 sm:border-4 transition-colors")
|
||||
el.onClick(() => {
|
||||
|
@ -110,7 +118,7 @@ export default class WikidataSearchBox extends InputElement<string> {
|
|||
|
||||
})).SetClass("flex flex-col")
|
||||
|
||||
}, [searchFailMessage]))
|
||||
}, [searchField.GetValue()]))
|
||||
|
||||
const full = new Combine([
|
||||
new Title(Translations.t.general.wikipedia.searchWikidata, 3).SetClass("m-2"),
|
||||
|
|
|
@ -8,7 +8,7 @@ import Title from "../Base/Title";
|
|||
import Wikipedia from "../../Logic/Web/Wikipedia";
|
||||
import Wikidata, {WikidataResponse} from "../../Logic/Web/Wikidata";
|
||||
import {TabbedComponent} from "../Base/TabbedComponent";
|
||||
import {UIEventSource} from "../../Logic/UIEventSource";
|
||||
import {Store, UIEventSource} from "../../Logic/UIEventSource";
|
||||
import Loading from "../Base/Loading";
|
||||
import {FixedUiElement} from "../Base/FixedUiElement";
|
||||
import Translations from "../i18n/Translations";
|
||||
|
@ -128,7 +128,7 @@ export default class WikipediaBox extends Combine {
|
|||
|
||||
const wp = Translations.t.general.wikipedia;
|
||||
|
||||
const wikiLink: UIEventSource<[string, string, WikidataResponse] | "loading" | "failed" | ["no page", WikidataResponse]> =
|
||||
const wikiLink: Store<[string, string, WikidataResponse] | "loading" | "failed" | ["no page", WikidataResponse]> =
|
||||
Wikidata.LoadWikidataEntry(wikidataId)
|
||||
.map(maybewikidata => {
|
||||
if (maybewikidata === undefined) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue