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.mjs CHANGED
@@ -17985,6 +17985,29 @@ function CommentThread({
17985
17985
  }
17986
17986
  loadComments();
17987
17987
  }, [dataSource, resolvedConfig.commentsPerPage]);
17988
+ function updateReplyCacheComment(cache, commentId, updater) {
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(
18000
+ Object.entries(cache).map(([parentId, replies]) => [
18001
+ parentId,
18002
+ replies.filter((reply) => reply.id !== commentId)
18003
+ ])
18004
+ );
18005
+ delete next[commentId];
18006
+ for (const childId of childIds) {
18007
+ next = removeCommentTreeFromCache(next, childId);
18008
+ }
18009
+ return next;
18010
+ }
17988
18011
  async function handleCreateComment(content) {
17989
18012
  const comment = await dataSource.createComment({
17990
18013
  content,
@@ -17992,6 +18015,25 @@ function CommentThread({
17992
18015
  });
17993
18016
  setComments((prev) => [comment, ...prev]);
17994
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
+ }
17995
18037
  async function handleReply(parentComment, content) {
17996
18038
  const reply = await dataSource.createComment({
17997
18039
  content,
@@ -18001,13 +18043,18 @@ function CommentThread({
18001
18043
  ...prev,
18002
18044
  [parentComment.id]: [...prev[parentComment.id] ?? [], reply]
18003
18045
  }));
18004
- setComments(
18005
- (prev) => prev.map(
18006
- (comment) => comment.id === parentComment.id ? {
18007
- ...comment,
18008
- replyCount: comment.replyCount + 1
18009
- } : comment
18010
- )
18046
+ const incrementReplyCount = (items) => items.map(
18047
+ (item) => item.id === parentComment.id ? {
18048
+ ...item,
18049
+ replyCount: item.replyCount + 1
18050
+ } : item
18051
+ );
18052
+ setComments((prev) => incrementReplyCount(prev));
18053
+ setReplyCache(
18054
+ (prev) => updateReplyCacheComment(prev, parentComment.id, (item) => ({
18055
+ ...item,
18056
+ replyCount: item.replyCount + 1
18057
+ }))
18011
18058
  );
18012
18059
  }
18013
18060
  async function handleLoadReplies(comment) {
@@ -18029,19 +18076,22 @@ function CommentThread({
18029
18076
  likes: comment.liked ? Math.max(comment.likes - 1, 0) : comment.likes + 1,
18030
18077
  dislikes: comment.disliked ? Math.max(comment.dislikes - 1, 0) : comment.dislikes
18031
18078
  };
18032
- setComments(
18033
- (prev) => updateComment(prev, comment.id, (item) => ({
18034
- ...item,
18035
- likes: optimistic.likes,
18036
- dislikes: optimistic.dislikes,
18037
- liked: optimistic.liked,
18038
- disliked: optimistic.disliked
18039
- }))
18040
- );
18079
+ const updater = (item) => ({
18080
+ ...item,
18081
+ likes: optimistic.likes,
18082
+ dislikes: optimistic.dislikes,
18083
+ liked: optimistic.liked,
18084
+ disliked: optimistic.disliked
18085
+ });
18086
+ setComments((prev) => updateComment(prev, comment.id, updater));
18087
+ setReplyCache((prev) => updateReplyCacheComment(prev, comment.id, updater));
18041
18088
  try {
18042
18089
  await dataSource.toggleLike(comment.id);
18043
18090
  } catch {
18044
18091
  setComments((prev) => updateComment(prev, comment.id, () => comment));
18092
+ setReplyCache(
18093
+ (prev) => updateReplyCacheComment(prev, comment.id, () => comment)
18094
+ );
18045
18095
  }
18046
18096
  }
18047
18097
  async function handleDislike(comment) {
@@ -18051,19 +18101,22 @@ function CommentThread({
18051
18101
  likes: comment.liked ? Math.max(comment.likes - 1, 0) : comment.likes,
18052
18102
  dislikes: comment.disliked ? Math.max(comment.dislikes - 1, 0) : comment.dislikes + 1
18053
18103
  };
18054
- setComments(
18055
- (prev) => updateComment(prev, comment.id, (item) => ({
18056
- ...item,
18057
- likes: optimistic.likes,
18058
- dislikes: optimistic.dislikes,
18059
- liked: optimistic.liked,
18060
- disliked: optimistic.disliked
18061
- }))
18062
- );
18104
+ const updater = (item) => ({
18105
+ ...item,
18106
+ likes: optimistic.likes,
18107
+ dislikes: optimistic.dislikes,
18108
+ liked: optimistic.liked,
18109
+ disliked: optimistic.disliked
18110
+ });
18111
+ setComments((prev) => updateComment(prev, comment.id, updater));
18112
+ setReplyCache((prev) => updateReplyCacheComment(prev, comment.id, updater));
18063
18113
  try {
18064
18114
  await dataSource.toggleDislike(comment.id);
18065
18115
  } catch {
18066
18116
  setComments((prev) => updateComment(prev, comment.id, () => comment));
18117
+ setReplyCache(
18118
+ (prev) => updateReplyCacheComment(prev, comment.id, () => comment)
18119
+ );
18067
18120
  }
18068
18121
  }
18069
18122
  if (loading || isLoadingComments) {
@@ -18091,7 +18144,7 @@ function CommentThread({
18091
18144
  onReply: handleReply,
18092
18145
  onLoadReplies: handleLoadReplies,
18093
18146
  onEdit,
18094
- onDelete,
18147
+ onDelete: handleDeleteComment,
18095
18148
  onReport
18096
18149
  }
18097
18150
  )