web: handle expand/collapse of messages separate from unread status

This commit is contained in:
2024-02-20 19:58:50 -08:00
parent fe980c5468
commit 5923547159
5 changed files with 95 additions and 47 deletions

View File

@@ -7,10 +7,7 @@ use chrono::{DateTime, Datelike, Duration, Local, Utc};
use itertools::Itertools;
use log::error;
use seed::{prelude::*, *};
use seed_hooks::{
state_access::{CloneState, StateAccess},
topo, use_state,
};
use seed_hooks::{state_access::CloneState, topo, use_state};
use wasm_timer::Instant;
use crate::{
@@ -333,8 +330,9 @@ fn has_unread(tags: &[String]) -> bool {
tags.contains(&String::from("unread"))
}
fn read_message_render(msg: &ShowThreadQueryThreadMessages, open: StateAccess<bool>) -> Node<Msg> {
fn read_message_render(msg: &ShowThreadQueryThreadMessages, open: bool) -> Node<Msg> {
let id = msg.id.clone();
let expand_id = msg.id.clone();
let is_unread = has_unread(&msg.tags);
div![
C!["message"],
@@ -365,16 +363,18 @@ fn read_message_render(msg: &ShowThreadQueryThreadMessages, open: StateAccess<bo
// TODO(wathiede): add first line of message body
],
ev(Ev::Click, move |e| {
open.set(!open.get());
e.stop_propagation();
if open {
Msg::MessageCollapse(expand_id)
} else {
Msg::MessageExpand(expand_id)
}
}),
]
}
fn unread_message_render(
msg: &ShowThreadQueryThreadMessages,
open: StateAccess<bool>,
) -> Node<Msg> {
fn unread_message_render(msg: &ShowThreadQueryThreadMessages, open: bool) -> Node<Msg> {
let id = msg.id.clone();
let expand_id = msg.id.clone();
let is_unread = has_unread(&msg.tags);
div![
C!["message"],
@@ -408,8 +408,12 @@ fn unread_message_render(
IF!(!msg.cc.is_empty() => span!["CC: ", view_addresses(&msg.cc)])
],
ev(Ev::Click, move |e| {
open.set(!open.get());
e.stop_propagation();
if open {
Msg::MessageCollapse(expand_id)
} else {
Msg::MessageExpand(expand_id)
}
}),
],
div![
@@ -452,7 +456,7 @@ fn unread_message_render(
}
#[topo::nested]
fn thread(thread: &ShowThreadQueryThread) -> Node<Msg> {
fn thread(thread: &ShowThreadQueryThread, open_messages: &HashSet<String>) -> Node<Msg> {
// TODO(wathiede): show per-message subject if it changes significantly from top-level subject
set_title(&thread.subject);
let mut tags: Vec<_> = thread
@@ -467,40 +471,39 @@ fn thread(thread: &ShowThreadQueryThread) -> Node<Msg> {
tags.sort();
let messages = thread.messages.iter().map(|msg| {
let is_unread = has_unread(&msg.tags);
let open = use_state(|| is_unread);
//info!("open {} {}", open.get(), msg.id);
if open.get() {
let open = open_messages.contains(&msg.id);
if open {
unread_message_render(&msg, open)
} else {
read_message_render(&msg, open)
}
});
let any_unread = thread.messages.iter().any(|msg| has_unread(&msg.tags));
let thread_id = thread.thread_id.clone();
let read_thread_id = thread.thread_id.clone();
let unread_thread_id = thread.thread_id.clone();
div![
C!["thread"],
h1![
C!["title"],
span![
C!["read-status"],
i![
style! {
St::Color => "gold"
},
C![
if any_unread { "fa-regular" } else { "fa-solid" },
"fa-envelope"
],
ev(Ev::Click, move |_| Msg::SetUnread(
format!("thread:{}", thread_id),
!any_unread
)),
],
" ",
h3![C!["is-size-5"], &thread.subject,],
tags_chiclet(&tags, false),
span![
C!["level-item", "buttons"],
button![
C!["button"],
attrs! {At::Title => "Mark as read"},
span![C!["icon", "is-small"], i![C!["far", "fa-envelope-open"]]],
ev(Ev::Click, move |_| Msg::SetUnread(
format!("thread:{read_thread_id}"),
false
)),
],
button![
C!["button"],
attrs! {At::Title => "Mark as unread"},
span![C!["icon", "is-small"], i![C!["far", "fa-envelope"]]],
ev(Ev::Click, move |_| Msg::SetUnread(
format!("thread:{unread_thread_id}"),
true
)),
],
&thread.subject,
" ",
tags_chiclet(&tags, false)
],
messages,
/* TODO(wathiede): plumb in orignal id