@liveblocks/react 1.4.2 → 1.4.4

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.d.mts CHANGED
@@ -476,6 +476,26 @@ declare type RoomContextBundleShared<TPresence extends JsonObject, TStorage exte
476
476
  * deleteComment({ threadId: "th_xxx", commentId: "cm_xxx" })
477
477
  */
478
478
  useDeleteComment(): (options: DeleteCommentOptions) => void;
479
+ /**
480
+ * @beta
481
+ *
482
+ * Returns a function that adds a reaction from a comment.
483
+ *
484
+ * @example
485
+ * const addReaction = useAddReaction();
486
+ * addReaction({ threadId: "th_xxx", commentId: "cm_xxx", emoji: "👍" })
487
+ */
488
+ useAddReaction(): (options: CommentReactionOptions) => void;
489
+ /**
490
+ * @beta
491
+ *
492
+ * Returns a function that removes a reaction on a comment.
493
+ *
494
+ * @example
495
+ * const removeReaction = useRemoveReaction();
496
+ * removeReaction({ threadId: "th_xxx", commentId: "cm_xxx", emoji: "👍" })
497
+ */
498
+ useRemoveReaction(): (options: CommentReactionOptions) => void;
479
499
  };
480
500
  declare type RoomContextBundle<TPresence extends JsonObject, TStorage extends LsonObject, TUserMeta extends BaseUserMeta, TRoomEvent extends Json, TThreadMetadata extends BaseMetadata> = Resolve<RoomContextBundleShared<TPresence, TStorage, TUserMeta, TRoomEvent, TThreadMetadata> & {
481
501
  /**
package/dist/index.d.ts CHANGED
@@ -476,6 +476,26 @@ declare type RoomContextBundleShared<TPresence extends JsonObject, TStorage exte
476
476
  * deleteComment({ threadId: "th_xxx", commentId: "cm_xxx" })
477
477
  */
478
478
  useDeleteComment(): (options: DeleteCommentOptions) => void;
479
+ /**
480
+ * @beta
481
+ *
482
+ * Returns a function that adds a reaction from a comment.
483
+ *
484
+ * @example
485
+ * const addReaction = useAddReaction();
486
+ * addReaction({ threadId: "th_xxx", commentId: "cm_xxx", emoji: "👍" })
487
+ */
488
+ useAddReaction(): (options: CommentReactionOptions) => void;
489
+ /**
490
+ * @beta
491
+ *
492
+ * Returns a function that removes a reaction on a comment.
493
+ *
494
+ * @example
495
+ * const removeReaction = useRemoveReaction();
496
+ * removeReaction({ threadId: "th_xxx", commentId: "cm_xxx", emoji: "👍" })
497
+ */
498
+ useRemoveReaction(): (options: CommentReactionOptions) => void;
479
499
  };
480
500
  declare type RoomContextBundle<TPresence extends JsonObject, TStorage extends LsonObject, TUserMeta extends BaseUserMeta, TRoomEvent extends Json, TThreadMetadata extends BaseMetadata> = Resolve<RoomContextBundleShared<TPresence, TStorage, TUserMeta, TRoomEvent, TThreadMetadata> & {
481
501
  /**
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ var _core = require('@liveblocks/core');
5
5
 
6
6
  // src/version.ts
7
7
  var PKG_NAME = "@liveblocks/react";
8
- var PKG_VERSION = "1.4.2";
8
+ var PKG_VERSION = "1.4.4";
9
9
  var PKG_FORMAT = "cjs";
10
10
 
11
11
  // src/ClientSideSuspense.tsx
@@ -488,18 +488,37 @@ function createCommentsRoom(room, errorEventSource) {
488
488
  }) {
489
489
  const threads = getThreads();
490
490
  const now = (/* @__PURE__ */ new Date()).toISOString();
491
+ const userId = getCurrentUserId();
491
492
  const optimisticData = threads.map(
492
493
  (thread) => thread.id === threadId ? {
493
494
  ...thread,
494
- comments: thread.comments.map(
495
- (comment) => comment.id === commentId ? {
496
- ...comment,
497
- reactions: [
495
+ comments: thread.comments.map((comment) => {
496
+ if (comment.id !== commentId) {
497
+ return comment;
498
+ }
499
+ let reactions;
500
+ if (comment.reactions.some((reaction) => reaction.emoji === emoji)) {
501
+ reactions = comment.reactions.map(
502
+ (reaction) => reaction.emoji === emoji ? {
503
+ ...reaction,
504
+ users: [...reaction.users, { id: userId }]
505
+ } : reaction
506
+ );
507
+ } else {
508
+ reactions = [
498
509
  ...comment.reactions,
499
- { emoji, userId: getCurrentUserId(), createdAt: now }
500
- ]
501
- } : comment
502
- )
510
+ {
511
+ emoji,
512
+ createdAt: now,
513
+ users: [{ id: userId }]
514
+ }
515
+ ];
516
+ }
517
+ return {
518
+ ...comment,
519
+ reactions
520
+ };
521
+ })
503
522
  } : thread
504
523
  );
