-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLoginButton.svelte
More file actions
114 lines (102 loc) · 3.46 KB
/
Copy pathLoginButton.svelte
File metadata and controls
114 lines (102 loc) · 3.46 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
<script lang="ts" module>
import type {IAuthService, IServerStatus} from '$lib/dotnet-types';
import {type Readable, writable, type Writable} from 'svelte/store';
import {t} from 'svelte-i18n-lingui';
let shouldUseSystemWebViewStore: Writable<boolean> | undefined = undefined;
function useSystemWebView(authService: IAuthService): Readable<boolean> {
if (shouldUseSystemWebViewStore) return shouldUseSystemWebViewStore;
shouldUseSystemWebViewStore = writable(true);
void authService.useSystemWebView().then(r => shouldUseSystemWebViewStore!.set(r));
return shouldUseSystemWebViewStore;
}
</script>
<script lang="ts">
import * as ResponsiveMenu from '$lib/components/responsive-menu';
import type {ILexboxServer} from '$lib/dotnet-types';
import {LoginResult} from '$lib/dotnet-types';
import {useAuthService} from '$lib/services/service-provider';
import {Button} from '$lib/components/ui/button';
import {AppNotification} from '$lib/notifications/notifications';
import {openUrl} from '$lib/services/url-opener';
const authService = useAuthService();
const shouldUseSystemWebView = useSystemWebView(authService);
interface Props {
status: Omit<IServerStatus, 'displayName'>;
text?: string | undefined;
statusChange?: (status: 'logged-in' | 'logged-out') => void;
}
let {
status,
text = undefined,
statusChange = () =>{ }
}: Props = $props();
let server = $derived(status.server);
let loading = $state(false);
async function login(server: ILexboxServer) {
loading = true;
try {
const result = await authService.signInWebView(server);
if (result === LoginResult.Success) {
statusChange('logged-in');
} else if (result === LoginResult.Offline) {
AppNotification.displayAction(
$t`You appear to be offline. Can you connect to ${server.displayName}?`,
{
label: $t`Open in browser`,
callback: () => {
void openUrl(server.authority);
return {dismiss: true};
},
},
{type: 'warning'},
);
} else {
AppNotification.display($t`Login cancelled.`, {type: 'warning', timeout: 'short'});
}
} finally {
loading = false;
}
}
async function logout(server: ILexboxServer) {
loading = true;
try {
await authService.logout(server);
statusChange('logged-out');
} finally {
loading = false;
}
}
</script>
{#if status.loggedIn}
<ResponsiveMenu.Root>
<ResponsiveMenu.Trigger>
{#snippet child({ props })}
<Button {...props} {loading} icon="i-mdi-account-circle">
{status.loggedInAs}
</Button>
{/snippet}
</ResponsiveMenu.Trigger>
<ResponsiveMenu.Content>
<ResponsiveMenu.Item icon="i-mdi-logout" onSelect={() => logout(server)}>
{$t`Logout`}
</ResponsiveMenu.Item>
</ResponsiveMenu.Content>
</ResponsiveMenu.Root>
{:else}
{#if $shouldUseSystemWebView}
<Button {loading}
variant="secondary"
onclick={() => login(server)}
icon="i-mdi-login">
{text ?? $t`Login to see projects`}
</Button>
{:else}
<Button {loading}
variant="secondary"
href="/api/auth/login/{server.id}"
external
icon="i-mdi-login">
{text ?? $t`Login to see projects`}
</Button>
{/if}
{/if}