@liveblocks/react 2.7.0-beta2 → 2.7.0-versions2

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.
@@ -1,6 +1,6 @@
1
1
  // src/version.ts
2
2
  var PKG_NAME = "@liveblocks/react";
3
- var PKG_VERSION = "2.7.0-beta2";
3
+ var PKG_VERSION = "2.7.0-versions2";
4
4
  var PKG_FORMAT = "esm";
5
5
 
6
6
  // src/ClientSideSuspense.tsx
@@ -174,7 +174,6 @@ var UpdateNotificationSettingsError = class extends Error {
174
174
  import { shallow as shallow2 } from "@liveblocks/client";
175
175
  import {
176
176
  addReaction,
177
- assert as assert2,
178
177
  CommentsApiError,
179
178
  console as console2,
180
179
  createCommentId,
@@ -1324,6 +1323,40 @@ function makeExtrasForClient2(client) {
1324
1323
  return;
1325
1324
  }
1326
1325
  }
1326
+ async function getRoomVersions(room, { retryCount } = { retryCount: 0 }) {
1327
+ const queryKey = getVersionsQueryKey(room.id);
1328
+ const existingRequest = requestsByQuery.get(queryKey);
1329
+ if (existingRequest !== void 0) return existingRequest;
1330
+ const request = room[kInternal2].listTextVersions();
1331
+ requestsByQuery.set(queryKey, request);
1332
+ store.setQueryState(queryKey, {
1333
+ isLoading: true
1334
+ });
1335
+ try {
1336
+ const result = await request;
1337
+ const data = await result.json();
1338
+ const versions = data.versions.map(({ createdAt, ...version2 }) => {
1339
+ return {
1340
+ createdAt: new Date(createdAt),
1341
+ ...version2
1342
+ };
1343
+ });
1344
+ store.updateRoomVersions(room.id, versions, queryKey);
1345
+ requestsByQuery.delete(queryKey);
1346
+ } catch (err) {
1347
+ requestsByQuery.delete(queryKey);
1348
+ retryError(() => {
1349
+ void getRoomVersions(room, {
1350
+ retryCount: retryCount + 1
1351
+ });
1352
+ }, retryCount);
1353
+ store.setQueryState(queryKey, {
1354
+ isLoading: false,
1355
+ error: err
1356
+ });
1357
+ }
1358
+ return;
1359
+ }
1327
1360
  async function getThreadsAndInboxNotifications(room, queryKey, options, { retryCount } = { retryCount: 0 }) {
1328
1361
  const existingRequest = requestsByQuery.get(queryKey);
1329
1362
  if (existingRequest !== void 0) return existingRequest;
@@ -1412,6 +1445,7 @@ function makeExtrasForClient2(client) {
1412
1445
  getThreadsUpdates,
1413
1446
  getThreadsAndInboxNotifications,
1414
1447
  getInboxNotificationSettings,
1448
+ getRoomVersions,
1415
1449
  onMutationFailure
1416
1450
  };
1417
1451
  }
@@ -1461,7 +1495,8 @@ function makeRoomContextBundle(client) {
1461
1495
  useRemoveReaction,
1462
1496
  useMarkThreadAsRead,
1463
1497
  useThreadSubscription,
1464
- useAttachmentUrl,
1498
+ useHistoryVersions,
1499
+ useHistoryVersionData,
1465
1500
  useRoomNotificationSettings,
1466
1501
  useUpdateRoomNotificationSettings,
1467
1502
  ...shared.classic,
@@ -1505,7 +1540,8 @@ function makeRoomContextBundle(client) {
1505
1540
  useRemoveReaction,
1506
1541
  useMarkThreadAsRead,
1507
1542
  useThreadSubscription,
1508
- useAttachmentUrl: useAttachmentUrlSuspense,
1543
+ // TODO: useHistoryVersionData: useHistoryVersionDataSuspense,
1544
+ useHistoryVersions: useHistoryVersionsSuspense,
1509
1545
  useRoomNotificationSettings: useRoomNotificationSettingsSuspense,
1510
1546
  useUpdateRoomNotificationSettings,
1511
1547
  ...shared.suspense
@@ -1970,7 +2006,6 @@ function useCreateThread() {
1970
2006
  (options) => {
1971
2007
  const body = options.body;
1972
2008
  const metadata = options.metadata ?? {};
1973
- const attachments = options.attachments;
1974
2009
  const threadId = createThreadId();
1975
2010
  const commentId = createCommentId();
1976
2011
  const createdAt = /* @__PURE__ */ new Date();
@@ -1982,8 +2017,7 @@ function useCreateThread() {
1982
2017
  type: "comment",
1983
2018
  userId: getCurrentUserId(room),
1984
2019
  body,
1985
- reactions: [],
1986
- attachments: attachments ?? []
2020
+ reactions: []
1987
2021
  };
1988
2022
  const newThread = {
1989
2023
  id: threadId,
@@ -2003,8 +2037,7 @@ function useCreateThread() {
2003
2037
  id: optimisticUpdateId,
2004
2038
  roomId: room.id
2005
2039
  });
2006
- const attachmentIds = attachments?.map((attachment) => attachment.id);
2007
- room.createThread({ threadId, commentId, body, metadata, attachmentIds }).then(
2040
+ room.createThread({ threadId, commentId, body, metadata }).then(
2008
2041
  (thread) => {
2009
2042
  store.set((state) => ({
2010
2043
  ...state,
@@ -2162,7 +2195,7 @@ function useCreateComment() {
2162
2195
  const client = useClient();
2163
2196
  const room = useRoom();
2164
2197
  return React4.useCallback(
2165
- ({ threadId, body, attachments }) => {
2198
+ ({ threadId, body }) => {
2166
2199
  const commentId = createCommentId();
2167
2200
  const createdAt = /* @__PURE__ */ new Date();
2168
2201
  const comment = {
@@ -2173,8 +2206,7 @@ function useCreateComment() {
2173
2206
  createdAt,
2174
2207
  userId: getCurrentUserId(room),
2175
2208
  body,
2176
- reactions: [],
2177
- attachments: attachments ?? []
2209
+ reactions: []
2178
2210
  };
2179
2211
  const optimisticUpdateId = nanoid2();
2180
2212
  const { store, onMutationFailure } = getExtrasForClient2(client);
@@ -2183,8 +2215,7 @@ function useCreateComment() {
2183
2215
  comment,
2184
2216
  id: optimisticUpdateId
2185
2217
  });
2186
- const attachmentIds = attachments?.map((attachment) => attachment.id);
2187
- room.createComment({ threadId, commentId, body, attachmentIds }).then(
2218
+ room.createComment({ threadId, commentId, body }).then(
2188
2219
  (newComment) => {
2189
2220
  store.set((state) => {
2190
2221
  const existingThread = state.threads[threadId];
@@ -2242,7 +2273,7 @@ function useEditComment() {
2242
2273
  const client = useClient();
2243
2274
  const room = useRoom();
2244
2275
  return React4.useCallback(
2245
- ({ threadId, commentId, body, attachments }) => {
2276
+ ({ threadId, commentId, body }) => {
2246
2277
  const editedAt = /* @__PURE__ */ new Date();
2247
2278
  const optimisticUpdateId = nanoid2();
2248
2279
  const { store, onMutationFailure } = getExtrasForClient2(client);
@@ -2267,13 +2298,11 @@ function useEditComment() {
2267
2298
  comment: {
2268
2299
  ...comment,
2269
2300
  editedAt,
2270
- body,
2271
- attachments: attachments ?? []
2301
+ body
2272
2302
  },
2273
2303
  id: optimisticUpdateId
2274
2304
  });
2275
- const attachmentIds = attachments?.map((attachment) => attachment.id);
2276
- room.editComment({ threadId, commentId, body, attachmentIds }).then(
2305
+ room.editComment({ threadId, commentId, body }).then(
2277
2306
  (editedComment) => {
2278
2307
  store.set((state) => {
2279
2308
  const existingThread = state.threads[threadId];
@@ -2734,6 +2763,68 @@ function useRoomNotificationSettings() {
2734
2763
  return [settings, updateRoomNotificationSettings];
2735
2764
  }, [settings, updateRoomNotificationSettings]);
2736
2765
  }
2766
+ function useHistoryVersionData(versionId) {
2767
+ const [state, setState] = React4.useState({
2768
+ isLoading: true
2769
+ });
2770
+ const room = useRoom();
2771
+ React4.useEffect(() => {
2772
+ setState({ isLoading: true });
2773
+ const load = async () => {
2774
+ try {
2775
+ const response = await room[kInternal2].getTextVersion(versionId);
2776
+ const buffer = await response.arrayBuffer();
2777
+ const data = new Uint8Array(buffer);
2778
+ setState({
2779
+ isLoading: false,
2780
+ data
2781
+ });
2782
+ } catch (error) {
2783
+ setState({
2784
+ isLoading: false,
2785
+ error: error instanceof Error ? error : new Error(
2786
+ "An unknown error occurred while loading this version"
2787
+ )
2788
+ });
2789
+ }
2790
+ };
2791
+ void load();
2792
+ }, [room, versionId]);
2793
+ return state;
2794
+ }
2795
+ function useHistoryVersions() {
2796
+ const client = useClient();
2797
+ const room = useRoom();
2798
+ const queryKey = getVersionsQueryKey(room.id);
2799
+ const { store, getRoomVersions } = getExtrasForClient2(client);
2800
+ React4.useEffect(() => {
2801
+ void getRoomVersions(room);
2802
+ }, [room]);
2803
+ const selector = React4.useCallback(
2804
+ (state2) => {
2805
+ const query = state2.queries[queryKey];
2806
+ if (query === void 0 || query.isLoading) {
2807
+ return {
2808
+ isLoading: true
2809
+ };
2810
+ }
2811
+ return {
2812
+ versions: state2.versions[room.id],
2813
+ isLoading: false,
2814
+ error: query.error
2815
+ };
2816
+ },
2817
+ [room, queryKey]
2818
+ // eslint-disable-line react-hooks/exhaustive-deps
2819
+ );
2820
+ const state = useSyncExternalStoreWithSelector2(
2821
+ store.subscribe,
2822
+ store.get,
2823
+ store.get,
2824
+ selector
2825
+ );
2826
+ return state;
2827
+ }
2737
2828
  function useUpdateRoomNotificationSettings() {
2738
2829
  const client = useClient();
2739
2830
  const room = useRoom();
@@ -2866,64 +2957,35 @@ function useThreadsSuspense(options = {
2866
2957
  useScrollToCommentOnLoadEffect(scrollOnLoad, state);
2867
2958
  return state;
2868
2959
  }
2869
- function selectorFor_useAttachmentUrl(state) {
2870
- if (state === void 0 || state?.isLoading) {
2871
- return state ?? { isLoading: true };
2960
+ function useHistoryVersionsSuspense() {
2961
+ const client = useClient();
2962
+ const room = useRoom();
2963
+ const queryKey = getVersionsQueryKey(room.id);
2964
+ const { store, getRoomVersions } = getExtrasForClient2(client);
2965
+ const query = store.get().queries[queryKey];
2966
+ if (query === void 0 || query.isLoading) {
2967
+ throw getRoomVersions(room);
2872
2968
  }
2873
- if (state.error) {
2874
- return state;
2969
+ if (query.error) {
2970
+ throw query.error;
2875
2971
  }
2876
- assert2(state.data !== void 0, "Unexpected missing attachment URL");
2877
- return {
2878
- isLoading: false,
2879
- url: state.data
2880
- };
2881
- }
2882
- function useAttachmentUrl(attachmentId) {
2883
- const room = useRoom();
2884
- const { attachmentUrlsStore } = room[kInternal2];
2885
- const getAttachmentUrlState = React4.useCallback(
2886
- () => attachmentUrlsStore.getState(attachmentId),
2887
- [attachmentUrlsStore, attachmentId]
2972
+ const selector = React4.useCallback(
2973
+ (state2) => {
2974
+ return {
2975
+ versions: state2.versions[room.id],
2976
+ isLoading: false
2977
+ };
2978
+ },
2979
+ [room, queryKey]
2980
+ // eslint-disable-line react-hooks/exhaustive-deps
2888
2981
  );
2889
- React4.useEffect(() => {
2890
- void attachmentUrlsStore.get(attachmentId);
2891
- }, [attachmentUrlsStore, attachmentId]);
2892
- return useSyncExternalStoreWithSelector2(
2893
- attachmentUrlsStore.subscribe,
2894
- getAttachmentUrlState,
2895
- getAttachmentUrlState,
2896
- selectorFor_useAttachmentUrl,
2897
- shallow2
2982
+ const state = useSyncExternalStoreWithSelector2(
2983
+ store.subscribe,
2984
+ store.get,
2985
+ store.get,
2986
+ selector
2898
2987
  );
2899
- }
2900
- function useAttachmentUrlSuspense(attachmentId) {
2901
- const room = useRoom();
2902
- const { attachmentUrlsStore } = room[kInternal2];
2903
- const getAttachmentUrlState = React4.useCallback(
2904
- () => attachmentUrlsStore.getState(attachmentId),
2905
- [attachmentUrlsStore, attachmentId]
2906
- );
2907
- const attachmentUrlState = getAttachmentUrlState();
2908
- if (!attachmentUrlState || attachmentUrlState.isLoading) {
2909
- throw attachmentUrlsStore.get(attachmentId);
2910
- }
2911
- if (attachmentUrlState.error) {
2912
- throw attachmentUrlState.error;
2913
- }
2914
- const state = useSyncExternalStore2(
2915
- attachmentUrlsStore.subscribe,
2916
- getAttachmentUrlState,
2917
- getAttachmentUrlState
2918
- );
2919
- assert2(state !== void 0, "Unexpected missing state");
2920
- assert2(!state.isLoading, "Unexpected loading state");
2921
- assert2(!state.error, "Unexpected error state");
2922
- return {
2923
- isLoading: false,
2924
- url: state.data,
2925
- error: void 0
2926
- };
2988
+ return state;
2927
2989
  }
2928
2990
  function useRoomNotificationSettingsSuspense() {
2929
2991
  const updateRoomNotificationSettings = useUpdateRoomNotificationSettings();
@@ -2966,6 +3028,9 @@ function createRoomContext(client) {
2966
3028
  function generateQueryKey(roomId, options) {
2967
3029
  return `${roomId}-${stringify(options ?? {})}`;
2968
3030
  }
3031
+ function getVersionsQueryKey(roomId) {
3032
+ return `${roomId}-VERSIONS`;
3033
+ }
2969
3034
  var _RoomProvider = RoomProvider;
2970
3035
  var _useBroadcastEvent = useBroadcastEvent;
2971
3036
  var _useOthersListener = useOthersListener;
@@ -2982,6 +3047,8 @@ var _useOthersMapped = useOthersMapped;
2982
3047
  var _useOthersMappedSuspense = useOthersMappedSuspense;
2983
3048
  var _useThreads = useThreads;
2984
3049
  var _useThreadsSuspense = useThreadsSuspense;
3050
+ var _useHistoryVersions = useHistoryVersions;
3051
+ var _useHistoryVersionsSuspense = useHistoryVersionsSuspense;
2985
3052
  var _useOther = useOther;
2986
3053
  function _useOthers(...args) {
2987
3054
  return useOthers(...args);
@@ -3030,11 +3097,10 @@ export {
3030
3097
  useMarkThreadAsUnresolved,
3031
3098
  useThreadSubscription,
3032
3099
  useRoomNotificationSettings,
3100
+ useHistoryVersionData,
3033
3101
  useUpdateRoomNotificationSettings,
3034
3102
  useOthersConnectionIdsSuspense,
3035
3103
  useStorageStatusSuspense,
3036
- useAttachmentUrl,
3037
- useAttachmentUrlSuspense,
3038
3104
  createRoomContext,
3039
3105
  _RoomProvider,
3040
3106
  _useBroadcastEvent,
@@ -3052,6 +3118,8 @@ export {
3052
3118
  _useOthersMappedSuspense,
3053
3119
  _useThreads,
3054
3120
  _useThreadsSuspense,
3121
+ _useHistoryVersions,
3122
+ _useHistoryVersionsSuspense,
3055
3123
  _useOther,
3056
3124
  _useOthers,
3057
3125
  _useOtherSuspense,
@@ -3082,4 +3150,4 @@ export {
3082
3150
  _useUserThreads_experimental,
3083
3151
  _useUserThreadsSuspense_experimental
3084
3152
  };
3085
- //# sourceMappingURL=chunk-ZEN5B7DF.mjs.map
3153
+ //# sourceMappingURL=chunk-HS6BFH3A.mjs.map