|
1 | 1 | <script lang="ts"> |
2 | | - import { goto } from '$app/navigation' |
3 | | - import { PUBLIC_CDN_URL } from '$env/static/public' |
4 | | - import { GetUser } from '$lib/api/User' |
| 2 | + import { type User } from '$lib/api/User' |
5 | 3 | import ToastContainer from '$lib/components/toast/ToastContainer.svelte' |
| 4 | + import { dashboardView } from '$lib/stores/appControl' |
6 | 5 | import { isLoggedIn, loadedVideosCount, user } from '$lib/stores/AppVars' |
7 | | - import { toastStore } from '$lib/stores/ToastStore' |
8 | | - import { dashboardView, shouldRefetch } from '$lib/stores/appControl' |
9 | 6 | import { videos } from '$lib/stores/VideoStore' |
| 7 | + import { onMount } from 'svelte' |
10 | 8 | import '../app.css' |
11 | | - import { getCookie } from '$lib/utils/cookies' |
12 | 9 |
|
13 | | - const { children } = $props() |
14 | | - let isLoading = $state(true) |
| 10 | + const { children, data }: { children: any; data: User | null } = $props() |
15 | 11 |
|
16 | | - async function loadData() { |
17 | | - try { |
18 | | - if (getCookie('logged_in') !== '1') return |
| 12 | + if (data) { |
| 13 | + user.set(data) |
| 14 | + videos.set(data.videos) |
19 | 15 |
|
20 | | - const data = await GetUser(fetch) |
21 | | - if (data) { |
22 | | - const vids = data.videos || [] |
23 | | -
|
24 | | - for (let i = 0; i < vids.length; i++) { |
25 | | - const v = vids[i] |
26 | | -
|
27 | | - vids[i].thumbnail_url = `${PUBLIC_CDN_URL}/${v.file_key.split('.')[0]}.webp` |
28 | | - vids[i].video_url = `${PUBLIC_CDN_URL}/${v.file_key}${v.version > 1 ? `?v=${v.version}` : ''}` |
29 | | - } |
30 | | -
|
31 | | - user.set(data) |
32 | | - videos.set(vids) |
33 | | - isLoggedIn.set(true) |
34 | | - loadedVideosCount.set(data.videos?.length ?? 0) |
35 | | - dashboardView.set(localStorage.getItem('view') || 'list' as any) |
36 | | - } else { |
37 | | - toastStore.error({ |
38 | | - title: 'Session expired', |
39 | | - message: 'Please log in again', |
40 | | - duration: 10000 |
41 | | - }) |
42 | | - goto('/login') |
43 | | - } |
44 | | - } catch (err) { |
45 | | - console.error(err) |
46 | | - } finally { |
47 | | - isLoading = false |
48 | | - } |
| 16 | + isLoggedIn.set(true) |
| 17 | + loadedVideosCount.set(data.videos?.length ?? 0) |
49 | 18 | } |
50 | 19 |
|
51 | | - $effect(() => { |
52 | | - if ($shouldRefetch) { |
53 | | - loadData().finally(() => { |
54 | | - shouldRefetch.set(false) |
55 | | - }) |
56 | | - } |
| 20 | + onMount(() => { |
| 21 | + dashboardView.set(localStorage.getItem('view') || 'grid') |
57 | 22 | }) |
58 | 23 | </script> |
59 | 24 |
|
60 | | -{#if isLoading} |
61 | | - <div></div> |
62 | | -{:else} |
63 | | - {@render children?.()} |
64 | | -{/if} |
| 25 | +{@render children?.()} |
65 | 26 |
|
66 | 27 | <ToastContainer /> |
0 commit comments