elseware-ui 2.31.11 → 2.31.13

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
@@ -18013,6 +18013,29 @@ function CommentThread({
18013
18013
  }
18014
18014
  loadComments();
18015
18015
  }, [dataSource, resolvedConfig.commentsPerPage]);
18016
+ function updateReplyCacheComment(cache, commentId, updater) {
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(
18028
+ Object.entries(cache).map(([parentId, replies]) => [
18029
+ parentId,
18030
+ replies.filter((reply) => reply.id !== commentId)
18031
+ ])
18032
+ );
18033
+ delete next[commentId];
18034
+ for (const childId of childIds) {
18035
+ next = removeCommentTreeFromCache(next, childId);
18036
+ }
18037
+ return next;
18038
+ }
18016
18039
  async function handleCreateComment(content) {
18017
18040
  const comment = await dataSource.createComment({
18018
18041
  content,
@@ -18020,6 +18043,25 @@ function CommentThread({
18020
18043
  });
18021
18044
  setComments((prev) => [comment, ...prev]);
18022
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
+ }
18023
18065
  async function handleReply(parentComment, content) {
18024
18066
  const reply = await dataSource.createComment({
18025
18067
  content,
@@ -18029,13 +18071,18 @@ function CommentThread({
18029
18071
  ...prev,
18030
18072
  [parentComment.id]: [...prev[parentComment.id] ?? [], reply]
18031
18073
  }));
18032
- setComments(
18033
- (prev) => prev.map(
18034
- (comment) => comment.id === parentComment.id ? {
18035
- ...comment,
18036
- replyCount: comment.replyCount + 1
18037
- } : comment
18038
- )
18074
+ const incrementReplyCount = (items) => items.map(
18075
+ (item) => item.id === parentComment.id ? {
18076
+ ...item,
18077
+ replyCount: item.replyCount + 1
18078
+ } : item
18079
+ );
18080
+ setComments((prev) => incrementReplyCount(prev));
18081
+ setReplyCache(
18082
+ (prev) => updateReplyCacheComment(prev, parentComment.id, (item) => ({
18083
+ ...item,
18084
+ replyCount: item.replyCount + 1
18085
+ }))
18039
18086
  );
18040
18087
  }
18041
18088
  async function handleLoadReplies(comment) {
@@ -18057,19 +18104,22 @@ function CommentThread({
18057
18104
  likes: comment.liked ? Math.max(comment.likes - 1, 0) : comment.likes + 1,
18058
18105
  dislikes: comment.disliked ? Math.max(comment.dislikes - 1, 0) : comment.dislikes
18059
18106
  };
18060
- setComments(
18061
- (prev) => updateComment(prev, comment.id, (item) => ({
18062
- ...item,
18063
- likes: optimistic.likes,
18064
- dislikes: optimistic.dislikes,
18065
- liked: optimistic.liked,
18066
- disliked: optimistic.disliked
18067
- }))
18068
- );
18107
+ const updater = (item) => ({
18108
+ ...item,
18109
+ likes: optimistic.likes,
18110
+ dislikes: optimistic.dislikes,
18111
+ liked: optimistic.liked,
18112
+ disliked: optimistic.disliked
18113
+ });
18114
+ setComments((prev) => updateComment(prev, comment.id, updater));
18115
+ setReplyCache((prev) => updateReplyCacheComment(prev, comment.id, updater));
18069
18116
  try {
18070
18117
  await dataSource.toggleLike(comment.id);
18071
18118
  } catch {
18072
18119
  setComments((prev) => updateComment(prev, comment.id, () => comment));
18120
+ setReplyCache(
18121
+ (prev) => updateReplyCacheComment(prev, comment.id, () => comment)
18122
+ );
18073
18123
  }
18074
18124
  }
18075
18125
  async function handleDislike(comment) {
@@ -18079,19 +18129,22 @@ function CommentThread({
18079
18129
  likes: comment.liked ? Math.max(comment.likes - 1, 0) : comment.likes,
18080
18130
  dislikes: comment.disliked ? Math.max(comment.dislikes - 1, 0) : comment.dislikes + 1
18081
18131
  };
18082
- setComments(
18083
- (prev) => updateComment(prev, comment.id, (item) => ({
18084
- ...item,
18085
- likes: optimistic.likes,
18086
- dislikes: optimistic.dislikes,
18087
- liked: optimistic.liked,
18088
- disliked: optimistic.disliked
18089
- }))
18090
- );
18132
+ const updater = (item) => ({
18133
+ ...item,
18134
+ likes: optimistic.likes,
18135
+ dislikes: optimistic.dislikes,
18136
+ liked: optimistic.liked,
18137
+ disliked: optimistic.disliked
18138
+ });
18139
+ setComments((prev) => updateComment(prev, comment.id, updater));
18140
+ setReplyCache((prev) => updateReplyCacheComment(prev, comment.id, updater));
18091
18141
  try {
18092
18142
  await dataSource.toggleDislike(comment.id);
18093
18143
  } catch {
18094
18144
  setComments((prev) => updateComment(prev, comment.id, () => comment));
18145
+ setReplyCache(
18146
+ (prev) => updateReplyCacheComment(prev, comment.id, () => comment)
18147
+ );
18095
18148
  }
18096
18149
  }
18097
18150
  if (loading || isLoadingComments) {
@@ -18119,7 +18172,7 @@ function CommentThread({
18119
18172
  onReply: handleReply,
18120
18173
  onLoadReplies: handleLoadReplies,
18121
18174
  onEdit,
18122
- onDelete,
18175
+ onDelete: handleDeleteComment,
18123
18176
  onReport
18124
18177
  }
18125
18178
  )