Skip to content

Commit 01c05a9

Browse files
authored
Improve sign in speed and loading feedback (#3441)
* WIP * Show loading indicator on enabling multiple accounts. * Show loading indicator on enabling multiple accounts.
1 parent 91734f8 commit 01c05a9

File tree

5 files changed

+66
-102
lines changed

5 files changed

+66
-102
lines changed

src/frontend/src/lib/globals.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
import { inferHost } from "$lib/utils/iiConnection";
1414
import { idlFactory as internet_identity_idl } from "$lib/generated/internet_identity_idl";
1515
import { features } from "$lib/legacy/features";
16-
import { LazyHttpAgent } from "$lib/utils/lazyHttpAgent";
1716

1817
export let canisterId: Principal;
1918
export let canisterConfig: InternetIdentityInit;
@@ -29,7 +28,10 @@ export const initGlobals = () => {
2928
shouldFetchRootKey:
3029
features.FETCH_ROOT_KEY || (canisterConfig.fetch_root_key[0] ?? false),
3130
};
32-
anonymousAgent = LazyHttpAgent.createLazy(agentOptions);
31+
anonymousAgent = HttpAgent.createSync(agentOptions);
32+
// Fetch subnet keys to speed up queries during authentication,
33+
// this avoids having to fetch them later on user interaction.
34+
void anonymousAgent.fetchSubnetKeys(canisterId);
3335
anonymousActor = Actor.createActor<_SERVICE>(internet_identity_idl, {
3436
agent: anonymousAgent,
3537
canisterId,

src/frontend/src/lib/stores/authentication.store.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
import { Principal } from "@icp-sdk/core/principal";
1111
import type { _SERVICE } from "$lib/generated/internet_identity_types";
1212
import { idlFactory as internet_identity_idl } from "$lib/generated/internet_identity_idl";
13-
import { LazyHttpAgent } from "$lib/utils/lazyHttpAgent";
1413
import { createAnonymousNonce } from "$lib/utils/openID";
1514

1615
export interface Authenticated {
@@ -40,7 +39,10 @@ const internalStore = writable<{
4039

4140
export const authenticationStore: AuthenticationStore = {
4241
init: ({ canisterId, agentOptions }) => {
43-
const agent = LazyHttpAgent.createLazy(agentOptions);
42+
const agent = HttpAgent.createSync(agentOptions);
43+
// Fetch subnet keys to speed up queries during authentication,
44+
// this avoids having to fetch them later on user interaction.
45+
void agent.fetchSubnetKeys(canisterId);
4446
const actor = Actor.createActor<_SERVICE>(internet_identity_idl, {
4547
agent,
4648
canisterId,

src/frontend/src/lib/stores/session.store.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { createAnonymousNonce } from "$lib/utils/openID";
1111
import type { _SERVICE } from "$lib/generated/internet_identity_types";
1212
import { Principal } from "@icp-sdk/core/principal";
1313
import { idlFactory as internet_identity_idl } from "$lib/generated/internet_identity_idl";
14-
import { LazyHttpAgent } from "$lib/utils/lazyHttpAgent";
1514
import { fromBase64, toBase64 } from "$lib/utils/utils";
1615
import { isNullish } from "@dfinity/utils";
1716

@@ -95,7 +94,10 @@ const read = async () => {
9594
export const sessionStore: SessionStore = {
9695
init: async ({ canisterId, agentOptions }) => {
9796
const { identity, nonce, salt } = (await read()) ?? (await create());
98-
const agent = LazyHttpAgent.createLazy({ ...agentOptions, identity });
97+
const agent = HttpAgent.createSync({ ...agentOptions, identity });
98+
// Fetch subnet keys to speed up queries during authentication,
99+
// this avoids having to fetch them later on user interaction.
100+
void agent.fetchSubnetKeys(canisterId);
99101
const actor = Actor.createActor<_SERVICE>(internet_identity_idl, {
100102
agent,
101103
canisterId,

src/frontend/src/lib/utils/lazyHttpAgent.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/frontend/src/routes/(new-styling)/authorize/(panel)/continue/+page.svelte

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
authenticationStore,
2424
isAuthenticatedStore,
2525
} from "$lib/stores/authentication.store";
26-
import { throwCanisterError } from "$lib/utils/utils";
26+
import { throwCanisterError, waitFor } from "$lib/utils/utils";
2727
import type {
2828
AccountInfo,
2929
AccountNumber,
@@ -32,6 +32,7 @@
3232
import { slide, fade, scale } from "svelte/transition";
3333
import Dialog from "$lib/components/ui/Dialog.svelte";
3434
import EditAccount from "$lib/components/views/EditAccount.svelte";
35+
import ProgressRing from "$lib/components/ui/ProgressRing.svelte";
3536
3637
const PRIMARY_ACCOUNT_NUMBER = undefined;
3738
const MAX_ACCOUNTS = 5;
@@ -75,9 +76,12 @@
7576
accounts?.map((account) => account.name[0] ?? primaryAccountName) ?? [],
7677
);
7778
const authLastUsedFlow = new AuthLastUsedFlow();
79+
const selectedIdentityNumber = $derived(
80+
$lastUsedIdentitiesStore.selected!.identityNumber,
81+
);
7882
// Initialize the flow every time the identity changes
7983
$effect(() => {
80-
authLastUsedFlow.init([$lastUsedIdentitiesStore.selected!.identityNumber]);
84+
authLastUsedFlow.init([selectedIdentityNumber]);
8185
});
8286
8387
const handleContinueDefault = async () => {
@@ -286,44 +290,42 @@
286290
</div>
287291
{/snippet}
288292

289-
{#snippet accountList()}
293+
{#snippet accountList(accounts: AccountInfo[])}
290294
<div class="col-start-1 row-start-1" out:fade={{ duration: 100 }}>
291295
<div class="!min-h-18" out:slide={{ axis: "y", duration: 300 }}>
292-
{#if nonNullish(accounts)}
293-
<div class="!min-h-18" in:slide={{ axis: "y", duration: 300 }}>
294-
<div class="flex flex-col gap-2 pb-6" in:fade={{ duration: 300 }}>
295-
<ul class="contents" aria-label={$t`Choose an account`}>
296-
{#each accounts as account (account.account_number[0])}
297-
<li class="contents">
298-
{@render accountListItem(account)}
299-
</li>
300-
{/each}
301-
</ul>
302-
<Tooltip
303-
label={$t`Limit reached`}
304-
description={$plural(MAX_ACCOUNTS, {
305-
one: `You have reached the maximum of # account for a single app.`,
306-
other: `You have reached the maximum of # accounts for a single app.`,
307-
})}
308-
direction="up"
309-
align="center"
310-
hidden={!isAccountLimitReached}
311-
>
312-
<div class="mt-3 shrink-0">
313-
<Button
314-
onclick={() => (isCreateAccountDialogVisible = true)}
315-
variant="tertiary"
316-
disabled={isAccountLimitReached}
317-
class="w-full"
318-
>
319-
<PlusIcon class="size-5" />
320-
{$t`Add another account`}
321-
</Button>
322-
</div>
323-
</Tooltip>
324-
</div>
296+
<div class="!min-h-18" in:slide={{ axis: "y", duration: 300 }}>
297+
<div class="flex flex-col gap-2 pb-6" in:fade={{ duration: 300 }}>
298+
<ul class="contents" aria-label={$t`Choose an account`}>
299+
{#each accounts as account (account.account_number[0])}
300+
<li class="contents">
301+
{@render accountListItem(account)}
302+
</li>
303+
{/each}
304+
</ul>
305+
<Tooltip
306+
label={$t`Limit reached`}
307+
description={$plural(MAX_ACCOUNTS, {
308+
one: `You have reached the maximum of # account for a single app.`,
309+
other: `You have reached the maximum of # accounts for a single app.`,
310+
})}
311+
direction="up"
312+
align="center"
313+
hidden={!isAccountLimitReached}
314+
>
315+
<div class="mt-3 shrink-0">
316+
<Button
317+
onclick={() => (isCreateAccountDialogVisible = true)}
318+
variant="tertiary"
319+
disabled={isAccountLimitReached}
320+
class="w-full"
321+
>
322+
<PlusIcon class="size-5" />
323+
{$t`Add another account`}
324+
</Button>
325+
</div>
326+
</Tooltip>
325327
</div>
326-
{/if}
328+
</div>
327329
</div>
328330
</div>
329331
{/snippet}
@@ -348,8 +350,21 @@
348350
{$t`with your Internet Identity`}
349351
</p>
350352
<div class="grid">
351-
{#if isMultipleAccountsEnabled}
352-
{@render accountList()}
353+
<!-- Nested if/else conditions breaks transitions, so they've been flattened here-->
354+
{#if isMultipleAccountsEnabled && $isAuthenticatedStore && nonNullish(accounts)}
355+
{@render accountList(accounts)}
356+
{:else if isMultipleAccountsEnabled && $isAuthenticatedStore}
357+
<!-- Display the progress ring if loading accounts takes longer than usual.
358+
359+
This may happen the first time a user enables multiple accounts,
360+
as authentication might require an update call in case OpenID is used. -->
361+
<div
362+
class="col-start-1 row-start-1 flex min-h-18 items-center justify-center pb-6"
363+
in:fade={{ duration: 100, delay: 300 }}
364+
out:fade={{ duration: 100 }}
365+
>
366+
<ProgressRing class="text-fg-tertiary size-8" />
367+
</div>
353368
{:else}
354369
{@render continueDefault()}
355370
{/if}

0 commit comments

Comments
 (0)