505
524
  mutate(room.addReaction({ threadId, commentId, emoji }), {
@@ -521,17 +540,37 @@ function createCommentsRoom(room, errorEventSource) {
521
540
  emoji
522
541
  }) {
523
542
  const threads = getThreads();
543
+ const userId = getCurrentUserId();
524
544
  const optimisticData = threads.map(
525
545
  (thread) => thread.id === threadId ? {
526
546
  ...thread,
527
547
  comments: thread.comments.map((comment) => {
548
+ if (comment.id !== commentId) {
549
+ return comment;
550
+ }
528
551
  const reactionIndex = comment.reactions.findIndex(
529
- (reaction) => reaction.emoji === emoji && reaction.userId === getCurrentUserId()
552
+ (reaction) => reaction.emoji === emoji
530
553
  );
531
- return comment.id === commentId ? {
554
+ let reactions = comment.reactions;
555
+ if (reactionIndex > 0 && comment.reactions[reactionIndex].users.some(
556
+ (user) => user.id === userId
557
+ )) {
558
+ if (comment.reactions[reactionIndex].users.length <= 1) {
559
+ reactions = [...comment.reactions];
560
+ reactions.splice(reactionIndex, 1);
561
+ } else {
562
+ reactions[reactionIndex] = {
563
+ ...reactions[reactionIndex],
564
+ users: reactions[reactionIndex].users.filter(
565
+ (user) => user.id !== userId
566
+ )
567
+ };
568
+ }
569
+ }
570
+ return {
532
571
  ...comment,
533
- reactions: reactionIndex < 0 ? comment.reactions : comment.reactions.slice(0, reactionIndex).concat(comment.reactions.slice(reactionIndex + 1))
534
- } : comment;
572
+ reactions
573
+ };
535
574
  })
536
575
  } : thread
537
576
  );
@@ -692,8 +731,12 @@ var superfluous_unstable_batchedUpdates = "You don\u2019t need to pass unstable_
692
731
  function useSyncExternalStore3(s, gs, gss) {
693
732
  return _withselectorjs.useSyncExternalStoreWithSelector.call(void 0, s, gs, gss, identity);
694
733
  }
695
- function getEmptyOthers() {
696
- return [];
734
+ var STABLE_EMPTY_LIST = Object.freeze([]);
735
+ function alwaysEmptyList() {
736
+ return STABLE_EMPTY_LIST;
737
+ }
738
+ function alwaysNull() {
739
+ return null;
697
740
  }
698
741
  function makeMutationContext(room) {
699
742
  const errmsg = "This mutation cannot be used until connected to the Liveblocks room";
@@ -850,7 +893,7 @@ function createRoomContext(client, options) {
850
893
  const room = useRoom();
851
894
  const subscribe = room.events.others.subscribe;
852
895
  const getSnapshot = room.getOthers;
853
- const getServerSnapshot = getEmptyOthers;
896
+ const getServerSnapshot = alwaysEmptyList;
854
897
  return _withselectorjs.useSyncExternalStoreWithSelector.call(void 0,
855
898
  subscribe,
856
899
  getSnapshot,
@@ -965,7 +1008,7 @@ function createRoomContext(client, options) {
965
1008
  (me) => me !== null ? selector(me) : null,
966
1009
  [selector]
967
1010
  );
968
- const getServerSnapshot = React2.useCallback(() => null, []);
1011
+ const getServerSnapshot = alwaysNull;
969
1012
  return _withselectorjs.useSyncExternalStoreWithSelector.call(void 0,
970
1013
  subscribe,
971
1014
  getSnapshot,
@@ -978,7 +1021,7 @@ function createRoomContext(client, options) {
978
1021
  const room = useRoom();
979
1022
  const subscribe = room.events.storageDidLoad.subscribeOnce;
980
1023
  const getSnapshot = room.getStorageSnapshot;
981
- const getServerSnapshot = React2.useCallback(() => null, []);
1024
+ const getServerSnapshot = alwaysNull;
982
1025
  return useSyncExternalStore3(subscribe, getSnapshot, getServerSnapshot);
983
1026
  }
984
1027
  function useStorageRoot() {
@@ -1065,7 +1108,7 @@ function createRoomContext(client, options) {
1065
1108
  return imm;
1066
1109
  }
1067
1110
  }, [rootOrNull]);
1068
- const getServerSnapshot = React2.useCallback(() => null, []);
1111
+ const getServerSnapshot = alwaysNull;
1069
1112
  return _withselectorjs.useSyncExternalStoreWithSelector.call(void 0,
1070
1113
  subscribe,
1071
1114
  getSnapshot,