summaryrefslogtreecommitdiff
path: root/ui/routes/(app)/+layout.svelte
blob: 9abaaf4cef447cb7791a2c24efcd56241969a1d3 (plain)
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
<script>
    import { page } from '$app/stores';
    import { goto } from '$app/navigation';
	import { onMount, onDestroy } from 'svelte';

	import { boot, subscribeToEvents } from '$lib/apiServer';
	import { currentUser, logins, channelsList, messages } from '$lib/store';

    import ChannelList from '$lib/components/ChannelList.svelte';
    import CreateChannelForm from '$lib/components/CreateChannelForm.svelte';
    import MessageInput from '$lib/components/MessageInput.svelte';

    let loading = true;
    let events = null;

    $: channel = $page?.params?.channel;

    function onBooted(boot) {
        currentUser.update(() => ({
            id: boot.login.id,
            username: boot.login.name,
        }));
        logins.update((value) => value.setLogins(boot.logins));
        channelsList.update((value) => value.setChannels(boot.channels));
        messages.update((value) => value.setMessages(boot.messages));
    }

	onMount(async () => {
        let response = await boot();
        switch (response.status) {
            case 200:
                onBooted(response.data);
                events = subscribeToEvents(response.data.resume_point);
                break;
            case 401:
                currentUser.update(() => null);
                goto('/login');
                break;
            case 503:
                currentUser.update(() => null);
                goto('/setup');
                break;
            default:
                // TODO: display error.
                break;
        }
        loading = false;
	});

    onDestroy(async () => {
        if (events !== null) {
            events.close();
        }
    });
</script>

{#if loading}
    <h2>Loading&hellip;</h2>
{:else}
    <div id="interface">
        <div class="channel-list">
            <ChannelList active={channel} />
        </div>
        <div class="active-channel">
            <slot />
        </div>
        <div class="create-channel">
            <CreateChannelForm />
        </div>
        <div class="create-message">
            <MessageInput {channel} />
        </div>
    </div>
{/if}

<style>
    #interface {
        height: 88vh;
        margin: 1rem;
        display: grid;
        grid-template-columns: 18rem auto;
        grid-template-rows: auto 2rem;
        grid-gap: 0.25rem;
    }
    #interface div {
        max-height: 100%;
        overflow: scroll;
    }
    #interface .active-channel {
        border: 1px solid grey;
        border-radius: 1.25rem;
    }
</style>