Fix wallet status when wallet is unreachable (#79) #80
@@ -68,6 +68,22 @@
|
||||
}
|
||||
}
|
||||
|
||||
const checkWebLnStatus = async () => {
|
||||
const webLn = getWebLn()
|
||||
|
||||
if (!webLn) {
|
||||
return "unavailable" as const
|
||||
}
|
||||
|
||||
try {
|
||||
await withTimeout(webLn.getInfo(), 5000)
|
||||
return "connected" as const
|
||||
} catch (error) {
|
||||
console.warn("WebLN wallet unavailable", error)
|
||||
return "unavailable" as const
|
||||
}
|
||||
}
|
||||
|
||||
let walletStatus = $state<"checking" | "connected" | "unavailable">("checking")
|
||||
let walletStatusRequestId = 0
|
||||
|
||||
@@ -80,7 +96,7 @@
|
||||
)
|
||||
const connectionVerb = $derived(walletStatus === "connected" ? "Connected to" : "Configured for")
|
||||
|
||||
$effect(() => {
|
||||
const startWalletStatusCheck = () => {
|
||||
const wallet = $session?.wallet
|
||||
|
||||
if (!wallet) {
|
||||
@@ -93,13 +109,19 @@
|
||||
walletStatus = "checking"
|
||||
|
||||
void (async () => {
|
||||
const nextStatus =
|
||||
wallet.type === "nwc" ? await checkNwcStatus() : getWebLn() ? "connected" : "unavailable"
|
||||
const nextStatus = wallet.type === "nwc" ? await checkNwcStatus() : await checkWebLnStatus()
|
||||
|
||||
if (requestId === walletStatusRequestId) {
|
||||
walletStatus = nextStatus
|
||||
|
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
const retryStatus = () => startWalletStatusCheck()
|
||||
|
||||
$effect(() => {
|
||||
$session?.wallet
|
||||
startWalletStatusCheck()
|
||||
})
|
||||
</script>
|
||||
|
||||
@@ -122,6 +144,9 @@
|
||||
<Icon icon={InfoCircle} size={4} />
|
||||
Unavailable
|
||||
{/if}
|
||||
{#if walletStatus === "unavailable"}
|
||||
<Button class="btn btn-ghost btn-xs" onclick={retryStatus}>Retry</Button>
|
||||
{/if}
|
||||
</div>
|
||||
{:else}
|
||||
<Button class="btn btn-primary btn-sm" onclick={connect}>
|
||||
|
||||
Reference in New Issue
Block a user
A nice little helper for this pattern is the
callfunction. It's just a little prettier.