Compare commits

..

3 Commits

3 changed files with 62 additions and 27 deletions

View File

@ -1,17 +1,14 @@
use dioxus::prelude::*; use dioxus::prelude::*;
use crate::context::UserContext;
use crate::models::comment::PublicComment; use crate::models::comment::PublicComment;
use crate::components::comments::section::CommentContext; use crate::components::comments::section::CommentContext;
use crate::components::comments::form::CommentForm; use crate::components::comments::form::CommentForm;
use crate::components::comments::actions::CommentActions;
#[component] #[component]
pub fn CommentItem(comment: PublicComment, post_id: i32) -> Element { pub fn CommentItem(comment: PublicComment, post_id: i32) -> Element {
let ctx: CommentContext = use_context(); let ctx: CommentContext = use_context();
let mut active_reply = ctx.active_reply; let mut active_reply = ctx.active_reply;
let refresh_trigger = ctx.refresh_trigger; let refresh_trigger = ctx.refresh_trigger;
let user_ctx: UserContext = use_context();
let depth = if comment.parent_id.is_none() && comment.depth > 0 { let depth = if comment.parent_id.is_none() && comment.depth > 0 {
0 0
@ -21,7 +18,6 @@ pub fn CommentItem(comment: PublicComment, post_id: i32) -> Element {
let indent = depth.min(6) * 24; 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 is_replying = active_reply() == Some(comment.id);
let show_reply = depth < 20; let show_reply = depth < 20;
@ -90,9 +86,6 @@ pub fn CommentItem(comment: PublicComment, post_id: i32) -> Element {
} }
} }
if is_admin {
CommentActions { comment_id: comment.id, post_id }
}
} }
if is_replying { if is_replying {

View File

@ -5,22 +5,44 @@ use crate::hooks::comment_storage::PendingComment;
use crate::components::comments::item::CommentItem; use crate::components::comments::item::CommentItem;
use crate::components::comments::pending_item::PendingCommentItem; use crate::components::comments::pending_item::PendingCommentItem;
#[derive(Clone)]
enum MergedComment { enum MergedComment {
Approved(PublicComment), Approved(PublicComment),
Pending(PendingComment), Pending(PendingComment),
} }
fn merge_comments( fn merge_and_treeify(
approved: Vec<PublicComment>, approved: Vec<PublicComment>,
pending: Vec<PendingComment>, pending: Vec<PendingComment>,
) -> Vec<MergedComment> { ) -> Vec<MergedComment> {
let mut merged: Vec<MergedComment> = approved use std::collections::{HashMap, HashSet};
let all: Vec<MergedComment> = approved
.into_iter() .into_iter()
.map(MergedComment::Approved) .map(MergedComment::Approved)
.chain(pending.into_iter().map(MergedComment::Pending)) .chain(pending.into_iter().map(MergedComment::Pending))
.collect(); .collect();
merged.sort_by(|a, b| { 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,
};
let effective_parent = match parent_id {
Some(pid) if !all_ids.contains(&pid) => None,
_ => parent_id,
};
children_map.entry(effective_parent).or_default().push(comment);
}
for children in children_map.values_mut() {
children.sort_by(|a, b| {
let time_a = match a { let time_a = match a {
MergedComment::Approved(c) => c.created_at_iso.as_str(), MergedComment::Approved(c) => c.created_at_iso.as_str(),
MergedComment::Pending(c) => c.created_at.as_str(), MergedComment::Pending(c) => c.created_at.as_str(),
@ -31,8 +53,28 @@ fn merge_comments(
}; };
time_a.cmp(time_b) time_a.cmp(time_b)
}); });
}
merged 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
} }
#[component] #[component]
@ -41,7 +83,7 @@ pub fn CommentList(
pending: Vec<PendingComment>, pending: Vec<PendingComment>,
post_id: i32, post_id: i32,
) -> Element { ) -> Element {
let merged = merge_comments(comments, pending); let merged = merge_and_treeify(comments, pending);
rsx! { rsx! {
div { class: "space-y-0 divide-y divide-gray-100 dark:divide-[#2a2a2a]", div { class: "space-y-0 divide-y divide-gray-100 dark:divide-[#2a2a2a]",

View File

@ -52,14 +52,14 @@ pub fn CommentSection(post_id: i32) -> Element {
} }
}); });
let comments_resource = use_server_future(move || { let comments_resource = use_resource(move || async move {
let _ = ctx.refresh_trigger; let _ = (ctx.refresh_trigger)();
get_comments(post_id) get_comments(post_id).await
})?; });
let data = comments_resource.read(); let data = comments_resource.read();
match data.as_ref().map(|r| r.as_ref()) { match &*data {
Some(Ok(CommentTreeResponse { comments, count })) => { Some(Ok(CommentTreeResponse { comments, count })) => {
let approved_count = *count; let approved_count = *count;
let pending_count = ctx.pending_comments.read().len() as i64; let pending_count = ctx.pending_comments.read().len() as i64;