mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-16 11:50:42 +08:00
Don't display old balances
By storing the balance, we displayed stale data. This forces the balance to be reloaded on refresh.
This commit is contained in:
+7
-2
@@ -5,6 +5,7 @@
|
||||
import { error, Failure } from '@app/error';
|
||||
import { disconnectWallet } from "@app/session";
|
||||
import type { Session } from '@app/session';
|
||||
import Loading from '@app/Loading.svelte';
|
||||
import Logo from './Logo.svelte';
|
||||
import Connect from './Connect.svelte';
|
||||
import type { Config } from '@app/config';
|
||||
@@ -134,9 +135,13 @@
|
||||
<span class="network unavailable">No Network</span>
|
||||
{/if}
|
||||
|
||||
{#if address && tokenBalance}
|
||||
{#if address}
|
||||
<span class="balance">
|
||||
{formatBalance(tokenBalance)} <strong>RAD</strong>
|
||||
{#if tokenBalance}
|
||||
{formatBalance(tokenBalance)} <strong>RAD</strong>
|
||||
{:else}
|
||||
<Loading small />
|
||||
{/if}
|
||||
</span>
|
||||
|
||||
<button class="address outline small" bind:this={sessionButton}
|
||||
|
||||
+17
-6
@@ -24,7 +24,7 @@ export type State =
|
||||
|
||||
export interface Session {
|
||||
address: string;
|
||||
tokenBalance: BigNumber;
|
||||
tokenBalance: BigNumber | null; // `null` means it isn't loaded yet.
|
||||
tx: TxState;
|
||||
}
|
||||
|
||||
@@ -65,20 +65,25 @@ export const loadState = (initial: State): Store => {
|
||||
|
||||
try {
|
||||
const tokenBalance: BigNumber = await config.token.balanceOf(address);
|
||||
const session = { address, tokenBalance, tx: null };
|
||||
store.set({
|
||||
connection: Connection.Connected,
|
||||
session: { address, tokenBalance, tx: null }
|
||||
session,
|
||||
});
|
||||
window.localStorage.setItem("session", JSON.stringify({ address, tokenBalance, tx: null }));
|
||||
saveSession({ ...session, tokenBalance: null });
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
},
|
||||
|
||||
updateBalance: (n: BigNumber) => {
|
||||
store.update((s) => {
|
||||
store.update((s: State) => {
|
||||
assert(s.connection === Connection.Connected);
|
||||
s.session.tokenBalance = s.session.tokenBalance.add(n);
|
||||
if (s.session.tokenBalance) {
|
||||
// If the token balance is loaded, we can update it, otherwise
|
||||
// we let it finish loading.
|
||||
s.session.tokenBalance = s.session.tokenBalance.add(n);
|
||||
}
|
||||
return s;
|
||||
});
|
||||
},
|
||||
@@ -165,7 +170,7 @@ export const loadState = (initial: State): Store => {
|
||||
disconnectWallet();
|
||||
} else {
|
||||
s.session.address = address;
|
||||
window.localStorage.setItem("session", JSON.stringify({ ...s.session }));
|
||||
saveSession(s.session);
|
||||
}
|
||||
return s;
|
||||
default:
|
||||
@@ -218,3 +223,9 @@ export function disconnectWallet(): void {
|
||||
window.localStorage.removeItem("session");
|
||||
location.reload();
|
||||
}
|
||||
|
||||
function saveSession(session: Session): void {
|
||||
window.localStorage.setItem("session", JSON.stringify({
|
||||
...session, tokenBalance: null
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user