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.js CHANGED
@@ -18014,14 +18014,27 @@ function CommentThread({
18014
18014
  loadComments();
18015
18015
  }, [dataSource, resolvedConfig.commentsPerPage]);
18016
18016
  function updateReplyCacheComment(cache, commentId, updater) {
18017
- return Object.fromEntries(
18017
+ const next = {};
18018
+ for (const [parentId, replies] of Object.entries(cache)) {
18019
+ next[parentId] = replies.map(
18020
+ (reply) => reply.id === commentId ? updater(reply) : reply
18021
+ );
18022
+ }
18023
+ return next;
18024
+ }
18025
+ function removeCommentTreeFromCache(cache, commentId) {
18026
+ const childIds = cache[commentId]?.map((reply) => reply.id) ?? [];
18027
+ let next = Object.fromEntries(
18018
18028
  Object.entries(cache).map(([parentId, replies]) => [
18019
18029
  parentId,
18020
- replies.map(
18021
- (reply) => reply.id === commentId ? updater(reply) : reply
18022
- )
18030
+ replies.filter((reply) => reply.id !== commentId)
18023
18031
  ])
18024
18032
  );
18033
+ delete next[commentId];
18034
+ for (const childId of childIds) {
18035
+ next = removeCommentTreeFromCache(next, childId);
18036
+ }
18037
+ return next;
18025
18038
  }
18026
18039
  async function handleCreateComment(content) {
18027
18040
  const comment = await dataSource.createComment({
@@ -18030,6 +18043,29 @@ function CommentThread({
18030
18043
  });
18031
18044
  setComments((prev) => [comment, ...prev]);
18032
18045
  }
18046
+ async function handleDeleteComment(comment) {
18047
+ await dataSource.deleteComment(comment.id);
18048
+ setComments((prev) => prev.filter((item) => item.id !== comment.id));
18049
+ setReplyCache((prev) => removeCommentTreeFromCache(prev, comment.id));
18050
+ if (comment.parentCommentId) {
18051
+ setComments(
18052
+ (prev) => updateComment(prev, comment.parentCommentId, (item) => ({
18053
+ ...item,
18054
+ replyCount: Math.max(item.replyCount - 1, 0)
18055
+ }))
18056
+ );
18057
+ setReplyCache(
18058
+ (prev) => updateReplyCacheComment(prev, comment.parentCommentId, (item) => ({
18059
+ ...item,
18060
+ replyCount: Math.max(item.replyCount - 1, 0)
18061
+ }))
18062
+ );
18063
+ }
18064
+ }
18065
+ function updateCommentEverywhere(commentId, updater) {
18066
+ setComments((prev) => updateComment(prev, commentId, updater));
18067
+ setReplyCache((prev) => updateReplyCacheComment(prev, commentId, updater));
18068
+ }
18033
18069
  async function handleReply(parentComment, content) {
18034
18070
  const reply = await dataSource.createComment({
18035
18071
  content,
@@ -18115,6 +18151,10 @@ function CommentThread({
18115
18151
  );
18116
18152
  }
18117
18153
  }
18154
+ function handleEditComment(updatedComment) {
18155
+ updateCommentEverywhere(updatedComment.id, () => updatedComment);
18156
+ onEdit?.(updatedComment);
18157
+ }
18118
18158
  if (loading || isLoadingComments) {
18119
18159
  return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-6", children: "Loading comments..." });
18120
18160
  }
@@ -18139,8 +18179,8 @@ function CommentThread({
18139
18179
  onDislike: handleDislike,
18140
18180
  onReply: handleReply,
18141
18181
  onLoadReplies: handleLoadReplies,
18142
- onEdit,
18143
- onDelete,
18182
+ onEdit: handleEditComment,
18183
+ onDelete: handleDeleteComment,
18144
18184
  onReport
18145
18185
  }
18146
18186
  )