@poolse/react 2.27.0 → 2.29.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.js CHANGED
@@ -1237,384 +1237,6 @@ function useCallRoom(conversationId, opts) {
1237
1237
  toggleMic
1238
1238
  };
1239
1239
  }
1240
- function useAloneWatch({ active, others, graceMs }) {
1241
- const [alone, setAlone] = useState(false);
1242
- const seenPeer = useRef(false);
1243
- useEffect(() => {
1244
- if (!active) {
1245
- seenPeer.current = false;
1246
- setAlone(false);
1247
- return;
1248
- }
1249
- if (others > 0) {
1250
- seenPeer.current = true;
1251
- return;
1252
- }
1253
- if (graceMs <= 0 || !seenPeer.current) return;
1254
- const timer = setTimeout(() => setAlone(true), graceMs);
1255
- return () => clearTimeout(timer);
1256
- }, [active, others, graceMs]);
1257
- return alone;
1258
- }
1259
-
1260
- // src/use-voice-room.ts
1261
- function useVoiceRoom(conversationId, opts = {}) {
1262
- const poolse = usePoolse();
1263
- const [status, setStatus] = useState("idle");
1264
- const [participants, setParticipants] = useState([]);
1265
- const [muted, setMuted] = useState(false);
1266
- const [error, setError] = useState(null);
1267
- const optsRef = useRef(opts);
1268
- const joined = useRef(false);
1269
- const held = useRef(null);
1270
- const room = useMemo(
1271
- () => conversationId ? poolse.realtime.voice(conversationId, optsRef.current) : null,
1272
- [poolse, conversationId]
1273
- );
1274
- useEffect(() => {
1275
- if (!room) return;
1276
- held.current = room;
1277
- setStatus(room.getStatus());
1278
- setParticipants(room.getParticipants());
1279
- setMuted(room.isMuted());
1280
- const offStatus = room.onStatus(setStatus);
1281
- const offRoster = room.onParticipants(setParticipants);
1282
- const offError = room.onError(setError);
1283
- return () => {
1284
- offStatus();
1285
- offRoster();
1286
- offError();
1287
- held.current = null;
1288
- if (joined.current) {
1289
- joined.current = false;
1290
- room.leave();
1291
- }
1292
- };
1293
- }, [room]);
1294
- const join = useCallback(async () => {
1295
- const target = room;
1296
- if (!target) return;
1297
- setError(null);
1298
- try {
1299
- await target.join();
1300
- if (held.current !== target) {
1301
- target.leave();
1302
- return;
1303
- }
1304
- joined.current = true;
1305
- } catch (err) {
1306
- setError(err instanceof Error ? err : new Error(String(err)));
1307
- }
1308
- }, [room]);
1309
- const leave = useCallback(() => {
1310
- if (!joined.current) return;
1311
- joined.current = false;
1312
- room?.leave();
1313
- }, [room]);
1314
- const toggleMute = useCallback(() => {
1315
- if (!room) return;
1316
- setMuted(room.toggleMute());
1317
- }, [room]);
1318
- const alone = useAloneWatch({
1319
- active: status === "connected",
1320
- others: Math.max(0, participants.length - 1),
1321
- graceMs: optsRef.current.aloneGraceMs ?? 1500
1322
- });
1323
- return { status, participants, muted, alone, error, join, leave, toggleMute };
1324
- }
1325
- function useCalls(userId, opts = {}) {
1326
- const ringTimeoutMs = opts.ringTimeoutMs ?? 45e3;
1327
- const poolse = usePoolse();
1328
- const [phase, setPhase] = useState("idle");
1329
- const phaseRef = useRef("idle");
1330
- phaseRef.current = phase;
1331
- const [incoming, setIncoming] = useState(null);
1332
- const [outgoing, setOutgoing] = useState(null);
1333
- const [activeConversationId, setActiveConversationId] = useState(null);
1334
- const [activeCallId, setActiveCallId] = useState(null);
1335
- const [activeMedia, setActiveMedia] = useState(null);
1336
- const [error, setError] = useState(null);
1337
- const incomingRef = useRef(null);
1338
- incomingRef.current = incoming;
1339
- const outgoingRef = useRef(null);
1340
- outgoingRef.current = outgoing;
1341
- const activeCallIdRef = useRef(null);
1342
- activeCallIdRef.current = activeCallId;
1343
- const isCurrent = useCallback((callId) => {
1344
- return callId === activeCallIdRef.current || callId === incomingRef.current?.callId || callId === outgoingRef.current?.callId;
1345
- }, []);
1346
- const rejectedRef = useRef(/* @__PURE__ */ new Map());
1347
- const pendingRef = useRef(null);
1348
- const actionsRef = useRef({
1349
- cancel: async () => {
1350
- },
1351
- decline: async () => {
1352
- }
1353
- });
1354
- const calls = useMemo(() => userId ? poolse.realtime.calls(userId) : null, [poolse, userId]);
1355
- const reject = useCallback((callId, userId2, kind) => {
1356
- if (phaseRef.current !== "ringing-out") return;
1357
- const out = outgoingRef.current;
1358
- if (!out || out.callId !== callId) return;
1359
- rejectedRef.current.set(userId2, kind);
1360
- const outstanding = out.calleeUserIds.filter((id) => !rejectedRef.current.has(id));
1361
- if (out.calleeUserIds.length > 0 && outstanding.length > 0) return;
1362
- const allBusy = [...rejectedRef.current.values()].every((k) => k === "busy");
1363
- setPhase(allBusy ? "busy" : "declined");
1364
- setOutgoing(null);
1365
- setActiveCallId(null);
1366
- setActiveMedia(null);
1367
- }, []);
1368
- useEffect(() => {
1369
- if (!calls) return;
1370
- const offIncoming = calls.onIncoming((call2) => {
1371
- if (call2.callId === incomingRef.current?.callId) return;
1372
- if (call2.callId === activeCallIdRef.current) return;
1373
- if (phaseRef.current !== "idle") {
1374
- void calls.markBusy(call2).catch(() => {
1375
- });
1376
- return;
1377
- }
1378
- setIncoming(call2);
1379
- setPhase("ringing-in");
1380
- });
1381
- const offAccepted = calls.onAccepted((call2) => {
1382
- if (outgoingRef.current && call2.callId !== outgoingRef.current.callId) return;
1383
- setPhase("active");
1384
- setActiveConversationId(call2.conversationId);
1385
- setActiveCallId(call2.callId);
1386
- });
1387
- const offDeclined = calls.onDeclined((call2) => {
1388
- if (!isCurrent(call2.callId)) return;
1389
- reject(call2.callId, call2.userId, "declined");
1390
- });
1391
- const offBusy = calls.onBusy((call2) => {
1392
- if (!isCurrent(call2.callId)) return;
1393
- reject(call2.callId, call2.userId, "busy");
1394
- });
1395
- const offEnded = calls.onEnded((call2) => {
1396
- if (!isCurrent(call2.callId)) return;
1397
- setPhase("idle");
1398
- setIncoming(null);
1399
- setOutgoing(null);
1400
- setActiveConversationId(null);
1401
- setActiveCallId(null);
1402
- setActiveMedia(null);
1403
- });
1404
- const offCancelled = calls.onCancelled((call2) => {
1405
- if (call2.callId !== incomingRef.current?.callId) return;
1406
- setIncoming(null);
1407
- setPhase("idle");
1408
- setActiveCallId(null);
1409
- setActiveMedia(null);
1410
- });
1411
- return () => {
1412
- offIncoming();
1413
- offAccepted();
1414
- offDeclined();
1415
- offBusy();
1416
- offEnded();
1417
- offCancelled();
1418
- };
1419
- }, [calls, isCurrent, reject]);
1420
- useEffect(() => {
1421
- if (!calls) return;
1422
- const queued = pendingRef.current;
1423
- if (!queued) return;
1424
- pendingRef.current = null;
1425
- const sent = queued.kind === "accept" ? calls.accept(queued.call) : calls.decline(queued.call);
1426
- void sent.catch((err) => {
1427
- setError(err instanceof Error ? err : new Error(String(err)));
1428
- });
1429
- }, [calls]);
1430
- const call = useCallback(
1431
- async (conversationId, media2 = "audio") => {
1432
- if (!calls) return;
1433
- setError(null);
1434
- rejectedRef.current = /* @__PURE__ */ new Map();
1435
- try {
1436
- const placed = await calls.invite(conversationId, media2);
1437
- setOutgoing(placed);
1438
- setActiveCallId(placed.callId);
1439
- setActiveMedia(placed.media);
1440
- setPhase("ringing-out");
1441
- } catch (err) {
1442
- setError(err instanceof Error ? err : new Error(String(err)));
1443
- setPhase("idle");
1444
- }
1445
- },
1446
- [calls]
1447
- );
1448
- const ring = useCallback((call2) => {
1449
- if (incomingRef.current?.callId === call2.callId) return;
1450
- if (activeCallIdRef.current === call2.callId) return;
1451
- if (phaseRef.current !== "idle") return;
1452
- setIncoming(call2);
1453
- setPhase("ringing-in");
1454
- }, []);
1455
- const dropped = useCallback(
1456
- (callId) => {
1457
- if (pendingRef.current?.call.callId === callId) pendingRef.current = null;
1458
- if (!isCurrent(callId)) return;
1459
- rejectedRef.current = /* @__PURE__ */ new Map();
1460
- setPhase("idle");
1461
- setIncoming(null);
1462
- setOutgoing(null);
1463
- setActiveConversationId(null);
1464
- setActiveCallId(null);
1465
- setActiveMedia(null);
1466
- },
1467
- [isCurrent]
1468
- );
1469
- const accept = useCallback(
1470
- async (call2) => {
1471
- const target = call2 ?? incoming;
1472
- if (!target) return;
1473
- if (!calls) {
1474
- pendingRef.current = { kind: "accept", call: target };
1475
- setActiveConversationId(target.conversationId);
1476
- setActiveCallId(target.callId);
1477
- setActiveMedia(target.media);
1478
- setPhase("active");
1479
- setIncoming(null);
1480
- return;
1481
- }
1482
- setActiveConversationId(target.conversationId);
1483
- setActiveCallId(target.callId);
1484
- setActiveMedia(target.media);
1485
- setPhase("active");
1486
- setIncoming(null);
1487
- try {
1488
- await calls.accept(target);
1489
- } catch (err) {
1490
- const message = err instanceof Error ? err.message : String(err);
1491
- if (message.includes("timed out")) return;
1492
- setPhase("idle");
1493
- setActiveConversationId(null);
1494
- setActiveCallId(null);
1495
- setActiveMedia(null);
1496
- setError(err instanceof Error ? err : new Error(String(err)));
1497
- }
1498
- },
1499
- [calls, incoming]
1500
- );
1501
- const decline = useCallback(
1502
- async (call2) => {
1503
- const target = call2 ?? incoming;
1504
- if (!target) return;
1505
- if (!calls) {
1506
- pendingRef.current = { kind: "decline", call: target };
1507
- setIncoming(null);
1508
- setActiveCallId(null);
1509
- setActiveMedia(null);
1510
- setPhase("idle");
1511
- return;
1512
- }
1513
- try {
1514
- await calls.decline(target);
1515
- } catch (err) {
1516
- setError(err instanceof Error ? err : new Error(String(err)));
1517
- } finally {
1518
- setIncoming(null);
1519
- setActiveCallId(null);
1520
- setActiveMedia(null);
1521
- setPhase("idle");
1522
- }
1523
- },
1524
- [calls, incoming]
1525
- );
1526
- const cancel = useCallback(async () => {
1527
- if (!calls || !outgoing) return;
1528
- try {
1529
- await calls.cancel(outgoing);
1530
- } catch (err) {
1531
- setError(err instanceof Error ? err : new Error(String(err)));
1532
- } finally {
1533
- setOutgoing(null);
1534
- setActiveCallId(null);
1535
- setActiveMedia(null);
1536
- setPhase("idle");
1537
- }
1538
- }, [calls, outgoing]);
1539
- actionsRef.current = { cancel, decline };
1540
- useEffect(() => {
1541
- if (ringTimeoutMs <= 0) return;
1542
- if (phase !== "ringing-out" && phase !== "ringing-in") return;
1543
- const outbound = phase === "ringing-out";
1544
- const timer = setTimeout(() => {
1545
- void (outbound ? actionsRef.current.cancel() : actionsRef.current.decline()).finally(() => {
1546
- if (outbound) setPhase("timed-out");
1547
- });
1548
- }, ringTimeoutMs);
1549
- return () => clearTimeout(timer);
1550
- }, [phase, ringTimeoutMs]);
1551
- const othersOnCallRef = useRef(0);
1552
- const reportOthersOnCall = useCallback((count) => {
1553
- othersOnCallRef.current = count;
1554
- }, []);
1555
- const hangUp = useCallback(async () => {
1556
- if (phaseRef.current === "active" && othersOnCallRef.current >= 2) {
1557
- setPhase("idle");
1558
- setIncoming(null);
1559
- setOutgoing(null);
1560
- setActiveConversationId(null);
1561
- setActiveCallId(null);
1562
- setActiveMedia(null);
1563
- setError(null);
1564
- return;
1565
- }
1566
- const conversationId = activeConversationId ?? incoming?.conversationId ?? outgoing?.conversationId;
1567
- const callId = activeCallId ?? incoming?.callId ?? outgoing?.callId;
1568
- try {
1569
- if (!calls || !callId || !conversationId) return;
1570
- if (phase === "ringing-out" && outgoing) {
1571
- await calls.cancel(outgoing);
1572
- } else if (phase === "ringing-in" && incoming) {
1573
- await calls.decline(incoming);
1574
- } else if (phase === "active") {
1575
- await calls.hangUp({ callId, conversationId });
1576
- }
1577
- } catch (err) {
1578
- setError(err instanceof Error ? err : new Error(String(err)));
1579
- } finally {
1580
- setPhase("idle");
1581
- setIncoming(null);
1582
- setOutgoing(null);
1583
- setActiveConversationId(null);
1584
- setActiveCallId(null);
1585
- setActiveMedia(null);
1586
- }
1587
- }, [calls, phase, incoming, outgoing, activeConversationId, activeCallId]);
1588
- const reset = useCallback(() => {
1589
- rejectedRef.current = /* @__PURE__ */ new Map();
1590
- pendingRef.current = null;
1591
- setPhase("idle");
1592
- setIncoming(null);
1593
- setOutgoing(null);
1594
- setActiveConversationId(null);
1595
- setActiveCallId(null);
1596
- setActiveMedia(null);
1597
- setError(null);
1598
- }, []);
1599
- const media = incoming?.media ?? outgoing?.media ?? activeMedia ?? "audio";
1600
- return {
1601
- phase,
1602
- incoming,
1603
- outgoing,
1604
- activeConversationId,
1605
- media,
1606
- error,
1607
- call,
1608
- ring,
1609
- dropped,
1610
- accept,
1611
- decline,
1612
- cancel,
1613
- hangUp,
1614
- reportOthersOnCall,
1615
- reset
1616
- };
1617
- }
1618
1240
  function useLiveCall(conversationId) {
1619
1241
  const chat = usePoolse();
1620
1242
  const [call, setCall] = useState(null);
@@ -1666,6 +1288,55 @@ function useLiveCall(conversationId) {
1666
1288
  }, [chat, conversationId, apply, refresh]);
