elseware-ui 2.31.12 → 2.31.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -17986,14 +17986,27 @@ function CommentThread({
17986
17986
  loadComments();
17987
17987
  }, [dataSource, resolvedConfig.commentsPerPage]);
17988
17988
  function updateReplyCacheComment(cache, commentId, updater) {
17989
- return Object.fromEntries(
17989
+ const next = {};
17990
+ for (const [parentId, replies] of Object.entries(cache)) {
17991
+ next[parentId] = replies.map(
17992
+ (reply) => reply.id === commentId ? updater(reply) : reply
17993
+ );
17994
+ }
17995
+ return next;
17996
+ }
17997
+ function removeCommentTreeFromCache(cache, commentId) {
17998
+ const childIds = cache[commentId]?.map((reply) => reply.id) ?? [];
17999
+ let next = Object.fromEntries(
17990
18000
  Object.entries(cache).map(([parentId, replies]) => [
17991
18001
  parentId,
17992
- replies.map(
17993
- (reply) => reply.id === commentId ? updater(reply) : reply
17994
- )
18002
+ replies.filter((reply) => reply.id !== commentId)
17995
18003
  ])
17996
18004
  );
18005
+ delete next[commentId];
18006
+ for (const childId of childIds) {
18007
+ next = removeCommentTreeFromCache(next, childId);
18008
+ }
18009
+ return next;
17997
18010
  }
17998
18011
  async function handleCreateComment(content) {
17999
18012
  const comment = await dataSource.createComment({
@@ -18002,6 +18015,29 @@ function CommentThread({
18002
18015
  });
18003
18016
  setComments((prev) => [comment, ...prev]);
18004
18017
  }
18018
+ async function handleDeleteComment(comment) {
18019
+ await dataSource.deleteComment(comment.id);
18020
+ setComments((prev) => prev.filter((item) => item.id !== comment.id));
18021
+ setReplyCache((prev) => removeCommentTreeFromCache(prev, comment.id));
18022
+ if (comment.parentCommentId) {
18023
+ setComments(
18024
+ (prev) => updateComment(prev, comment.parentCommentId, (item) => ({
18025
+ ...item,
18026
+ replyCount: Math.max(item.replyCount - 1, 0)
18027
+ }))
18028
+ );
18029
+ setReplyCache(
18030
+ (prev) => updateReplyCacheComment(prev, comment.parentCommentId, (item) => ({
18031
+ ...item,
18032
+ replyCount: Math.max(item.replyCount - 1, 0)
18033
+ }))
18034
+ );
18035
+ }
18036
+ }
18037
+ function updateCommentEverywhere(commentId, updater) {
18038
+ setComments((prev) => updateComment(prev, commentId, updater));
18039
+ setReplyCache((prev) => updateReplyCacheComment(prev, commentId, updater));
18040
+ }
18005
18041
  async function handleReply(parentComment, content) {
18006
18042
  const reply = await dataSource.createComment({
18007
18043
  content,
@@ -18087,6 +18123,10 @@ function CommentThread({
18087
18123
  );
18088
18124
  }
18089
18125
  }
18126
+ function handleEditComment(updatedComment) {
18127
+ updateCommentEverywhere(updatedComment.id, () => updatedComment);
18128
+ onEdit?.(updatedComment);
18129
+ }
18090
18130
  if (loading || isLoadingComments) {
18091
18131
  return /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-6", children: "Loading comments..." });
18092
18132
  }
@@ -18111,8 +18151,8 @@ function CommentThread({
18111
18151
  onDislike: handleDislike,
18112
18152
  onReply: handleReply,
18113
18153
  onLoadReplies: handleLoadReplies,
18114
- onEdit,
18115
- onDelete,
18154
+ onEdit: handleEditComment,
18155
+ onDelete: handleDeleteComment,
18116
18156
  onReport
18117
18157
  }
18118
18158
  )