-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathIdentitySwitcher.svelte
More file actions
303 lines (286 loc) · 9 KB
/
IdentitySwitcher.svelte
File metadata and controls
303 lines (286 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<script lang="ts">
import Avatar from "$lib/components/ui/Avatar.svelte";
import {
UserIcon,
XIcon,
LogOutIcon,
ArrowRightIcon,
PlusIcon,
} from "@lucide/svelte";
import type { HTMLAttributes } from "svelte/elements";
import type { LastUsedIdentity } from "$lib/stores/last-used-identities.store";
import { formatRelative, t } from "$lib/stores/locale.store";
import ProgressRing from "$lib/components/ui/ProgressRing.svelte";
import { getMetadataString, openIdLogo } from "$lib/utils/openID";
import PasskeyIcon from "../icons/PasskeyIcon.svelte";
type Props = HTMLAttributes<HTMLElement> & {
selected: bigint;
identities: LastUsedIdentity[];
onSwitchIdentity: (identityNumber: bigint) => Promise<void>;
onUseAnotherIdentity: () => void;
onManageIdentity?: () => Promise<void>;
onError: (error: unknown) => void;
onClose: () => void;
onSignOut?: () => Promise<void>;
};
const {
selected,
identities,
onSwitchIdentity,
onUseAnotherIdentity,
onManageIdentity,
onError,
onClose,
onSignOut,
}: Props = $props();
// Snapshot identities on render to prevent UI changes while popover is open.
// Popover shows up-to-date info when opened, but stays stable during interaction.
let initialIdentities = $state(identities);
let initialSelected = $state(selected);
let switchingToIdentity = $state<bigint>();
let isNavigatingToManage = $state(false);
let isSigningOut = $state(false);
let windowHeight = $state(window.innerHeight);
const selectedIdentity = $derived(
initialIdentities.find(
(identity) => identity.identityNumber === initialSelected,
)!,
);
const selectedLogo = $derived(
selectedIdentity !== undefined &&
"openid" in selectedIdentity.authMethod &&
selectedIdentity.authMethod.openid.metadata !== undefined
? openIdLogo(
selectedIdentity.authMethod.openid.iss,
selectedIdentity.authMethod.openid.metadata,
)
: undefined,
);
const otherIdentities = $derived(
initialIdentities.filter(
(identity) => identity.identityNumber !== initialSelected,
),
);
const passkeyNameCounts = $derived.by(() => {
const counts = new Map<string | undefined, number>();
for (const identity of otherIdentities) {
if (!("passkey" in identity.authMethod)) {
continue;
}
counts.set(identity.name, (counts.get(identity.name) ?? 0) + 1);
}
return counts;
});
const handleSwitchIdentity = async (identityNumber: bigint) => {
try {
switchingToIdentity = identityNumber;
await onSwitchIdentity(identityNumber);
} catch (error) {
onError(error);
} finally {
switchingToIdentity = undefined;
}
};
const handleManageIdentity = async () => {
try {
isNavigatingToManage = true;
await onManageIdentity?.();
} catch (error) {
onError(error);
} finally {
isNavigatingToManage = false;
}
};
const handleSignOut = async () => {
try {
isSigningOut = true;
await onSignOut?.();
} catch (error) {
onError(error);
} finally {
isSigningOut = false;
}
};
</script>
{#snippet authMethodBadge(logo: string | undefined, size: "sm" | "lg")}
<span
class={[
"bg-bg-primary_alt border-border-secondary absolute flex items-center justify-center rounded-full border",
size === "lg"
? "-end-1 -bottom-1 size-6.5"
: "-end-1.25 -bottom-1.25 size-5",
]}
>
{#if logo !== undefined}
<span
class={["text-fg-tertiary", size === "lg" ? "size-4.25" : "size-3.25"]}
>
{@html logo}
</span>
{:else}
<PasskeyIcon
class={["text-fg-tertiary", size === "lg" ? "!size-4.25" : "!size-3"]}
/>
{/if}
</span>
{/snippet}
{#snippet selectedIdentityCard()}
<div
class="bg-bg-secondary border-border-secondary relative mx-[-1px] my-[-1px] flex flex-col items-center rounded-b-2xl border-x border-b p-8"
>
<div class="relative mb-2">
<Avatar size="lg">
<UserIcon class="size-6" />
</Avatar>
{@render authMethodBadge(selectedLogo, "lg")}
</div>
<p
class="text-text-primary max-w-full overflow-hidden text-sm font-semibold text-ellipsis whitespace-nowrap"
>
{selectedIdentity.name ?? selectedIdentity.identityNumber}
</p>
<p
class="text-text-tertiary mb-6 max-w-full overflow-hidden text-sm text-ellipsis whitespace-nowrap"
>
{#if "openid" in selectedIdentity.authMethod && selectedIdentity.authMethod.openid.metadata !== undefined}
<span
>{getMetadataString(
selectedIdentity.authMethod.openid.metadata,
"email",
) ?? $t`Hidden email`}</span
>
{/if}
</p>
{#if onSignOut !== undefined}
<button onclick={handleSignOut} class="btn btn-secondary w-full">
<LogOutIcon class="size-4" />
{$t`Sign out`}
</button>
{/if}
{#if onManageIdentity !== undefined}
<button
onclick={handleManageIdentity}
class="btn btn-secondary group w-full gap-2.5"
>
{#if isNavigatingToManage}
<ProgressRing class="size-4" />
{/if}
{$t`Manage your Internet Identity`}
</button>
{/if}
<button
onclick={onClose}
class="btn btn-tertiary btn-sm btn-icon absolute end-2 top-2 !rounded-full"
aria-label={$t`Close`}
>
<XIcon class="size-5" />
</button>
</div>
{/snippet}
{#snippet identityListItem(identity: LastUsedIdentity)}
{@const logo =
"openid" in identity.authMethod &&
identity.authMethod.openid.metadata !== undefined
? openIdLogo(
identity.authMethod.openid.iss,
identity.authMethod.openid.metadata,
)
: undefined}
{@const notUnique =
"passkey" in identity.authMethod &&
(passkeyNameCounts.get(identity.name) ?? 0) > 1}
<li class="mx-4">
<button
onclick={() => handleSwitchIdentity(identity.identityNumber)}
class={[
"group flex w-full flex-row items-center gap-3 p-3 text-start",
"border-border-secondary rounded-md border outline-none",
"enabled:hover:bg-bg-primary_hover enabled:focus-visible:bg-bg-primary_hover",
"disabled:border-border-disabled",
]}
>
<span class="relative">
<Avatar size="sm">
<UserIcon class="size-5" />
</Avatar>
{@render authMethodBadge(logo, "sm")}
</span>
<span class="flex flex-col overflow-hidden group-disabled:opacity-50">
<span class="text-text-primary text-sm font-semibold">
{identity.name ?? identity.identityNumber}
</span>
<span
class="text-text-tertiary overflow-hidden text-sm text-ellipsis whitespace-nowrap"
>
{#if "openid" in identity.authMethod && identity.authMethod.openid.metadata !== undefined}
<span
>{getMetadataString(
identity.authMethod.openid.metadata,
"email",
) ?? $t`Hidden email`}</span
>
{:else}
<span>
{$t`Passkey`}
{#if notUnique && identity.createdAtMillis !== undefined}
{" | "}
{$t`Created ${$formatRelative(
new Date(identity.createdAtMillis),
{
style: "long",
},
)}`}
{/if}
</span>
{/if}
</span>
</span>
{#if switchingToIdentity === identity.identityNumber}
<ProgressRing class="text-fg-disabled ms-auto size-5" />
{:else}
<ArrowRightIcon
class={[
"text-fg-tertiary ms-auto me-1 size-5 transform opacity-0 transition-all duration-200 rtl:-scale-x-100",
"group-enabled:group-hover:me-0 group-enabled:group-hover:opacity-100",
"group-enabled:group-focus-visible:me-0 group-enabled:group-focus-visible:opacity-100",
]}
/>
{/if}
</button>
</li>
{/snippet}
{#snippet otherIdentitiesList()}
<div>
<h2 class="text-text-primary mx-4 mt-6 mb-4 text-sm font-semibold">
{$t`Sign in with another identity`}
</h2>
<ul
class="flex flex-col gap-2 overflow-y-auto"
style={`max-height: ${Math.max(2, Math.floor((windowHeight - 380) / 74)) * 74 - 41}px`}
>
{#each otherIdentities as identity}
{@render identityListItem(identity)}
{/each}
</ul>
</div>
{/snippet}
<svelte:window bind:innerHeight={windowHeight} />
<fieldset
disabled={switchingToIdentity !== undefined ||
isNavigatingToManage ||
isSigningOut}
class="contents"
>
<div class="flex flex-col overflow-x-hidden">
{#if selectedIdentity !== undefined}
{@render selectedIdentityCard()}
{/if}
{#if otherIdentities.length > 0}
{@render otherIdentitiesList()}
{/if}
<button onclick={onUseAnotherIdentity} class="btn btn-tertiary m-4">
<PlusIcon class="size-4" />
{$t`Add another identity`}
</button>
</div>
</fieldset>