@poolse/react 2.25.0 → 2.26.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
@@ -1613,6 +1613,57 @@ function useCalls(userId, opts = {}) {
1613
1613
  reset
1614
1614
  };
1615
1615
  }
1616
+ function useLiveCall(conversationId) {
1617
+ const chat = usePoolse();
1618
+ const [call, setCall] = react.useState(null);
1619
+ const [participantIds, setParticipantIds] = react.useState([]);
1620
+ const callIdRef = react.useRef(null);
1621
+ const seq = react.useRef(0);
1622
+ const apply = react.useCallback((next) => {
1623
+ callIdRef.current = next?.id ?? null;
1624
+ setCall(next);
1625
+ setParticipantIds(next ? next.participants.map((p) => p.user_id) : []);
1626
+ }, []);
1627
+ const refresh = react.useCallback(() => {
1628
+ if (!conversationId) return;
1629
+ const mine = ++seq.current;
1630
+ chat.liveCalls.get(conversationId).then((c) => {
1631
+ if (mine === seq.current) apply(c);
1632
+ }).catch(() => {
1633
+ });
1634
+ }, [chat, conversationId, apply]);
1635
+ react.useEffect(() => {
1636
+ apply(null);
1637
+ if (!conversationId) return;
1638
+ refresh();
1639
+ const conv = chat.realtime.conversation(conversationId);
1640
+ const offStarted = conv.onCallStarted((evt) => {
1641
+ seq.current++;
1642
+ apply(evt.call);
1643
+ });
1644
+ const offParticipants = conv.onCallParticipants((evt) => {
1645
+ if (evt.call_id === callIdRef.current) setParticipantIds(evt.participants);
1646
+ else refresh();
1647
+ });
1648
+ const offFinished = conv.onCallFinished((evt) => {
1649
+ if (evt.call_id !== callIdRef.current) return;
1650
+ seq.current++;
1651
+ apply(null);
1652
+ });
1653
+ let wasConnected = chat.realtime.getStatus() === "connected";
1654
+ const offStatus = chat.realtime.onStatus((status) => {
1655
+ if (status === "connected" && !wasConnected) refresh();
1656
+ wasConnected = status === "connected";
1657
+ });
1658
+ return () => {
1659
+ offStarted();
1660
+ offParticipants();
1661
+ offFinished();
1662
+ offStatus();
1663
+ };
1664
+ }, [chat, conversationId, apply, refresh]);
1665
+ return { call, participantIds, refresh };
1666
+ }
1616
1667
 
1617
1668
  Object.defineProperty(exports, "ApiError", {
1618
1669
  enumerable: true,
@@ -1677,6 +1728,7 @@ exports.useCallRoom = useCallRoom;
1677
1728
  exports.useCalls = useCalls;
1678
1729
  exports.useConversation = useConversation;
1679
1730
  exports.useConversations = useConversations;
1731
+ exports.useLiveCall = useLiveCall;
1680
1732
  exports.useMe = useMe;
1681
1733
  exports.useMembers = useMembers;
1682
1734
  exports.useMessages = useMessages;