use dioxus::prelude::*;
const REPLY_TRUNCATE_THRESHOLD: usize = 200;
#[component]
pub fn ReplyPreviewComponent(sender_name: String, body: String) -> Element {
let mut expanded = use_signal(|| false);
let is_long = body.len() > REPLY_TRUNCATE_THRESHOLD;
let is_expanded = expanded();
let display_body = if is_long && !is_expanded {
let truncated: String = body.chars().take(REPLY_TRUNCATE_THRESHOLD).collect();
let mut result = truncated;
result.push_str("...");
result
} else {
body.clone()
};
let body_class = if is_long && !is_expanded {
"reply-preview__body reply-preview__body--truncated"
} else {
"reply-preview__body reply-preview__body--expanded"
};
let toggle_label = if is_expanded {
"Show less"
} else {
"Show more"
};
rsx! {
div {
class: "reply-preview",
div { class: "reply-preview__bar" }
div {
class: "reply-preview__content",
span {
class: "reply-preview__sender",
"{sender_name}"
}
span {
class: "{body_class}",
"{display_body}"
}
if is_long {
button {
class: "reply-preview__toggle",
onclick: move |evt| {
evt.stop_propagation();
let current = expanded();
expanded.set(!current);
},
"{toggle_label}"
}
}
}
}
}
}