@poolse/react 2.26.0 → 2.27.0

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.cjs CHANGED
@@ -69,27 +69,30 @@ function usePoolse() {
69
69
  }
70
70
  function useMe() {
71
71
  const chat = usePoolse();
72
- const [state, setState] = react.useState({
73
- me: null,
74
- loading: true,
75
- error: null
72
+ const [state, setState] = react.useState(() => {
73
+ const known = chat.me.peek();
74
+ return { me: known, loading: known === null, error: null };
76
75
  });
77
76
  react.useEffect(() => {
78
77
  let cancelled = false;
79
78
  const controller = new AbortController();
79
+ const off = chat.me.onChange((me) => {
80
+ if (!cancelled) setState({ me, loading: false, error: null });
81
+ });
80
82
  chat.me.show(controller.signal).then((me) => {
81
83
  if (!cancelled) setState({ me, loading: false, error: null });
82
84
  }).catch((err) => {
83
85
  if (!cancelled) {
84
- setState({
85
- me: null,
86
+ setState((prev) => ({
87
+ me: prev.me,
86
88
  loading: false,
87
89
  error: err instanceof Error ? err : new Error(String(err))
88
- });
90
+ }));
89
91
  }
90
92
  });
91
93
  return () => {
92
94
  cancelled = true;
95
+ off();
93
96
  controller.abort();
94
97
  };
95
98
  }, [chat]);
@@ -1664,6 +1667,34 @@ function useLiveCall(conversationId) {
1664
1667
  }, [chat, conversationId, apply, refresh]);
1665
1668
  return { call, participantIds, refresh };
1666
1669
  }
1670
+ function useReadReceipts() {
1671
+ const chat = usePoolse();
1672
+ const { me } = useMe();
1673
+ const state = receiptsOf(me);
1674
+ const setShared = react.useCallback(
1675
+ async (share) => {
1676
+ await chat.me.update({ read_receipts: share });
1677
+ },
1678
+ [chat]
1679
+ );
1680
+ return { ...state, setShared };
1681
+ }
1682
+ function receiptsOf(me) {
1683
+ const r = me?.read_receipts;
1684
+ return {
1685
+ visible: r?.visible ?? true,
1686
+ mode: r?.mode ?? "on",
1687
+ shared: r?.shared ?? true,
1688
+ canChange: r?.can_change ?? false
1689
+ };
1690
+ }
1691
+ var EDIT_WINDOW_MS = 15 * 60 * 1e3;
1692
+ function canEditOwnMessage(args) {
1693
+ if (!args.isSelf || args.deleted) return false;
1694
+ if (args.receiptsVisible) return (args.readState ?? "sent") === "sent";
1695
+ const sent = Date.parse(args.insertedAt);
1696
+ return Number.isFinite(sent) && (args.now ?? Date.now()) - sent < EDIT_WINDOW_MS;
1697
+ }
1667
1698
 
1668
1699
  Object.defineProperty(exports, "ApiError", {
1669
1700
  enumerable: true,
@@ -1717,9 +1748,12 @@ Object.defineProperty(exports, "parseCallLogBody", {
1717
1748
  enumerable: true,
1718
1749
  get: function () { return sdk.parseCallLogBody; }
1719
1750
  });
1751
+ exports.EDIT_WINDOW_MS = EDIT_WINDOW_MS;
1720
1752
  exports.PoolseProvider = PoolseProvider;
1753
+ exports.canEditOwnMessage = canEditOwnMessage;
1721
1754
  exports.computeGroupPosition = computeGroupPosition;
1722
1755
  exports.formatDayLabel = formatDayLabel;
1756
+ exports.receiptsOf = receiptsOf;
1723
1757
  exports.sameDay = sameDay;
1724
1758
  exports.sameGroup = sameGroup;
1725
1759
  exports.useAttachmentUpload = useAttachmentUpload;
@@ -1735,6 +1769,7 @@ exports.useMessages = useMessages;
1735
1769
  exports.usePoolse = usePoolse;
1736
1770
  exports.usePresence = usePresence;
1737
1771
  exports.useReactions = useReactions;
1772
+ exports.useReadReceipts = useReadReceipts;
1738
1773
  exports.useRealtimeStatus = useRealtimeStatus;
1739
1774
  exports.useThread = useThread;
1740
1775
  exports.useTyping = useTyping;