|
1 | 1 | <script lang="ts"> |
2 | | - import { onMount, type Snippet } from 'svelte'; |
| 2 | + import { type Snippet } from 'svelte'; |
3 | 3 | import { t } from 'i18next'; |
| 4 | + import * as Sentry from '@sentry/sveltekit'; |
4 | 5 | import { navigate, Destination } from '$lib/destinations'; |
5 | 6 | import { DisplayableError } from '$lib/events'; |
| 7 | + import { getUsersClient } from '$lib/openapi'; |
6 | 8 | import SavedValue from '$lib/components/saved-value.svelte'; |
7 | 9 | import EventListener from '$lib/components/event-listener.svelte'; |
8 | 10 | import CurrentDestination from '$lib/components/current-destination.svelte'; |
|
17 | 19 | let { children }: Props = $props(); |
18 | 20 |
|
19 | 21 | let token = $state<string>(); |
| 22 | + let currentUserId = $state<string>(); |
20 | 23 | let currentDestination = $state(Destination.Feed); |
21 | 24 | let errors = $state<DisplayableError[]>([]); |
22 | 25 | let currentError = $state<DisplayableError>(); |
23 | 26 |
|
| 27 | + $effect(() => { |
| 28 | + fetchCurrentUser(token); |
| 29 | + }); |
| 30 | +
|
24 | 31 | $effect(() => { |
25 | 32 | if (!token && currentDestination.requiresAuthentication) { |
26 | 33 | navigate(Destination.Settings); |
|
39 | 46 | currentError = errors[0]; |
40 | 47 | } |
41 | 48 | } |
| 49 | +
|
| 50 | + async function fetchCurrentUser(token?: string) { |
| 51 | + if (token) { |
| 52 | + const client = await getUsersClient(); |
| 53 | + let currentUser = await client.getCurrentUser(); |
| 54 | + currentUserId = currentUser.id; |
| 55 | + Sentry.setUser({ id: currentUserId, username: currentUser.username }); |
| 56 | + } else { |
| 57 | + currentUserId = ''; |
| 58 | + Sentry.setUser(null); |
| 59 | + } |
| 60 | + } |
42 | 61 | </script> |
43 | 62 |
|
44 | 63 | <SavedValue name="connection.token" bind:value={token} /> |
| 64 | +<SavedValue name="currentUser.id" bind:value={currentUserId} /> |
45 | 65 | <EventListener type={DisplayableError} listener={(event) => addError(event.detail)} /> |
46 | 66 | <CurrentDestination bind:destination={currentDestination} /> |
47 | 67 |
|
|
0 commit comments