From e08ce00850e5cc45c280b7ec5adb9dcbbf337a28 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Mon, 16 Sep 2024 17:33:56 +0200 Subject: [PATCH] UX(plantnet): add retry button if detection failed, fix #2148 --- src/Logic/Web/PlantNet.ts | 2 +- src/UI/PlantNet/PlantNet.svelte | 13 +++++++++---- src/Utils.ts | 17 ++++++++++++++--- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/Logic/Web/PlantNet.ts b/src/Logic/Web/PlantNet.ts index 4012040e07..769fb60c9d 100644 --- a/src/Logic/Web/PlantNet.ts +++ b/src/Logic/Web/PlantNet.ts @@ -15,7 +15,7 @@ export default class PlantNet { for (const image of imageUrls) { url += "&images=" + encodeURIComponent(image) } - return Utils.downloadJsonCached(url, 365 * 24 * 60 * 60 * 1000) + return Utils.downloadJsonCached(url, 365 * 24 * 60 * 60 * 1000, undefined, true) } public static exampleResult: PlantNetResult = { diff --git a/src/UI/PlantNet/PlantNet.svelte b/src/UI/PlantNet/PlantNet.svelte index 893aa86c15..d9f0651cdb 100644 --- a/src/UI/PlantNet/PlantNet.svelte +++ b/src/UI/PlantNet/PlantNet.svelte @@ -9,8 +9,8 @@ import BackButton from "../Base/BackButton.svelte" import NextButton from "../Base/NextButton.svelte" import WikipediaPanel from "../Wikipedia/WikipediaPanel.svelte" - import { createEventDispatcher } from "svelte" import Plantnet_logo from "../../assets/svg/Plantnet_logo.svelte" + import ArrowPath from "@babeard/svelte-heroicons/mini/ArrowPath" /** * The main entry point for the plantnet wizard @@ -23,7 +23,6 @@ */ export let imageUrls: Store export let onConfirm: (wikidataId: string) => void - const dispatch = createEventDispatcher<{ selected: string }>() let collapsedMode = true let options: UIEventSource = new UIEventSource( undefined @@ -38,18 +37,20 @@ let done = false - function speciesSelected(species: PlantNetSpeciesMatch) { + function speciesSelected(species: string) { console.log("Selected:", species) selectedOption = species } async function detectSpecies() { + error = undefined collapsedMode = false try { const result = await PlantNet.query(imageUrls.data.slice(0, 5)) options.set(result.results.filter((r) => r.score > 0.005).slice(0, 8)) } catch (e) { + console.error("Caught", e) error = e } } @@ -60,8 +61,12 @@ - {:else if $error !== undefined} + {:else if error !== undefined} + {:else if $imageUrls.length === 0}
diff --git a/src/Utils.ts b/src/Utils.ts index 2a23d76fda..483c48eac0 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -1073,19 +1073,22 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be public static async downloadJsonCached( url: string, maxCacheTimeMs: number, - headers?: Record + headers?: Record, + dontCacheErrors: boolean = false ): Promise { - const result = await Utils.downloadJsonCachedAdvanced(url, maxCacheTimeMs, headers) + const result = await Utils.downloadJsonCachedAdvanced(url, maxCacheTimeMs, headers, dontCacheErrors) if (result["content"]) { return result["content"] } throw result["error"] + } public static async downloadJsonCachedAdvanced( url: string, maxCacheTimeMs: number, - headers?: Record + headers?: Record, + dontCacheErrors = false ): Promise<{ content: T } | { error: string; url: string; statuscode?: number }> { const cached = Utils._download_cache.get(url) if (cached !== undefined) { @@ -1099,7 +1102,15 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be headers ) Utils._download_cache.set(url, { promise, timestamp: new Date().getTime() }) + try { + return await promise + }catch (e) { + if(dontCacheErrors){ + Utils._download_cache.delete(url) + } + throw e + } } public static async downloadJson(