1667
1289
  return { call, participantIds, refresh };
1668
1290
  }
1291
+ function useRoom(conversationId, opts) {
1292
+ const poolse = usePoolse();
1293
+ const { call: live, participantIds } = useLiveCall(conversationId);
1294
+ const media = useCallRoom(conversationId, { livekit: opts.livekit, aloneGraceMs: 0 });
1295
+ const session = useRef(null);
1296
+ const [joining, setJoining] = useState(false);
1297
+ const [error, setError] = useState(null);
1298
+ useEffect(() => {
1299
+ session.current = null;
1300
+ setError(null);
1301
+ }, [conversationId]);
1302
+ const join = useCallback(
1303
+ async (kind = "audio", joinOpts = {}) => {
1304
+ if (!conversationId) return;
1305
+ setJoining(true);
1306
+ setError(null);
1307
+ try {
1308
+ session.current = joinOpts.ring ? await poolse.rooms.ring(conversationId, kind) : await poolse.rooms.start(conversationId, kind);
1309
+ const connected = await media.join();
1310
+ if (!connected) throw new Error("Rooms need the SFU, which this deployment does not have.");
1311
+ if (kind === "video") await media.setCameraEnabled(true);
1312
+ } catch (err) {
1313
+ setError(err instanceof Error ? err : new Error(String(err)));
1314
+ media.leave();
1315
+ } finally {
1316
+ setJoining(false);
1317
+ }
1318
+ },
1319
+ [poolse, conversationId, media]
1320
+ );
1321
+ const leave = useCallback(() => {
1322
+ media.leave();
1323
+ const s = session.current;
1324
+ session.current = null;
1325
+ if (s) void poolse.rooms.leave(s).catch(() => {
1326
+ });
1327
+ }, [poolse, media]);
1328
+ const joined = media.status === "connected" || media.status === "connecting" || joining;
1329
+ return {
1330
+ live,
1331
+ presentIds: participantIds,
1332
+ joined,
1333
+ joining,
1334
+ join,
1335
+ leave,
1336
+ media,
1337
+ error: error ?? media.error
1338
+ };
1339
+ }
1669
1340
  function useReadReceipts() {
1670
1341
  const chat = usePoolse();
1671
1342
  const { me } = useMe();
@@ -1695,6 +1366,6 @@ function canEditOwnMessage(args) {
1695
1366
  return Number.isFinite(sent) && (args.now ?? Date.now()) - sent < EDIT_WINDOW_MS;
1696
1367
  }
1697
1368
 
1698
- export { EDIT_WINDOW_MS, PoolseProvider, canEditOwnMessage, computeGroupPosition, formatDayLabel, receiptsOf, sameDay, sameGroup, useAttachmentUpload, useAttachmentUrl, useCallRoom, useCalls, useConversation, useConversations, useLiveCall, useMe, useMembers, useMessages, usePoolse, usePresence, useReactions, useReadReceipts, useRealtimeStatus, useThread, useTyping, useUser, useVoiceRoom };
1369
+ export { EDIT_WINDOW_MS, PoolseProvider, canEditOwnMessage, computeGroupPosition, formatDayLabel, receiptsOf, sameDay, sameGroup, useAttachmentUpload, useAttachmentUrl, useCallRoom, useConversation, useConversations, useLiveCall, useMe, useMembers, useMessages, usePoolse, usePresence, useReactions, useReadReceipts, useRealtimeStatus, useRoom, useThread, useTyping, useUser };
1699
1370
  //# sourceMappingURL=index.js.map
1700
1371
  //# sourceMappingURL=index.js.map