aboutsummaryrefslogtreecommitdiff
path: root/ui/src/lib.rs
blob: c71e30f1bc9e3cb4be3dc778326d2ef76bbdc2e9 (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
/*
    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) 2025 metamuffin <metamuffin.org>
*/
pub mod account;
pub mod admin;
pub mod error;
pub mod filter_sort;
pub mod format;
pub mod home;
pub mod items;
pub mod locale;
pub mod node_card;
pub mod node_page;
pub mod props;
pub mod scaffold;
pub mod search;
pub mod stats;

use jellycommon::user::Theme;
use locale::Language;
use markup::DynRender;
use scaffold::{RenderInfo, Scaffold};
use serde::{Deserialize, Serialize};
use std::{
    path::PathBuf,
    sync::{LazyLock, Mutex},
};

#[rustfmt::skip]
#[derive(Debug, Deserialize, Serialize, Default)]
pub struct Config {
    brand: String,
    slogan: String,
    asset_path: PathBuf,
}

static CONF: LazyLock<Config> = LazyLock::new(|| {
    CONF_PRELOAD
        .lock()
        .unwrap()
        .take()
        .expect("cache config not preloaded. logic error")
});
pub static CONF_PRELOAD: Mutex<Option<Config>> = Mutex::new(None);

pub fn get_brand() -> String {
    CONF.brand.clone()
}
pub fn get_slogan() -> String {
    CONF.slogan.clone()
}

/// render as supertrait would be possible but is not
/// dyn compatible and I really dont want to expose generics
/// that generate rendering code because of compile speed.
pub trait Page {
    fn title(&self) -> String;
    fn to_render(&self) -> DynRender;
    fn class(&self) -> Option<&'static str> {
        None
    }
}

pub fn render_page(page: &dyn Page, renderinfo: RenderInfo) -> String {
    Scaffold {
        lang: renderinfo.lang,
        class: &format!(
            "{} theme-{}",
            page.class().unwrap_or("custom-page"),
            renderinfo
                .session
                .as_ref()
                .map(|s| s.user.theme)
                .unwrap_or(Theme::Dark)
                .to_str()
        ),
        renderinfo,
        title: page.title(),
        main: page.to_render(),
    }
    .to_string()
}

pub struct CustomPage {
    pub title: String,
    pub body: String,
}
impl Page for CustomPage {
    fn title(&self) -> String {
        self.title.clone()
    }
    fn to_render(&self) -> DynRender {
        markup::new!(@markup::raw(&self.body))
    }
}