peach-workspace/peach-web/src/routes/settings/scuttlebutt/menu.rs

70 lines
3.0 KiB
Rust

use maud::{html, PreEscaped};
use peach_lib::sbot::SbotStatus;
use rouille::Request;
use crate::{
templates,
utils::{cookie::CookieRequest, flash::FlashRequest, theme},
};
/// Read the status of the go-sbot service and render buttons accordingly.
fn render_process_buttons() -> PreEscaped<String> {
// retrieve go-sbot systemd process status
let sbot_status = SbotStatus::read();
html! {
// render the stop and restart buttons if sbot process is currently active
@if let Ok(status) = sbot_status {
@if status.state == Some("active".to_string()) {
a id="stop" class="button button-primary center" href="/settings/scuttlebutt/stop" title="Stop Sbot" { "Stop Sbot" }
a id="restart" class="button button-primary center" href="/settings/scuttlebutt/restart" title="Restart Sbot" { "Restart Sbot" }
// render the start button if sbot process is currently inactive
} @else {
a id="start" class="button button-primary center" href="/settings/scuttlebutt/start" title="Start Sbot" { "Start Sbot" }
}
// render the start button if an error was returned by the status query
} @else {
a id="start" class="button button-primary center" href="/settings/scuttlebutt/start" title="Start Sbot" { "Start Sbot" }
}
}
}
// ROUTE: /settings/scuttlebutt
/// Scuttlebutt settings menu template builder.
pub fn build_template(request: &Request) -> PreEscaped<String> {
// check for flash cookies; will be (None, None) if no flash cookies are found
let (flash_name, flash_msg) = request.retrieve_flash();
let menu_template = html! {
(PreEscaped("<!-- SCUTTLEBUTT SETTINGS MENU -->"))
div class="card center" {
(PreEscaped("<!-- BUTTONS -->"))
div id="settingsButtons" {
a id="configureSbot" class="button button-primary center" href="/settings/scuttlebutt/configure" title="Configure Sbot" { "Configure Sbot" }
// conditionally render the start / stop / restart buttons
(render_process_buttons())
}
// render flash message if cookies were found in the request
@if let (Some(name), Some(msg)) = (flash_name, flash_msg) {
(PreEscaped("<!-- FLASH MESSAGE -->"))
(templates::flash::build_template(name, msg))
}
}
};
// retrieve the value of the "back_url" cookie
// if the cookie value is not found then set a hardcoded fallback value
let back_url = request.retrieve_cookie("back_url").or(Some("/settings"));
// wrap the nav bars around the settings menu template content
// parameters are template, title and back url
let body = templates::nav::build_template(menu_template, "Scuttlebutt Settings", back_url);
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body, theme)
}