@liveblocks/react 1.10.0-beta3 → 1.10.0-beta4

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
@@ -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.10.0-beta3";
8
+ var PKG_VERSION = "1.10.0-beta4";
9
9
  var PKG_FORMAT = "cjs";
10
10
 
11
11
  // src/ClientSideSuspense.tsx
@@ -63,6 +63,10 @@ var _client = require('@liveblocks/client');
63
63
 
64
64
 
65
65
 
66
+
67
+
68
+
69
+
66
70
 
67
71
  // src/comments/errors.ts
68
72
  var CreateThreadError = class extends Error {
@@ -187,34 +191,6 @@ function selectedThreads(roomId, state, options) {
187
191
  return threads.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
188
192
  }
189
193
 
190
- // src/comments/lib/upsert-comment.ts
191
- function upsertComment(threads, newComment) {
192
- const thread = threads[newComment.threadId];
193
- if (thread === void 0) {
194
- return threads;
195
- }
196
- const newComments = [];
197
- let updated = false;
198
- for (const comment of thread.comments) {
199
- if (comment.id === newComment.id) {
200
- updated = true;
201
- newComments.push(newComment);
202
- } else {
203
- newComments.push(comment);
204
- }
205
- }
206
- if (!updated) {
207
- newComments.push(newComment);
208
- }
209
- return {
210
- ...threads,
211
- [thread.id]: {
212
- ...thread,
213
- comments: newComments
214
- }
215
- };
216
- }
217
-
218
194
  // src/lib/use-initial.ts
219
195
 
220
196
  function useInitial(value) {
@@ -383,7 +359,9 @@ function createRoomContext(client, options) {
383
359
  );
384
360
  React2.useEffect(() => {
385
361
  async function handleCommentEvent(message) {
386
- const info = await room.getThread({ threadId: message.threadId });
362
+ const info = await room[_core.kInternal].comments.getThread({
363
+ threadId: message.threadId
364
+ });
387
365
  if (!info) {
388
366
  store.deleteThread(message.threadId);
389
367
  return;
@@ -411,6 +389,9 @@ function createRoomContext(client, options) {
411
389
  (message) => void handleCommentEvent(message)
412
390
  );
413
391
  }, [room]);
392
+ React2.useEffect(() => {
393
+ void getThreadsUpdates(room.id);
394
+ }, [room.id]);
414
395
  React2.useEffect(() => {
415
396
  const pair = stableEnterRoom(roomId, frozenProps);
416
397
  setRoomLeavePair(pair);
@@ -803,7 +784,7 @@ function createRoomContext(client, options) {
803
784
  );
804
785
  if (requestsCache.has(notificationSettingsQuery)) {
805
786
  requests.push(
806
- room.getRoomNotificationSettings().then((settings) => {
787
+ room[_core.kInternal].notifications.getRoomNotificationSettings().then((settings) => {
807
788
  store.updateRoomInboxNotificationSettings(
808
789
  room.id,
809
790
  settings,
@@ -813,11 +794,11 @@ function createRoomContext(client, options) {
813
794
  })
814
795
  );
815
796
  }
816
- const lastRequestedAt = room[_core.kInternal].comments.lastRequestedAt;
817
- if (lastRequestedAt === null)
797
+ const lastRequestedAt = lastRequestedAtByRoom.get(room.id);
798
+ if (lastRequestedAt === void 0)
818
799
  return;
819
800
  requests.push(
820
- room.getThreads({ since: lastRequestedAt }).then((result) => {
801
+ room[_core.kInternal].comments.getThreads({ since: lastRequestedAt }).then((result) => {
821
802
  store.updateThreadsAndNotifications(
822
803
  result.threads,
823
804
  result.inboxNotifications,
@@ -827,7 +808,7 @@ function createRoomContext(client, options) {
827
808
  const room2 = client.getRoom(roomId);
828
809
  if (room2 === null)
829
810
  return;
830
- room2[_core.kInternal].comments.lastRequestedAt = result.meta.requestedAt;
811
+ lastRequestedAtByRoom.set(room2.id, result.meta.requestedAt);
831
812
  }).catch(() => {
832
813
  })
833
814
  );
@@ -867,7 +848,7 @@ function createRoomContext(client, options) {
867
848
  if (requestInfo !== void 0) {
868
849
  return requestInfo.promise;
869
850
  }
870
- const promise = room.getThreads(options2);
851
+ const promise = room[_core.kInternal].comments.getThreads(options2);
871
852
  requestsCache.set(queryKey, {
872
853
  promise,
873
854
  subscribers: 0
@@ -884,9 +865,9 @@ function createRoomContext(client, options) {
884
865
  result.deletedInboxNotifications,
885
866
  queryKey
886
867
  );
887
- const lastRequestedAt = room[_core.kInternal].comments.lastRequestedAt;
888
- if (lastRequestedAt === null || lastRequestedAt > result.meta.requestedAt) {
889
- room[_core.kInternal].comments.lastRequestedAt = result.meta.requestedAt;
868
+ const lastRequestedAt = lastRequestedAtByRoom.get(room.id);
869
+ if (lastRequestedAt === void 0 || lastRequestedAt > result.meta.requestedAt) {
870
+ lastRequestedAtByRoom.set(room.id, result.meta.requestedAt);
890
871
  }
891
872
  } catch (err) {
892
873
  store.setQueryState(queryKey, {
@@ -896,6 +877,35 @@ function createRoomContext(client, options) {
896
877
  }
897
878
  poller.start(POLLING_INTERVAL);
898
879
  }
880
+ const DEFAULT_DEDUPING_INTERVAL = 2e3;
881
+ const lastRequestedAtByRoom = /* @__PURE__ */ new Map();
882
+ let isFetchingThreadsUpdates = false;
883
+ async function getThreadsUpdates(roomId) {
884
+ const room = client.getRoom(roomId);
885
+ if (room === null)
886
+ return;
887
+ const since = lastRequestedAtByRoom.get(room.id);
888
+ if (since === void 0)
889
+ return;
890
+ if (isFetchingThreadsUpdates)
891
+ return;
892
+ try {
893
+ isFetchingThreadsUpdates = true;
894
+ const updates = await room[_core.kInternal].comments.getThreads({ since });
895
+ setTimeout(() => {
896
+ isFetchingThreadsUpdates = false;
897
+ }, DEFAULT_DEDUPING_INTERVAL);
898
+ store.updateThreadsAndNotifications(
899
+ updates.threads,
900
+ updates.inboxNotifications,
901
+ updates.deletedThreads,
902
+ updates.deletedInboxNotifications
903
+ );
904
+ lastRequestedAtByRoom.set(room.id, updates.meta.requestedAt);
905
+ } catch (err) {
906
+ isFetchingThreadsUpdates = false;
907
+ }
908
+ }
899
909
  function useThreads(options2 = { query: { metadata: {} } }) {
900
910
  const room = useRoom();
901
911
  const queryKey = React2.useMemo(
@@ -975,12 +985,12 @@ function createRoomContext(client, options) {
975
985
  const metadata = "metadata" in options2 ? options2.metadata : {};
976
986
  const threadId = createThreadId();
977
987
  const commentId = createCommentId();
978
- const now = /* @__PURE__ */ new Date();
988
+ const createdAt = /* @__PURE__ */ new Date();
979
989
  const newComment = {
980
990
  id: commentId,
981
991
  threadId,
982
992
  roomId: room.id,
983
- createdAt: now,
993
+ createdAt,
984
994
  type: "comment",
985
995
  userId: getCurrentUserId(room),
986
996
  body,
@@ -989,7 +999,8 @@ function createRoomContext(client, options) {
989
999
  const newThread = {
990
1000
  id: threadId,
991
1001
  type: "thread",
992
- createdAt: now,
1002
+ createdAt,
1003
+ updatedAt: createdAt,
993
1004
  roomId: room.id,
994
1005
  metadata,
995
1006
  comments: [newComment]
@@ -1000,7 +1011,7 @@ function createRoomContext(client, options) {
1000
1011
  thread: newThread,
1001
1012
  id: optimisticUpdateId
1002
1013
  });
1003
- room.createThread({ threadId, commentId, body, metadata }).then(
1014
+ room[_core.kInternal].comments.createThread({ threadId, commentId, body, metadata }).then(
1004
1015
  (thread) => {
1005
1016
  store.set((state) => ({
1006
1017
  ...state,
@@ -1039,23 +1050,38 @@ function createRoomContext(client, options) {
1039
1050
  }
1040
1051
  const threadId = options2.threadId;
1041
1052
  const metadata = options2.metadata;
1053
+ const updatedAt = /* @__PURE__ */ new Date();
1042
1054
  const optimisticUpdateId = _nanoid.nanoid.call(void 0, );
1043
1055
  store.pushOptimisticUpdate({
1044
1056
  type: "edit-thread-metadata",
1045
1057
  metadata,
1046
1058
  id: optimisticUpdateId,
1047
- threadId
1059
+ threadId,
1060
+ updatedAt
1048
1061
  });
1049
- room.editThreadMetadata({ metadata, threadId }).then(
1062
+ room[_core.kInternal].comments.editThreadMetadata({ metadata, threadId }).then(
1050
1063
  (metadata2) => {
1051
1064
  store.set((state) => {
1052
1065
  const existingThread = state.threads[threadId];
1066
+ const updatedOptimisticUpdates = state.optimisticUpdates.filter(
1067
+ (update) => update.id !== optimisticUpdateId
1068
+ );
1053
1069
  if (existingThread === void 0) {
1054
1070
  return {
1055
1071
  ...state,
1056
- optimisticUpdates: state.optimisticUpdates.filter(
1057
- (update) => update.id !== optimisticUpdateId
1058
- )
1072
+ optimisticUpdates: updatedOptimisticUpdates
1073
+ };
1074
+ }
1075
+ if (existingThread.deletedAt !== void 0) {
1076
+ return {
1077
+ ...state,
1078
+ optimisticUpdates: updatedOptimisticUpdates
1079
+ };
1080
+ }
1081
+ if (existingThread.updatedAt && existingThread.updatedAt > updatedAt) {
1082
+ return {
1083
+ ...state,
1084
+ optimisticUpdates: updatedOptimisticUpdates
1059
1085
  };
1060
1086
  }
1061
1087
  return {
@@ -1070,9 +1096,7 @@ function createRoomContext(client, options) {
1070
1096
  }
1071
1097
  }
1072
1098
  },
1073
- optimisticUpdates: state.optimisticUpdates.filter(
1074
- (update) => update.id !== optimisticUpdateId
1075
- )
1099
+ optimisticUpdates: updatedOptimisticUpdates
1076
1100
  };
1077
1101
  });
1078
1102
  },
@@ -1094,64 +1118,44 @@ function createRoomContext(client, options) {
1094
1118
  const room = useRoom();
1095
1119
  return React2.useCallback(
1096
1120
  ({ threadId, commentId, emoji }) => {
1097
- const now = /* @__PURE__ */ new Date();
1121
+ const createdAt = /* @__PURE__ */ new Date();
1098
1122
  const userId = getCurrentUserId(room);
1099
1123
  const optimisticUpdateId = _nanoid.nanoid.call(void 0, );
1100
1124
  store.pushOptimisticUpdate({
1101
1125
  type: "add-reaction",
1102
1126
  threadId,
1103
1127
  commentId,
1104
- emoji,
1105
- userId,
1106
- createdAt: now,
1128
+ reaction: {
1129
+ emoji,
1130
+ userId,
1131
+ createdAt
1132
+ },
1107
1133
  id: optimisticUpdateId
1108
1134
  });
1109
- room.addReaction({ threadId, commentId, emoji }).then(
1135
+ room[_core.kInternal].comments.addReaction({ threadId, commentId, emoji }).then(
1110
1136
  (addedReaction) => {
1111
1137
  store.set((state) => {
1112
1138
  const existingThread = state.threads[threadId];
1139
+ const updatedOptimisticUpdates = state.optimisticUpdates.filter(
1140
+ (update) => update.id !== optimisticUpdateId
1141
+ );
1113
1142
  if (existingThread === void 0) {
1114
1143
  return {
1115
1144
  ...state,
1116
- optimisticUpdates: state.optimisticUpdates.filter(
1117
- (update) => update.id !== optimisticUpdateId
1118
- )
1145
+ optimisticUpdates: updatedOptimisticUpdates
1119
1146
  };
1120
1147
  }
1121
1148
  return {
1122
1149
  ...state,
1123
1150
  threads: {
1124
1151
  ...state.threads,
1125
- [threadId]: {
1126
- ...existingThread,
1127
- comments: existingThread.comments.map(
1128
- (comment) => comment.id === commentId ? {
1129
- ...comment,
1130
- reactions: comment.reactions.some(
1131
- (reaction) => reaction.emoji === addedReaction.emoji
1132
- ) ? comment.reactions.map(
1133
- (reaction) => reaction.emoji === addedReaction.emoji ? {
1134
- ...reaction,
1135
- users: [
1136
- ...reaction.users,
1137
- { id: addedReaction.userId }
1138
- ]
1139
- } : reaction
1140
- ) : [
1141
- ...comment.reactions,
1142
- {
1143
- emoji: addedReaction.emoji,
1144
- createdAt: addedReaction.createdAt,
1145
- users: [{ id: addedReaction.userId }]
1146
- }
1147
- ]
1148
- } : comment
1149
- )
1150
- }
1152
+ [threadId]: _core.addReaction.call(void 0,
1153
+ existingThread,
1154
+ commentId,
1155
+ addedReaction
1156
+ )
1151
1157
  },
1152
- optimisticUpdates: state.optimisticUpdates.filter(
1153
- (update) => update.id !== optimisticUpdateId
1154
- )
1158
+ optimisticUpdates: updatedOptimisticUpdates
1155
1159
  };
1156
1160
  });
1157
1161
  },
@@ -1175,6 +1179,7 @@ function createRoomContext(client, options) {
1175
1179
  return React2.useCallback(
1176
1180
  ({ threadId, commentId, emoji }) => {
1177
1181
  const userId = getCurrentUserId(room);
1182
+ const removedAt = /* @__PURE__ */ new Date();
1178
1183
  const optimisticUpdateId = _nanoid.nanoid.call(void 0, );
1179
1184
  store.pushOptimisticUpdate({
1180
1185
  type: "remove-reaction",
@@ -1182,67 +1187,35 @@ function createRoomContext(client, options) {
1182
1187
  commentId,
1183
1188
  emoji,
1184
1189
  userId,
1190
+ removedAt,
1185
1191
  id: optimisticUpdateId
1186
1192
  });
1187
- room.removeReaction({ threadId, commentId, emoji }).then(
1193
+ room[_core.kInternal].comments.removeReaction({ threadId, commentId, emoji }).then(
1188
1194
  () => {
1189
1195
  store.set((state) => {
1190
1196
  const existingThread = state.threads[threadId];
1197
+ const updatedOptimisticUpdates = state.optimisticUpdates.filter(
1198
+ (update) => update.id !== optimisticUpdateId
1199
+ );
1191
1200
  if (existingThread === void 0) {
1192
1201
  return {
1193
1202
  ...state,
1194
- optimisticUpdates: state.optimisticUpdates.filter(
1195
- (update) => update.id !== optimisticUpdateId
1196
- )
1203
+ optimisticUpdates: updatedOptimisticUpdates
1197
1204
  };
1198
1205
  }
1199
1206
  return {
1200
1207
  ...state,
1201
1208
  threads: {
1202
1209
  ...state.threads,
1203
- [threadId]: {
1204
- ...existingThread,
1205
- comments: existingThread.comments.map((comment) => {
1206
- if (comment.id !== commentId) {
1207
- return comment;
1208
- }
1209
- const existingReaction = comment.reactions.find(
1210
- (reaction) => reaction.emoji === emoji
1211
- );
1212
- if (existingReaction === void 0) {
1213
- return comment;
1214
- }
1215
- const reactions = comment.reactions;
1216
- if (!existingReaction.users.some(
1217
- (user) => user.id === userId
1218
- )) {
1219
- return comment;
1220
- }
1221
- if (existingReaction.users.length <= 1) {
1222
- return {
1223
- ...comment,
1224
- reactions: reactions.filter(
1225
- (reaction) => reaction.emoji !== emoji
1226
- )
1227
- };
1228
- }
1229
- return {
1230
- ...comment,
1231
- reactions: reactions.map(
1232
- (reaction) => reaction.emoji !== emoji ? reaction : {
1233
- ...reaction,
1234
- users: reaction.users.filter(
1235
- (user) => user.id !== userId
1236
- )
1237
- }
1238
- )
1239
- };
1240
- })
1241
- }
1210
+ [threadId]: _core.removeReaction.call(void 0,
1211
+ existingThread,
1212
+ commentId,
1213
+ emoji,
1214
+ userId,
1215
+ removedAt
1216
+ )
1242
1217
  },
1243
- optimisticUpdates: state.optimisticUpdates.filter(
1244
- (update) => update.id !== optimisticUpdateId
1245
- )
1218
+ optimisticUpdates: updatedOptimisticUpdates
1246
1219
  };
1247
1220
  });
1248
1221
  },
@@ -1266,44 +1239,58 @@ function createRoomContext(client, options) {
1266
1239
  return React2.useCallback(
1267
1240
  ({ threadId, body }) => {
1268
1241
  const commentId = createCommentId();
1269
- const now = /* @__PURE__ */ new Date();
1242
+ const createdAt = /* @__PURE__ */ new Date();
1270
1243
  const comment = {
1271
1244
  id: commentId,
1272
1245
  threadId,
1273
1246
  roomId: room.id,
1274
1247
  type: "comment",
1275
- createdAt: now,
1248
+ createdAt,
1276
1249
  userId: getCurrentUserId(room),
1277
1250
  body,
1278
1251
  reactions: []
1279
1252
  };
1280
1253
  const optimisticUpdateId = _nanoid.nanoid.call(void 0, );
1281
- const inboxNotification = Object.values(
1282
- store.get().inboxNotifications
1283
- ).find((inboxNotification2) => inboxNotification2.threadId === threadId);
1284
1254
  store.pushOptimisticUpdate({
1285
1255
  type: "create-comment",
1286
1256
  comment,
1287
- id: optimisticUpdateId,
1288
- inboxNotificationId: _optionalChain([inboxNotification, 'optionalAccess', _6 => _6.id])
1257
+ id: optimisticUpdateId
1289
1258
  });
1290
- room.createComment({ threadId, commentId, body }).then(
1259
+ room[_core.kInternal].comments.createComment({ threadId, commentId, body }).then(
1291
1260
  (newComment) => {
1292
- store.set((state) => ({
1293
- ...state,
1294
- threads: upsertComment(state.threads, newComment),
1295
- inboxNotifications: inboxNotification ? {
1261
+ store.set((state) => {
1262
+ const existingThread = state.threads[threadId];
1263
+ const updatedOptimisticUpdates = state.optimisticUpdates.filter(
1264
+ (update) => update.id !== optimisticUpdateId
1265
+ );
1266
+ if (existingThread === void 0) {
1267
+ return {
1268
+ ...state,
1269
+ optimisticUpdates: updatedOptimisticUpdates
1270
+ };
1271
+ }
1272
+ const inboxNotification = Object.values(
1273
+ state.inboxNotifications
1274
+ ).find((notification) => notification.threadId === threadId);
1275
+ const updatedInboxNotifications = inboxNotification !== void 0 ? {
1296
1276
  ...state.inboxNotifications,
1297
1277
  [inboxNotification.id]: {
1298
1278
  ...inboxNotification,
1299
1279
  notifiedAt: newComment.createdAt,
1300
1280
  readAt: newComment.createdAt
1301
1281
  }
1302
- } : state.inboxNotifications,
1303
- optimisticUpdates: state.optimisticUpdates.filter(
1304
- (update) => update.id !== optimisticUpdateId
1305
- )
1306
- }));
1282
+ } : state.inboxNotifications;
1283
+ return {
1284
+ ...state,
1285
+ threads: {
1286
+ ...state.threads,
1287
+ [threadId]: _core.upsertComment.call(void 0, existingThread, newComment)
1288
+ // Upsert the new comment into the thread comments list (if applicable)
1289
+ },
1290
+ inboxNotifications: updatedInboxNotifications,
1291
+ optimisticUpdates: updatedOptimisticUpdates
1292
+ };
1293
+ });
1307
1294
  },
1308
1295
  (err) => onMutationFailure(
1309
1296
  err,
@@ -1325,25 +1312,56 @@ function createRoomContext(client, options) {
1325
1312
  const room = useRoom();
1326
1313
  return React2.useCallback(
1327
1314
  ({ threadId, commentId, body }) => {
1328
- const now = /* @__PURE__ */ new Date();
1315
+ const editedAt = /* @__PURE__ */ new Date();
1329
1316
  const optimisticUpdateId = _nanoid.nanoid.call(void 0, );
1317
+ const thread = store.get().threads[threadId];
1318
+ if (thread === void 0) {
1319
+ _core.console.warn(
1320
+ `Internal unexpected behavior. Cannot edit comment in thread "${threadId}" because the thread does not exist in the cache.`
1321
+ );
1322
+ return;
1323
+ }
1324
+ const comment = thread.comments.find(
1325
+ (comment2) => comment2.id === commentId
1326
+ );
1327
+ if (comment === void 0 || comment.deletedAt !== void 0) {
1328
+ _core.console.warn(
1329
+ `Internal unexpected behavior. Cannot edit comment "${commentId}" in thread "${threadId}" because the comment does not exist in the cache.`
1330
+ );
1331
+ return;
1332
+ }
1330
1333
  store.pushOptimisticUpdate({
1331
1334
  type: "edit-comment",
1332
- threadId,
1333
- commentId,
1334
- body,
1335
- editedAt: now,
1335
+ comment: {
1336
+ ...comment,
1337
+ editedAt,
1338
+ body
1339
+ },
1336
1340
  id: optimisticUpdateId
1337
1341
  });
1338
- room.editComment({ threadId, commentId, body }).then(
1342
+ room[_core.kInternal].comments.editComment({ threadId, commentId, body }).then(
1339
1343
  (editedComment) => {
1340
- store.set((state) => ({
1341
- ...state,
1342
- threads: upsertComment(state.threads, editedComment),
1343
- optimisticUpdates: state.optimisticUpdates.filter(
1344
+ store.set((state) => {
1345
+ const existingThread = state.threads[threadId];
1346
+ const updatedOptimisticUpdates = state.optimisticUpdates.filter(
1344
1347
  (update) => update.id !== optimisticUpdateId
1345
- )
1346
- }));
1348
+ );
1349
+ if (existingThread === void 0) {
1350
+ return {
1351
+ ...state,
1352
+ optimisticUpdates: updatedOptimisticUpdates
1353
+ };
1354
+ }
1355
+ return {
1356
+ ...state,
1357
+ threads: {
1358
+ ...state.threads,
1359
+ [threadId]: _core.upsertComment.call(void 0, existingThread, editedComment)
1360
+ // Upsert the edited comment into the thread comments list (if applicable)
1361
+ },
1362
+ optimisticUpdates: updatedOptimisticUpdates
1363
+ };
1364
+ });
1347
1365
  },
1348
1366
  (err) => onMutationFailure(
1349
1367
  err,
@@ -1364,49 +1382,39 @@ function createRoomContext(client, options) {
1364
1382
  const room = useRoom();
1365
1383
  return React2.useCallback(
1366
1384
  ({ threadId, commentId }) => {
1367
- const now = /* @__PURE__ */ new Date();
1385
+ const deletedAt = /* @__PURE__ */ new Date();
1368
1386
  const optimisticUpdateId = _nanoid.nanoid.call(void 0, );
1369
1387
  store.pushOptimisticUpdate({
1370
1388
  type: "delete-comment",
1371
1389
  threadId,
1372
1390
  commentId,
1373
- deletedAt: now,
1391
+ deletedAt,
1374
1392
  id: optimisticUpdateId
1375
1393
  });
1376
- room.deleteComment({ threadId, commentId }).then(
1394
+ room[_core.kInternal].comments.deleteComment({ threadId, commentId }).then(
1377
1395
  () => {
1378
1396
  store.set((state) => {
1379
1397
  const existingThread = state.threads[threadId];
1398
+ const updatedOptimisticUpdates = state.optimisticUpdates.filter(
1399
+ (update) => update.id !== optimisticUpdateId
1400
+ );
1380
1401
  if (existingThread === void 0) {
1381
1402
  return {
1382
1403
  ...state,
1383
- optimisticUpdates: state.optimisticUpdates.filter(
1384
- (update) => update.id !== optimisticUpdateId
1385
- )
1404
+ optimisticUpdates: updatedOptimisticUpdates
1386
1405
  };
1387
1406
  }
1388
- const newThread = {
1389
- ...existingThread,
1390
- comments: existingThread.comments.map(
1391
- (comment) => comment.id === commentId ? {
1392
- ...comment,
1393
- deletedAt: now,
1394
- body: void 0
1395
- } : comment
1396
- )
1397
- };
1398
- const newThreads = { ...state.threads, [threadId]: newThread };
1399
- if (!newThread.comments.some(
1400
- (comment) => comment.deletedAt === void 0
1401
- )) {
1402
- delete newThreads[threadId];
1403
- }
1404
1407
  return {
1405
1408
  ...state,
1406
- threads: newThreads,
1407
- optimisticUpdates: state.optimisticUpdates.filter(
1408
- (update) => update.id !== optimisticUpdateId
1409
- )
1409
+ threads: {
1410
+ ...state.threads,
1411
+ [threadId]: _core.deleteComment.call(void 0,
1412
+ existingThread,
1413
+ commentId,
1414
+ deletedAt
1415
+ )
1416
+ },
1417
+ optimisticUpdates: updatedOptimisticUpdates
1410
1418
  };
1411
1419
  });
1412
1420
  },
@@ -1454,7 +1462,7 @@ function createRoomContext(client, options) {
1454
1462
  );
1455
1463
  }
1456
1464
  } catch (error) {
1457
- _core.console.error(_optionalChain([error, 'optionalAccess', _7 => _7.message]));
1465
+ _core.console.error(_optionalChain([error, 'optionalAccess', _6 => _6.message]));
1458
1466
  }
1459
1467
  };
1460
1468
  if (mentionSuggestionsCache.has(mentionSuggestionsCacheKey)) {
@@ -1502,48 +1510,52 @@ function createRoomContext(client, options) {
1502
1510
  );
1503
1511
  }
1504
1512
  function useMarkThreadAsRead() {
1505
- return React2.useCallback((threadId) => {
1506
- const inboxNotification = Object.values(
1507
- store.get().inboxNotifications
1508
- ).find((inboxNotification2) => inboxNotification2.threadId === threadId);
1509
- if (!inboxNotification)
1510
- return;
1511
- const optimisticUpdateId = _nanoid.nanoid.call(void 0, );
1512
- const now = /* @__PURE__ */ new Date();
1513
- store.pushOptimisticUpdate({
1514
- type: "mark-inbox-notification-as-read",
1515
- id: optimisticUpdateId,
1516
- inboxNotificationId: inboxNotification.id,
1517
- readAt: now
1518
- });
1519
- client.markInboxNotificationAsRead(inboxNotification.id).then(
1520
- () => {
1521
- store.set((state) => ({
1522
- ...state,
1523
- inboxNotifications: {
1524
- ...state.inboxNotifications,
1525
- [inboxNotification.id]: {
1526
- ...inboxNotification,
1527
- readAt: now
1528
- }
1529
- },
1530
- optimisticUpdates: state.optimisticUpdates.filter(
1531
- (update) => update.id !== optimisticUpdateId
1532
- )
1533
- }));
1534
- },
1535
- (err) => {
1536
- onMutationFailure(
1537
- err,
1538
- optimisticUpdateId,
1539
- (error) => new MarkInboxNotificationAsReadError(error, {
1540
- inboxNotificationId: inboxNotification.id
1541
- })
1542
- );
1513
+ const room = useRoom();
1514
+ return React2.useCallback(
1515
+ (threadId) => {
1516
+ const inboxNotification = Object.values(
1517
+ store.get().inboxNotifications
1518
+ ).find((inboxNotification2) => inboxNotification2.threadId === threadId);
1519
+ if (!inboxNotification)
1543
1520
  return;
1544
- }
1545
- );
1546
- }, []);
1521
+ const optimisticUpdateId = _nanoid.nanoid.call(void 0, );
1522
+ const now = /* @__PURE__ */ new Date();
1523
+ store.pushOptimisticUpdate({
1524
+ type: "mark-inbox-notification-as-read",
1525
+ id: optimisticUpdateId,
1526
+ inboxNotificationId: inboxNotification.id,
1527
+ readAt: now
1528
+ });
1529
+ room[_core.kInternal].notifications.markInboxNotificationAsRead(inboxNotification.id).then(
1530
+ () => {
1531
+ store.set((state) => ({
1532
+ ...state,
1533
+ inboxNotifications: {
1534
+ ...state.inboxNotifications,
1535
+ [inboxNotification.id]: {
1536
+ ...inboxNotification,
1537
+ readAt: now
1538
+ }
1539
+ },
1540
+ optimisticUpdates: state.optimisticUpdates.filter(
1541
+ (update) => update.id !== optimisticUpdateId
1542
+ )
1543
+ }));
1544
+ },
1545
+ (err) => {
1546
+ onMutationFailure(
1547
+ err,
1548
+ optimisticUpdateId,
1549
+ (error) => new MarkInboxNotificationAsReadError(error, {
1550
+ inboxNotificationId: inboxNotification.id
1551
+ })
1552
+ );
1553
+ return;
1554
+ }
1555
+ );
1556
+ },
1557
+ [room]
1558
+ );
1547
1559
  }
1548
1560
  function makeNotificationSettingsQueryKey(roomId) {
1549
1561
  return `${roomId}:NOTIFICATION_SETTINGS`;
@@ -1553,7 +1565,7 @@ function createRoomContext(client, options) {
1553
1565
  if (requestInfo !== void 0) {
1554
1566
  return requestInfo.promise;
1555
1567
  }
1556
- const promise = room.getRoomNotificationSettings();
1568
+ const promise = room[_core.kInternal].notifications.getRoomNotificationSettings();
1557
1569
  requestsCache.set(queryKey, {
1558
1570
  promise,
1559
1571
  subscribers: 0
@@ -1653,7 +1665,7 @@ function createRoomContext(client, options) {
1653
1665
  roomId: room.id,
1654
1666
  settings
1655
1667
  });
1656
- room.updateRoomNotificationSettings(settings).then(
1668
+ room[_core.kInternal].notifications.updateRoomNotificationSettings(settings).then(
1657
1669
  (settings2) => {
1658
1670
  store.set((state) => ({
1659
1671
  ...state,
@@ -1787,7 +1799,7 @@ function getCurrentUserId(room) {
1787
1799
  }
1788
1800
  function handleApiError(err) {
1789
1801
  const message = `Request failed with status ${err.status}: ${err.message}`;
1790
- if (_optionalChain([err, 'access', _8 => _8.details, 'optionalAccess', _9 => _9.error]) === "FORBIDDEN") {
1802
+ if (_optionalChain([err, 'access', _7 => _7.details, 'optionalAccess', _8 => _8.error]) === "FORBIDDEN") {
1791
1803
  const detailedMessage = [message, err.details.suggestion, err.details.docs].filter(Boolean).join("\n");
1792
1804
  _core.console.error(detailedMessage);
1793
1805
  }
@@ -1851,7 +1863,7 @@ function createSharedContext(client) {
1851
1863
  );
1852
1864
  return {
1853
1865
  ...state,
1854
- user: _optionalChain([state, 'optionalAccess', _10 => _10.data])
1866
+ user: _optionalChain([state, 'optionalAccess', _9 => _9.data])
1855
1867
  };
1856
1868
  }
1857
1869
  function useRoomInfo(roomId) {
@@ -1891,7 +1903,7 @@ function createSharedContext(client) {
1891
1903
  );
1892
1904
  return {
1893
1905
  ...state,
1894
- info: _optionalChain([state, 'optionalAccess', _11 => _11.data])
1906
+ info: _optionalChain([state, 'optionalAccess', _10 => _10.data])
1895
1907
  };
1896
1908
  }
1897
1909
  const bundle = {
@@ -1919,6 +1931,7 @@ var INBOX_NOTIFICATIONS_QUERY = "INBOX_NOTIFICATIONS";
1919
1931
  function createLiveblocksContext(client) {
1920
1932
  const shared = createSharedContext(client);
1921
1933
  const store = client[_core.kInternal].cacheStore;
1934
+ const notifications = client[_core.kInternal].notifications;
1922
1935
  function LiveblocksProvider(props) {
1923
1936
  return /* @__PURE__ */ React.default.createElement(
1924
1937
  ContextBundle2.Provider,
@@ -1933,7 +1946,7 @@ function createLiveblocksContext(client) {
1933
1946
  let lastRequestedAt;
1934
1947
  const poller = _core.makePoller.call(void 0, refreshThreadsAndNotifications);
1935
1948
  function refreshThreadsAndNotifications() {
1936
- return client.getInboxNotifications({ since: lastRequestedAt }).then(
1949
+ return notifications.getInboxNotifications({ since: lastRequestedAt }).then(
1937
1950
  (result) => {
1938
1951
  lastRequestedAt = result.meta.requestedAt;
1939
1952
  store.updateThreadsAndNotifications(
@@ -1972,7 +1985,7 @@ function createLiveblocksContext(client) {
1972
1985
  isLoading: true
1973
1986
  });
1974
1987
  try {
1975
- fetchInboxNotificationsRequest = client.getInboxNotifications();
1988
+ fetchInboxNotificationsRequest = notifications.getInboxNotifications();
1976
1989
  const result = await fetchInboxNotificationsRequest;
1977
1990
  store.updateThreadsAndNotifications(
1978
1991
  result.threads,
@@ -2122,7 +2135,7 @@ function createLiveblocksContext(client) {
2122
2135
  inboxNotificationId,
2123
2136
  readAt
2124
2137
  });
2125
- client.markInboxNotificationAsRead(inboxNotificationId).then(
2138
+ notifications.markInboxNotificationAsRead(inboxNotificationId).then(
2126
2139
  () => {
2127
2140
  store.set((state) => {
2128
2141
  const existingNotification = state.inboxNotifications[inboxNotificationId];
@@ -2169,7 +2182,7 @@ function createLiveblocksContext(client) {
2169
2182
  id: optimisticUpdateId,
2170
2183
  readAt
2171
2184
  });
2172
- client.markAllInboxNotificationsAsRead().then(
2185
+ notifications.markAllInboxNotificationsAsRead().then(
2173
2186
  () => {
2174
2187
  store.set((state) => ({
2175
2188
  ...state,