@poolse/react 2.25.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]);
@@ -1613,6 +1616,85 @@ function useCalls(userId, opts = {}) {
1613
1616
  reset
1614
1617
  };
1615
1618
  }
1619
+ function useLiveCall(conversationId) {
1620
+ const chat = usePoolse();
1621
+ const [call, setCall] = react.useState(null);
1622
+ const [participantIds, setParticipantIds] = react.useState([]);
1623
+ const callIdRef = react.useRef(null);
1624
+ const seq = react.useRef(0);
1625
+ const apply = react.useCallback((next) => {
1626
+ callIdRef.current = next?.id ?? null;
1627
+ setCall(next);
1628
+ setParticipantIds(next ? next.participants.map((p) => p.user_id) : []);
1629
+ }, []);
1630
+ const refresh = react.useCallback(() => {
1631
+ if (!conversationId) return;
1632
+ const mine = ++seq.current;
1633
+ chat.liveCalls.get(conversationId).then((c) => {
1634
+ if (mine === seq.current) apply(c);
1635
+ }).catch(() => {
1636
+ });
1637
+ }, [chat, conversationId, apply]);
1638
+ react.useEffect(() => {
1639
+ apply(null);
1640
+ if (!conversationId) return;
1641
+ refresh();
1642
+ const conv = chat.realtime.conversation(conversationId);
1643
+ const offStarted = conv.onCallStarted((evt) => {
1644
+ seq.current++;
1645
+ apply(evt.call);
1646
+ });
1647
+ const offParticipants = conv.onCallParticipants((evt) => {
1648
+ if (evt.call_id === callIdRef.current) setParticipantIds(evt.participants);
1649
+ else refresh();
1650
+ });
1651
+ const offFinished = conv.onCallFinished((evt) => {
1652
+ if (evt.call_id !== callIdRef.current) return;
1653
+ seq.current++;
1654
+ apply(null);
1655
+ });
1656
+ let wasConnected = chat.realtime.getStatus() === "connected";
1657
+ const offStatus = chat.realtime.onStatus((status) => {
1658
+ if (status === "connected" && !wasConnected) refresh();
1659
+ wasConnected = status === "connected";
1660
+ });
1661
+ return () => {
1662
+ offStarted();
1663
+ offParticipants();
1664
+ offFinished();
1665
+ offStatus();
1666
+ };
1667
+ }, [chat, conversationId, apply, refresh]);
1668
+ return { call, participantIds, refresh };
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
+ }
1616
1698
 
1617
1699
  Object.defineProperty(exports, "ApiError", {
1618
1700
  enumerable: true,
@@ -1666,9 +1748,12 @@ Object.defineProperty(exports, "parseCallLogBody", {
1666
1748
  enumerable: true,
1667
1749
  get: function () { return sdk.parseCallLogBody; }
1668
1750
  });
1751
+ exports.EDIT_WINDOW_MS = EDIT_WINDOW_MS;
1669
1752
  exports.PoolseProvider = PoolseProvider;
1753
+ exports.canEditOwnMessage = canEditOwnMessage;
1670
1754
  exports.computeGroupPosition = computeGroupPosition;
1671
1755
  exports.formatDayLabel = formatDayLabel;
1756
+ exports.receiptsOf = receiptsOf;
1672
1757
  exports.sameDay = sameDay;
1673
1758
  exports.sameGroup = sameGroup;
1674
1759
  exports.useAttachmentUpload = useAttachmentUpload;
@@ -1677,12 +1762,14 @@ exports.useCallRoom = useCallRoom;
1677
1762
  exports.useCalls = useCalls;
1678
1763
  exports.useConversation = useConversation;
1679
1764
  exports.useConversations = useConversations;
1765
+ exports.useLiveCall = useLiveCall;
1680
1766
  exports.useMe = useMe;
1681
1767
  exports.useMembers = useMembers;
1682
1768
  exports.useMessages = useMessages;
1683
1769
  exports.usePoolse = usePoolse;
1684
1770
  exports.usePresence = usePresence;
1685
1771
  exports.useReactions = useReactions;
1772
+ exports.useReadReceipts = useReadReceipts;
1686
1773
  exports.useRealtimeStatus = useRealtimeStatus;
1687
1774
  exports.useThread = useThread;
1688
1775
  exports.useTyping = useTyping;