Compare commits
No commits in common. "b55409d4215c5e1be26c8c5b0ba04e4656d98c0c" and "2b36a7c669509390094d47b6c3a515e8b47409b1" have entirely different histories.
b55409d421
...
2b36a7c669
@ -1,14 +1,17 @@
|
|||||||
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
|
||||||
@ -18,6 +21,7 @@ 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;
|
||||||
|
|
||||||
@ -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 {
|
if is_replying {
|
||||||
|
|||||||
@ -5,76 +5,34 @@ 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_and_treeify(
|
fn merge_comments(
|
||||||
approved: Vec<PublicComment>,
|
approved: Vec<PublicComment>,
|
||||||
pending: Vec<PendingComment>,
|
pending: Vec<PendingComment>,
|
||||||
) -> Vec<MergedComment> {
|
) -> Vec<MergedComment> {
|
||||||
use std::collections::{HashMap, HashSet};
|
let mut merged: Vec<MergedComment> = approved
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
let all_ids: HashSet<i64> = all.iter().map(|c| match c {
|
merged.sort_by(|a, b| {
|
||||||
MergedComment::Approved(c) => c.id,
|
let time_a = match a {
|
||||||
MergedComment::Pending(c) => c.id,
|
MergedComment::Approved(c) => c.created_at_iso.as_str(),
|
||||||
}).collect();
|
MergedComment::Pending(c) => c.created_at.as_str(),
|
||||||
|
|
||||||
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 {
|
let time_b = match b {
|
||||||
Some(pid) if !all_ids.contains(&pid) => None,
|
MergedComment::Approved(c) => c.created_at_iso.as_str(),
|
||||||
_ => parent_id,
|
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() {
|
merged
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
@ -83,7 +41,7 @@ pub fn CommentList(
|
|||||||
pending: Vec<PendingComment>,
|
pending: Vec<PendingComment>,
|
||||||
post_id: i32,
|
post_id: i32,
|
||||||
) -> Element {
|
) -> Element {
|
||||||
let merged = merge_and_treeify(comments, pending);
|
let merged = merge_comments(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]",
|
||||||
|
|||||||
@ -52,14 +52,14 @@ pub fn CommentSection(post_id: i32) -> Element {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let comments_resource = use_resource(move || async move {
|
let comments_resource = use_server_future(move || {
|
||||||
let _ = (ctx.refresh_trigger)();
|
let _ = ctx.refresh_trigger;
|
||||||
get_comments(post_id).await
|
get_comments(post_id)
|
||||||
});
|
})?;
|
||||||
|
|
||||||
let data = comments_resource.read();
|
let data = comments_resource.read();
|
||||||
|
|
||||||
match &*data {
|
match data.as_ref().map(|r| r.as_ref()) {
|
||||||
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;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user