campfire/src/routes/+layout.svelte

75 lines
1.8 KiB
Svelte
Raw Normal View History

2024-06-29 17:27:46 +01:00
<script>
2024-06-29 19:52:52 +01:00
import '$lib/app.css';
import * as api from '$lib/api.js';
import { server } from '$lib/client/server.js';
import { app } from '$lib/client/app.js';
import { account, logged_in } from '$lib/stores/account.js';
import { parseAccount } from '$lib/account.js';
import { unread_notif_count, last_read_notif_id } from '$lib/notifications.js';
import { get } from 'svelte/store';
2024-06-29 17:27:46 +01:00
import Navigation from '$lib/ui/Navigation.svelte';
import Widgets from '$lib/ui/Widgets.svelte';
async function init() {
if (!get(app) || !get(app).token) {
account.set(false);
logged_in.set(false);
return;
}
// logged in- attempt to retrieve using token
const data = await api.verifyCredentials(get(server).host, get(app).token);
if (!data) return;
account.set(parseAccount(data));
logged_in.set(true);
console.log(`Logged in as @${get(account).username}@${get(account).host}`);
2024-07-02 20:21:34 +01:00
// spin up async task to fetch notifications
const notif_data = await api.getNotifications(
get(server).host,
get(app).token,
get(last_read_notif_id)
);
if (!notif_data) return;
unread_notif_count.set(notif_data.length);
};
2024-06-29 17:27:46 +01:00
</script>
2024-06-30 17:37:19 +01:00
<div id="app">
2024-06-29 17:27:46 +01:00
<header>
<Navigation />
2024-06-29 17:27:46 +01:00
</header>
<main>
{#await init()}
<div class="loading throb">
<span>just a moment...</span>
</div>
{:then}
<slot></slot>
{/await}
2024-06-29 17:27:46 +01:00
</main>
<div id="widgets">
<Widgets />
</div>
</div>
<style>
.loading {
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-size: 2em;
font-weight: bold;
}
</style>