Compare commits
No commits in common. "b55409d4215c5e1be26c8c5b0ba04e4656d98c0c" and "2b36a7c669509390094d47b6c3a515e8b47409b1" have entirely different histories.
b55409d421
...
2b36a7c669
@ -1,14 +1,17 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
use crate::context::UserContext;
|
||||
use crate::models::comment::PublicComment;
|
||||
use crate::components::comments::section::CommentContext;
|
||||
use crate::components::comments::form::CommentForm;
|
||||
use crate::components::comments::actions::CommentActions;
|
||||
|
||||
#[component]
|
||||
pub fn CommentItem(comment: PublicComment, post_id: i32) -> Element {
|
||||
let ctx: CommentContext = use_context();
|
||||
let mut active_reply = ctx.active_reply;
|
||||
let refresh_trigger = ctx.refresh_trigger;
|
||||
let user_ctx: UserContext = use_context();
|
||||
|
||||
let depth = if comment.parent_id.is_none() && comment.depth > 0 {
|
||||
0
|
||||
@ -18,6 +21,7 @@ pub fn CommentItem(comment: PublicComment, post_id: i32) -> Element {
|
||||
|
||||
let indent = depth.min(6) * 24;
|
||||
|
||||
let is_admin = user_ctx.user.read().as_ref().is_some();
|
||||
let is_replying = active_reply() == Some(comment.id);
|
||||
let show_reply = depth < 20;
|
||||
|
||||
@ -86,6 +90,9 @@ pub fn CommentItem(comment: PublicComment, post_id: i32) -> Element {
|
||||
}
|
||||
}
|
||||
|
||||
if is_admin {
|
||||
CommentActions { comment_id: comment.id, post_id }
|
||||
}
|
||||
}
|
||||
|
||||
if is_replying {
|
||||
|
||||
@ -5,76 +5,34 @@ use crate::hooks::comment_storage::PendingComment;
|
||||
use crate::components::comments::item::CommentItem;
|
||||
use crate::components::comments::pending_item::PendingCommentItem;
|
||||
|
||||
#[derive(Clone)]
|
||||
enum MergedComment {
|
||||
Approved(PublicComment),
|
||||
Pending(PendingComment),
|
||||
}
|
||||
|
||||
fn merge_and_treeify(
|
||||
fn merge_comments(
|
||||
approved: Vec<PublicComment>,
|
||||
pending: Vec<PendingComment>,
|
||||
) -> Vec<MergedComment> {
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
let all: Vec<MergedComment> = approved
|
||||
let mut merged: Vec<MergedComment> = approved
|
||||
.into_iter()
|
||||
.map(MergedComment::Approved)
|
||||
.chain(pending.into_iter().map(MergedComment::Pending))
|
||||
.collect();
|
||||
|
||||
let all_ids: HashSet<i64> = all.iter().map(|c| match c {
|
||||
MergedComment::Approved(c) => c.id,
|
||||
MergedComment::Pending(c) => c.id,
|
||||
}).collect();
|
||||
|
||||
let mut children_map: HashMap<Option<i64>, Vec<MergedComment>> = HashMap::new();
|
||||
for comment in all {
|
||||
let parent_id = match &comment {
|
||||
MergedComment::Approved(c) => c.parent_id,
|
||||
MergedComment::Pending(c) => c.parent_id,
|
||||
merged.sort_by(|a, b| {
|
||||
let time_a = match a {
|
||||
MergedComment::Approved(c) => c.created_at_iso.as_str(),
|
||||
MergedComment::Pending(c) => c.created_at.as_str(),
|
||||
};
|
||||
let effective_parent = match parent_id {
|
||||
Some(pid) if !all_ids.contains(&pid) => None,
|
||||
_ => parent_id,
|
||||
let time_b = match b {
|
||||
MergedComment::Approved(c) => c.created_at_iso.as_str(),
|
||||
MergedComment::Pending(c) => c.created_at.as_str(),
|
||||
};
|
||||
children_map.entry(effective_parent).or_default().push(comment);
|
||||
}
|
||||
time_a.cmp(time_b)
|
||||
});
|
||||
|
||||
for children in children_map.values_mut() {
|
||||
children.sort_by(|a, b| {
|
||||
let time_a = match a {
|
||||
MergedComment::Approved(c) => c.created_at_iso.as_str(),
|
||||
MergedComment::Pending(c) => c.created_at.as_str(),
|
||||
};
|
||||
let time_b = match b {
|
||||
MergedComment::Approved(c) => c.created_at_iso.as_str(),
|
||||
MergedComment::Pending(c) => c.created_at.as_str(),
|
||||
};
|
||||
time_a.cmp(time_b)
|
||||
});
|
||||
}
|
||||
|
||||
fn dfs(
|
||||
parent_id: Option<i64>,
|
||||
children_map: &HashMap<Option<i64>, Vec<MergedComment>>,
|
||||
result: &mut Vec<MergedComment>,
|
||||
) {
|
||||
if let Some(children) = children_map.get(&parent_id) {
|
||||
for child in children {
|
||||
result.push(child.clone());
|
||||
let child_id = match child {
|
||||
MergedComment::Approved(c) => Some(c.id),
|
||||
MergedComment::Pending(c) => Some(c.id),
|
||||
};
|
||||
dfs(child_id, children_map, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut result = Vec::new();
|
||||
dfs(None, &children_map, &mut result);
|
||||
result
|
||||
merged
|
||||
}
|
||||
|
||||
#[component]
|
||||
@ -83,7 +41,7 @@ pub fn CommentList(
|
||||
pending: Vec<PendingComment>,
|
||||
post_id: i32,
|
||||
) -> Element {
|
||||
let merged = merge_and_treeify(comments, pending);
|
||||
let merged = merge_comments(comments, pending);
|
||||
|
||||
rsx! {
|
||||
div { class: "space-y-0 divide-y divide-gray-100 dark:divide-[#2a2a2a]",
|
||||
|
||||
@ -52,14 +52,14 @@ pub fn CommentSection(post_id: i32) -> Element {
|
||||
}
|
||||
});
|
||||
|
||||
let comments_resource = use_resource(move || async move {
|
||||
let _ = (ctx.refresh_trigger)();
|
||||
get_comments(post_id).await
|
||||
});
|
||||
let comments_resource = use_server_future(move || {
|
||||
let _ = ctx.refresh_trigger;
|
||||
get_comments(post_id)
|
||||
})?;
|
||||
|
||||
let data = comments_resource.read();
|
||||
|
||||
match &*data {
|
||||
match data.as_ref().map(|r| r.as_ref()) {
|
||||
Some(Ok(CommentTreeResponse { comments, count })) => {
|
||||
let approved_count = *count;
|
||||
let pending_count = ctx.pending_comments.read().len() as i64;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user