use crate::channel::entries::{ENTRIES, WIRE};
use crate::ui::{Chunk, Held, Model, theme};
pub const ABOUT: &str = "Every server this box reaches. Each is a set of files on this box: \
a key, a certificate, the server's anchors and its address.";
pub const REMOVE: &str = "remove";
pub const REMOVE_HELP: &str = "arms removing this connection; nothing is deleted until confirmed";
pub const WARNING: &str = "Deletes this box's key and certificate for this server. The server \
still lists this box until an operator there removes it, and joining again takes a new \
offer.";
pub const KEEP: &str = "keep it";
pub const KEEP_HELP: &str = "leaves this connection as it is";
pub const CONFIRM: &str = "remove it";
pub const CONFIRM_HELP: &str = "deletes this connection's files from this box";
pub const OWN: &str =
"This box's own server: its files are the box's own, not an entry, and are not removed here.";
pub const NO_ADDRESS: &str = "dials nothing: this box holds no address for it";
pub fn remove(name: &str) -> String {
format!("{REMOVE} {name}")
}
pub fn facts(chunk: &Chunk, roster: &[Chunk]) -> Vec<String> {
let channel = &chunk.channel;
let mut said = Vec::new();
if let Some(there) = &channel.named_there {
said.push(format!("named {there:?} on its host"));
}
said.push(match &channel.dials {
Some(at) => format!("dials {at}"),
None => NO_ADDRESS.to_owned(),
});
let twins: Vec<&str> = roster
.iter()
.map(|other| &other.channel)
.filter(|other| other.name != channel.name && other.dials == channel.dials)
.filter(|_| channel.dials.is_some())
.map(|other| other.name.as_str())
.collect();
if !twins.is_empty() {
said.push(format!("the same address as {}", twins.join(", ")));
}
said.push(match &channel.named_there {
Some(_) => format!(
"files: {WIRE}/{ENTRIES}/{} under this box's data root",
channel.name
),
None => format!("files: {WIRE} under this box's data root"),
});
said.push(match &chunk.held {
Held::Unheard => crate::ui::roster::NOT_ANSWERED.to_owned(),
Held::Heard => {
let names: Vec<&str> = chunk
.workspaces
.iter()
.map(|row| row.workspace.as_str())
.collect();
format!(
"answered with {} workspace(s): {}",
names.len(),
names.join(", ")
)
}
held @ (Held::Unheld(_) | Held::Refused(_)) => held.said().unwrap_or_default(),
});
said
}
pub fn render(ui: &mut egui::Ui, model: &mut Model) {
ui.add(egui::Label::new(egui::RichText::new(ABOUT).color(theme::INK_WEAK)).wrap());
if let Some(why) = &model.servers.forgetting.refused {
ui.add(egui::Label::new(egui::RichText::new(why).color(theme::NOTICE)).wrap());
}
let roster = model.roster.clone();
for chunk in &roster {
ui.add_space(theme::space::S);
ui.label(
egui::RichText::new(&chunk.channel.name)
.strong()
.color(theme::INK),
);
for fact in facts(chunk, &roster) {
ui.add(egui::Label::new(egui::RichText::new(fact).color(theme::INK_WEAK)).wrap());
}
if chunk.channel.named_there.is_some() {
controls(ui, model, &chunk.channel.name);
} else {
ui.add(egui::Label::new(egui::RichText::new(OWN).color(theme::INK_FAINT)).wrap());
}
}
}
fn controls(ui: &mut egui::Ui, model: &mut Model, name: &str) {
if model.servers.forgetting.armed.as_deref() != Some(name) {
if theme::paint::button(ui, &remove(name), true, REMOVE_HELP).clicked() {
model.arm_forget(name);
}
return;
}
ui.add(egui::Label::new(egui::RichText::new(WARNING).color(theme::NOTICE)).wrap());
ui.horizontal(|ui| {
if theme::paint::button(ui, KEEP, true, KEEP_HELP).clicked() {
model.disarm_forget();
}
if theme::paint::button(ui, CONFIRM, true, CONFIRM_HELP).clicked() {
model.forget();
}
});
}
#[cfg(test)]
mod tests;