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

79 lines
3.3 KiB
Rust

use std::collections::HashMap;
use maud::{html, Markup, PreEscaped};
use peach_lib::sbot::SbotStatus;
use crate::templates;
/// 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 profile_link = format!("/scuttlebutt/profile?public_key={}", peer["id"]);
li {
a class="list-item link" href=(profile_link) {
@if peer.get("blob_path").is_some() && peer["blob_exists"] == "true" {
@let blob_path = format!("/blob/{}", peer["blob_path"]);
img id="peerImage" class="icon list-icon" src=(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) };
@let name_title = format!("{}'s public key", name);
label class="label-small label-ellipsis list-label font-gray" for="peerName" title=(name_title) {
(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 { "No follows 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"));
// render the base template with the provided body
templates::base::build_template(body)
}