Skip to content

Commit

Permalink
UX(plantnet): add retry button if detection failed, fix #2148
Browse files Browse the repository at this point in the history
  • Loading branch information
pietervdvn committed Sep 16, 2024
1 parent 9b4f59b commit e08ce00
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Logic/Web/PlantNet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
13 changes: 9 additions & 4 deletions src/UI/PlantNet/PlantNet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +23,6 @@
*/
export let imageUrls: Store<string[]>
export let onConfirm: (wikidataId: string) => void
const dispatch = createEventDispatcher<{ selected: string }>()
let collapsedMode = true
let options: UIEventSource<PlantNetSpeciesMatch[]> = new UIEventSource<PlantNetSpeciesMatch[]>(
undefined
Expand All @@ -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
}
}
Expand All @@ -60,8 +61,12 @@
<button class="w-full" on:click={detectSpecies}>
<Tr t={t.button} />
</button>
{:else if $error !== undefined}
{:else if error !== undefined}
<Tr cls="alert" t={t.error.Subs({ error })} />
<button on:click={() => detectSpecies()}>
<ArrowPath class="w-6 h-6"/>
<Tr t={Translations.t.general.retry}/>
</button>
{:else if $imageUrls.length === 0}
<!-- No urls are available, show the explanation instead-->
<div class=" border-region relative mb-1 p-2">
Expand Down
17 changes: 14 additions & 3 deletions src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1073,19 +1073,22 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
public static async downloadJsonCached<T = object | []>(
url: string,
maxCacheTimeMs: number,
headers?: Record<string, string>
headers?: Record<string, string>,
dontCacheErrors: boolean = false
): Promise<T> {
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<T = object | []>(
url: string,
maxCacheTimeMs: number,
headers?: Record<string, string>
headers?: Record<string, string>,
dontCacheErrors = false
): Promise<{ content: T } | { error: string; url: string; statuscode?: number }> {
const cached = Utils._download_cache.get(url)
if (cached !== undefined) {
Expand All @@ -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<T = object | []>(
Expand Down

0 comments on commit e08ce00

Please sign in to comment.