@natoe/colab 0.1.14 → 0.1.16

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
@@ -57,6 +57,19 @@ function toCamelKey(key) {
57
57
  }
58
58
 
59
59
  // src/core/socket.ts
60
+ function normalizeUnreadPayload(payload) {
61
+ if (payload && typeof payload === "object") {
62
+ const obj = payload;
63
+ if (obj.conversation && typeof obj.conversation === "object") {
64
+ return {
65
+ conversation: obj.conversation,
66
+ order: obj.order && typeof obj.order === "object" ? obj.order : {}
67
+ };
68
+ }
69
+ return { conversation: payload, order: {} };
70
+ }
71
+ return { conversation: {}, order: {} };
72
+ }
60
73
  var CollabSocket = class {
61
74
  constructor() {
62
75
  this.socket = null;
@@ -122,7 +135,7 @@ var CollabSocket = class {
122
135
  if (!this.socket || !this.config) return;
123
136
  this.userChannel = this.socket.channel("user_notifications", {});
124
137
  this.userChannel.on("unread_update", (payload) => {
125
- this.onUnreadUpdate?.(payload);
138
+ this.onUnreadUpdate?.(normalizeUnreadPayload(payload));
126
139
  });
127
140
  this.userChannel.join().receive("ok", () => {
128
141
  }).receive("error", (reason) => {
@@ -133,7 +146,9 @@ var CollabSocket = class {
133
146
  });
134
147
  });
135
148
  }
136
- /** Register callback for unread count changes */
149
+ /** Register callback for unread count changes. The callback receives
150
+ * the normalised {@link UnreadCounts} shape regardless of which
151
+ * payload format the backend pushed. */
137
152
  onUnreadCountUpdate(callback) {
138
153
  this.onUnreadUpdate = callback;
139
154
  }
@@ -577,6 +592,7 @@ function CollabProvider({ config, apiBaseUrl, children }) {
577
592
  return new CollabSocket();
578
593
  });
579
594
  const [unreadCounts, setUnreadCounts] = useState({});
595
+ const [unreadCountsByOrder, setUnreadCountsByOrder] = useState({});
580
596
  const pendingOrderIds = useRef(/* @__PURE__ */ new Set());
581
597
  const pendingResolvers = useRef(/* @__PURE__ */ new Map());
582
598
  const previewCache = useRef(/* @__PURE__ */ new Map());
@@ -592,7 +608,8 @@ function CollabProvider({ config, apiBaseUrl, children }) {
592
608
  setSocket(s);
593
609
  }
594
610
  s.onUnreadCountUpdate((counts) => {
595
- setUnreadCounts(counts);
611
+ setUnreadCounts(counts.conversation);
612
+ setUnreadCountsByOrder(counts.order);
596
613
  previewCache.current.clear();
597
614
  });
598
615
  if (!s.isConnected()) {
@@ -800,6 +817,7 @@ function CollabProvider({ config, apiBaseUrl, children }) {
800
817
  apiBaseUrl,
801
818
  totalUnread,
802
819
  unreadCounts,
820
+ unreadCountsByOrder,
803
821
  requestPreview,
804
822
  invalidatePreview,
805
823
  fetchMessages,
@@ -6321,7 +6339,7 @@ function useUnreadCount() {
6321
6339
  const [counts, setCounts] = useState({});
6322
6340
  useEffect(() => {
6323
6341
  socket.onUnreadCountUpdate((serverCounts) => {
6324
- setCounts(serverCounts);
6342
+ setCounts(serverCounts.conversation);
6325
6343
  });
6326
6344
  }, [socket]);
6327
6345
  const getCountForConversation = useCallback(