Something went wrong. Try again.
Streamplace AtmosphereConf 2026 VOD JAM
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128//! `CommentBody` — renders a single comment node with author, divider, text,//! timestamp, and recursively renders replies with threading.
use dioxus::prelude::*;use jacquard::types::string::Datetime;
use crate::PDT;use crate::comments::CommentNode;use crate::components::facet_text::FacetText;
/// Derive a short hex string from an AT URI for use in DOM `id` attributes.////// Uses `DefaultHasher` — output may vary across Rust versions, but this is/// fine since IDs are only used within a single render pass.fn uri_hash(uri: &str) -> String { use std::hash::{Hash, Hasher}; let mut hasher = std::collections::hash_map::DefaultHasher::new(); uri.hash(&mut hasher); format!("{:x}", hasher.finish())}
/// Format a `Datetime` as a short timestamp string for display (e.g. "Apr 12, 2026 3:42 PM").fn format_comment_timestamp(dt: &Datetime) -> String { let chrono_dt: &chrono::DateTime<chrono::FixedOffset> = dt.as_ref(); let local = chrono_dt.with_timezone(&PDT); local.format("%b %-d, %Y %-I:%M %p").to_string()}
/// Render a single `CommentNode`, including its nested replies.////// # Threading rules////// - **Linear chain (exactly one reply):** the reply is rendered at the same/// indentation level — no `div.comment-replies` wrapper, no extra indent./// - **Branching (two or more replies):** replies are wrapped in/// `div.comment-replies` which applies `margin-inline-start: 1.5rem`.////// `is_last_sibling` is stored as a `data-last-sibling` attribute so the SVG/// connector overlay (Task 3) can distinguish `└` corners from `├` tees.#[component]pub fn CommentBody(node: CommentNode, is_last_sibling: bool) -> Element { let hash = uri_hash(node.comment.uri.as_str()); let body_id = format!("comment-body-{hash}"); let rule_id = format!("comment-rule-{hash}");
let author = node.comment.author_handle.as_str().to_owned(); let text = node.comment.text.clone(); let facets = node.comment.facets.clone().unwrap_or_default(); let timestamp = format_comment_timestamp(&node.comment.created_at);
let reply_count = node.replies.len();
rsx! { div { id: "{body_id}", class: "comment-body", "data-last-sibling": if is_last_sibling { "true" } else { "false" },
div { class: "comment-author", "@{author}" span { class: "comment-timestamp", "{timestamp}" } } div { id: "{rule_id}", class: "comment-rule", } div { class: "comment-text", FacetText { text: text, facets: facets } }
// Render replies. if reply_count == 1 { // Linear chain — stay flat, no indent wrapper. if let Some(reply) = node.replies.into_iter().next() { CommentBody { node: reply, is_last_sibling: true, } } } else if reply_count > 1 { // Branching — indent with comment-replies wrapper. div { class: "comment-replies", for (i, reply) in node.replies.into_iter().enumerate() { CommentBody { node: reply, is_last_sibling: i + 1 == reply_count, } } } } } }}
#[cfg(test)]mod tests { use super::*;
#[test] fn uri_hash_deterministic() { let uri = "at://did:plc:abc/tv.ionosphere.comment/xyz"; assert_eq!(uri_hash(uri), uri_hash(uri)); }
#[test] fn uri_hash_different_uris_differ() { let a = uri_hash("at://did:plc:abc/tv.ionosphere.comment/aaa"); let b = uri_hash("at://did:plc:abc/tv.ionosphere.comment/bbb"); assert_ne!(a, b); }
#[test] fn format_timestamp_known_input() { let dt: Datetime = "2026-04-03T17:56:07.616Z".parse().unwrap(); let formatted = format_comment_timestamp(&dt); // PDT is UTC-7, so 17:56 UTC → 10:56 AM PDT. assert!(formatted.contains("10:56"), "expected 10:56 in '{formatted}'"); assert!(formatted.contains("AM"), "expected AM in '{formatted}'"); assert!(formatted.contains("2026"), "expected year in '{formatted}'"); }}