@poolse/react-native 2.6.0 → 2.7.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
@@ -5115,7 +5115,8 @@ function CallScreen({
5115
5115
  labelFor,
5116
5116
  calls: external,
5117
5117
  webrtc,
5118
- onStatusChange
5118
+ onStatusChange,
5119
+ nativeIncomingUi = false
5119
5120
  }) {
5120
5121
  const theme = usePoolseTheme();
5121
5122
  const own = react$1.useCalls(external ? null : userId);
@@ -5136,7 +5137,8 @@ function CallScreen({
5136
5137
  onStatusChange?.(voice.status);
5137
5138
  }, [voice.status, onStatusChange]);
5138
5139
  const label = labelFor ?? ((id) => `User ${id.slice(0, 6)}`);
5139
- const visible = calls.phase !== "idle";
5140
+ const ringingIn = calls.phase === "ringing-in";
5141
+ const visible = calls.phase !== "idle" && !(nativeIncomingUi && ringingIn);
5140
5142
  const hangUp = () => {
5141
5143
  voice.leave();
5142
5144
  calls.reset();
@@ -5237,29 +5239,50 @@ var styles24 = reactNative.StyleSheet.create({
5237
5239
  function useVoipCalls({
5238
5240
  voipPush,
5239
5241
  callKeep,
5242
+ calls,
5240
5243
  onAnswer,
5241
5244
  onDecline,
5242
5245
  appName = "poolse",
5243
- apnsEnvironment
5246
+ apnsEnvironment,
5247
+ staleAfterMs = 45e3
5244
5248
  }) {
5245
5249
  const poolse = react$1.usePoolse();
5246
- const pending = react.useRef(/* @__PURE__ */ new Map());
5247
- const handlers = react.useRef({ onAnswer, onDecline });
5248
- handlers.current = { onAnswer, onDecline };
5250
+ const shown = react.useRef(/* @__PURE__ */ new Map());
5251
+ const seen = react.useRef(/* @__PURE__ */ new Set());
5252
+ const handlers = react.useRef({ onAnswer, onDecline, calls });
5253
+ handlers.current = { onAnswer, onDecline, calls };
5254
+ const staleRef = react.useRef(staleAfterMs);
5255
+ staleRef.current = staleAfterMs;
5256
+ const dismiss = react.useRef((callId) => {
5257
+ if (!shown.current.has(callId)) return;
5258
+ shown.current.delete(callId);
5259
+ try {
5260
+ callKeep.endCall(callId);
5261
+ } catch {
5262
+ }
5263
+ });
5264
+ const present = react.useRef((call, sentAt) => {
5265
+ if (seen.current.has(call.callId)) return;
5266
+ if (typeof sentAt === "number" && Date.now() - sentAt > staleRef.current) return;
5267
+ seen.current.add(call.callId);
5268
+ shown.current.set(call.callId, call);
5269
+ callKeep.displayIncomingCall(call.callId, call.callerUserId, call.callerName, "generic", false);
5270
+ });
5249
5271
  react.useEffect(() => {
5250
5272
  let cancelled = false;
5251
- const reportCall = (payload) => {
5273
+ const fromPush = (payload) => {
5252
5274
  const callId = payload.call_id;
5253
5275
  const conversationId = payload.conversation_id;
5254
5276
  if (!callId || !conversationId) return;
5255
- const call = {
5256
- callId,
5257
- conversationId,
5258
- callerUserId: payload.caller_user_id ?? "",
5259
- callerName: payload.caller_name ?? "Incoming call"
5260
- };
5261
- pending.current.set(callId, call);
5262
- callKeep.displayIncomingCall(callId, call.callerUserId, call.callerName, "generic", false);
5277
+ present.current(
5278
+ {
5279
+ callId,
5280
+ conversationId,
5281
+ callerUserId: payload.caller_user_id ?? "",
5282
+ callerName: payload.caller_name ?? "Incoming call"
5283
+ },
5284
+ payload.sent_at
5285
+ );
5263
5286
  };
5264
5287
  void callKeep.setup({
5265
5288
  ios: { appName },
@@ -5282,23 +5305,36 @@ function useVoipCalls({
5282
5305
  });
5283
5306
  }));
5284
5307
  voipPush.addEventListener("notification", ((payload) => {
5285
- reportCall(payload);
5308
+ fromPush(payload);
5286
5309
  }));
5287
5310
  voipPush.addEventListener("didLoadWithEvents", ((events) => {
5288
5311
  for (const event of events ?? []) {
5289
5312
  if (event.name === "RNVoipPushRemoteNotificationReceivedEvent") {
5290
- reportCall(event.data);
5313
+ fromPush(event.data);
5291
5314
  }
5292
5315
  }
5293
5316
  }));
5294
5317
  callKeep.addEventListener("answerCall", (({ callUUID }) => {
5295
- const call = pending.current.get(callUUID);
5296
- if (call) handlers.current.onAnswer?.(call);
5318
+ const call = shown.current.get(callUUID);
5319
+ if (!call) return;
5320
+ shown.current.delete(callUUID);
5321
+ void handlers.current.calls?.accept({
5322
+ callId: call.callId,
5323
+ conversationId: call.conversationId,
5324
+ callerUserId: call.callerUserId
5325
+ });
5326
+ handlers.current.onAnswer?.(call);
5297
5327
  }));
5298
5328
  callKeep.addEventListener("endCall", (({ callUUID }) => {
5299
- const call = pending.current.get(callUUID);
5300
- pending.current.delete(callUUID);
5301
- if (call) handlers.current.onDecline?.(call);
5329
+ const call = shown.current.get(callUUID);
5330
+ shown.current.delete(callUUID);
5331
+ if (!call) return;
5332
+ void handlers.current.calls?.decline({
5333
+ callId: call.callId,
5334
+ conversationId: call.conversationId,
5335
+ callerUserId: call.callerUserId
5336
+ });
5337
+ handlers.current.onDecline?.(call);
5302
5338
  }));
5303
5339
  voipPush.registerVoipToken();
5304
5340
  return () => {
@@ -5310,6 +5346,22 @@ function useVoipCalls({
5310
5346
  callKeep.removeEventListener("endCall");
5311
5347
  };
5312
5348
  }, [poolse, voipPush, callKeep, appName, apnsEnvironment]);
5349
+ const incoming = calls?.incoming ?? null;
5350
+ const phase = calls?.phase;
5351
+ react.useEffect(() => {
5352
+ if (phase === "ringing-in" && incoming) {
5353
+ present.current({
5354
+ callId: incoming.callId,
5355
+ conversationId: incoming.conversationId,
5356
+ callerUserId: incoming.callerUserId,
5357
+ callerName: incoming.callerUserId
5358
+ });
5359
+ }
5360
+ }, [phase, incoming]);
5361
+ react.useEffect(() => {
5362
+ if (phase === "ringing-in" || phase === "ringing-out") return;
5363
+ for (const callId of [...shown.current.keys()]) dismiss.current(callId);
5364
+ }, [phase]);
5313
5365
  }
5314
5366
  function guessApnsEnvironment() {
5315
5367
  return __DEV__ ? "sandbox" : "production";