aboutsummaryrefslogtreecommitdiff
path: root/server/src/routes/ui/layout.rs
blob: 1c472478ac3a11c7e19fba7602dde3fe098305a8 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2023 metamuffin <metamuffin.org>
*/
use crate::{
    routes::ui::{
        account::{
            rocket_uri_macro_r_account_login, rocket_uri_macro_r_account_logout,
            rocket_uri_macro_r_account_register, session::Session,
            settings::rocket_uri_macro_r_account_settings,
        },
        admin::rocket_uri_macro_r_admin_dashboard,
        browser::rocket_uri_macro_r_all_items,
        node::rocket_uri_macro_r_library_node,
    },
    uri,
};
use futures::executor::block_on;
use jellybase::CONF;
use markup::{DynRender, Render};
use rocket::{
    http::ContentType,
    response::{self, Responder},
    Request, Response,
};
use std::io::Cursor;

markup::define! {
    Layout<'a, Main: Render>(title: String, main: Main, class: Option<&'a str>, session: Option<Session>, show_back: bool) {
        @markup::doctype()
        html {
            head {
                title { @title " - " @CONF.brand }
                link[rel="stylesheet", href="/assets/style.css"];
                script[src="/assets/bundle.js"] {}
            }
            body[class=class.unwrap_or("")] {
                nav {
                    @if *show_back { a[href="javascript:history.back()"] { "<- Back" } } " "
                    h1 { a[href="/"] { @CONF.brand } } " "
                    @if let Some(_) = session {
                        a[href=uri!(r_library_node("library"))] { "My Library" } " "
                        a[href=uri!(r_all_items())] { "All Items" } " "
                    }
                    div.account {
                        @if let Some(session) = session {
                            span { "Logged in as " } span.username { @session.user.display_name } " "
                            @if session.user.admin {
                                a[href=uri!(r_admin_dashboard())] { "Administration" } " "
                            }
                            a[href=uri!(r_account_settings())] { "Settings" } " "
                            a[href=uri!(r_account_logout())] { "Log out" }
                        } else {
                            a[href=uri!(r_account_register())] { "Register" } " "
                            a[href=uri!(r_account_login())] { "Log in" }
                        }
                    }
                }
                #main { @main }
                footer {
                    p { @CONF.brand " - " @CONF.slogan " | powered by Jellything" }
                }
            }
        }
    }

    FlashDisplay(flash: Option<Result<String, String>>) {
        @if let Some(flash) = &flash {
            @match flash {
                Ok(mesg) => { section.message { p.success { @mesg } } }
                Err(err) => { section.message { p.error { @format!("{err}") } } }
            }
        }
    }
}

pub type DynLayoutPage<'a> = LayoutPage<markup::DynRender<'a>>;

pub struct LayoutPage<T> {
    pub title: String,
    pub class: Option<&'static str>,
    pub show_back: bool,
    pub content: T,
}

impl Default for LayoutPage<DynRender<'_>> {
    fn default() -> Self {
        Self {
            class: None,
            content: markup::new!(),
            title: String::new(),
            show_back: false,
        }
    }
}

impl<'r, Main: Render> Responder<'r, 'static> for LayoutPage<Main> {
    fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
        // TODO blocking the event loop here. it seems like there is no other way to
        // TODO offload this, since the guard references `req` which has a lifetime.
        // TODO therefore we just block. that is fine since the database is somewhat fast.
        let session = block_on(req.guard::<Option<Session>>()).unwrap();
        let mut out = String::new();
        Layout {
            show_back: self.show_back,
            main: self.content,
            title: self.title,
            class: self.class.as_deref(),
            session,
        }
        .render(&mut out)
        .unwrap();

        Response::build()
            .header(ContentType::HTML)
            .streamed_body(Cursor::new(out))
            .ok()
    }
}