peach-workspace/peach-web/src/templates/peers_list.rs

80 lines
3.4 KiB
Rust

use std::collections::HashMap;
use maud::{html, Markup, PreEscaped};
use peach_lib::sbot::SbotStatus;
use crate::{templates, utils::theme};
/// Render an unordered list of peers with one list element for each peer.
fn peers_template(peers: Vec<HashMap<String, String>>) -> Markup {
html! {
ul class="center list" {
@for peer in peers {
@let (name, name_alt) = match peer.get("name") {
Some(name) => (
name.to_owned(),
format!("{}'s profile image", name)
),
None => (
// set a fall-back value for name in case the data is unavailable
"Name unavailable".to_string(),
"Profile image".to_string()
)
};
@let url_safe_peer_id = peer["id"].replace('/', "%2F");
li {
a class="list-item link" href={ "/scuttlebutt/profile/" (url_safe_peer_id) } {
@if peer.get("blob_path").is_some() && peer["blob_exists"] == "true" {
img id="peerImage" class="icon list-icon" src={ "/blob/" (peer["blob_path"]) } alt=(name_alt);
} @else {
// use a placeholder image if we don't have the blob
img id="peerImage" class="icon icon-active list-icon" src="/icons/user.svg" alt="Placeholder profile image";
}
p id="peerName" class="font-normal list-text " { (name) };
label class="label-small label-ellipsis list-label font-gray" for="peerName" title={ (name) "'s public key" } {
(peer["id"])
}
}
}
}
}
}
}
/// Scuttlebutt peers list template builder.
///
/// A list of peers. Currently used to render lists of friends, follows and
/// blocks. The title of the page is set according to the provided parameter.
pub fn build_template(peers: Vec<HashMap<String, String>>, title: &str) -> PreEscaped<String> {
let peer_list_template = match SbotStatus::read() {
// only render the complete peers list if the sbot is active
Ok(status) if status.state == Some("active".to_string()) => {
html! {
div class="card center" {
@if !peers.is_empty() {
// render the peers list template
(peers_template(peers))
} @else {
p class="center-text font-normal" { "None found" }
}
}
}
}
_ => {
// the sbot is not active; render a message instead of the menu
templates::inactive::build_template("Social lists and interactions are unavailable.")
}
};
// wrap the nav bars around the settings menu template content
// parameters are template, title and back url
let body =
templates::nav::build_template(peer_list_template, title, Some("/scuttlebutt/peers"));
// 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)
}