@thunderphone/widget 0.4.0 → 0.4.2

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.
@@ -57441,10 +57441,16 @@ var ThunderPhone = (() => {
57441
57441
  const room = ua();
57442
57442
  const audioRef = (0, import_react2.useRef)(null);
57443
57443
  (0, import_react2.useEffect)(() => {
57444
- if (room.remoteParticipants.size > 0) {
57444
+ let didSignalConnected = false;
57445
+ const signalConnected = () => {
57446
+ if (didSignalConnected) return;
57447
+ didSignalConnected = true;
57445
57448
  onAgentConnected();
57449
+ };
57450
+ if (room.remoteParticipants.size > 0) {
57451
+ signalConnected();
57446
57452
  }
57447
- const handleParticipantConnected = () => onAgentConnected();
57453
+ const handleParticipantConnected = () => signalConnected();
57448
57454
  const handleDisconnect = () => onDisconnected();
57449
57455
  const attachTrack = (track, _pub, _participant) => {
57450
57456
  if (track.kind === Track.Kind.Audio && audioRef.current) {
@@ -57456,6 +57462,7 @@ var ThunderPhone = (() => {
57456
57462
  audioRef.current.play().catch(() => {
57457
57463
  });
57458
57464
  }
57465
+ signalConnected();
57459
57466
  }
57460
57467
  };
57461
57468
  room.on(RoomEvent.ParticipantConnected, handleParticipantConnected);
@@ -57473,6 +57480,7 @@ var ThunderPhone = (() => {
57473
57480
  audioRef.current.srcObject = new MediaStream(initialTracks);
57474
57481
  audioRef.current.play().catch(() => {
57475
57482
  });
57483
+ signalConnected();
57476
57484
  }
57477
57485
  return () => {
57478
57486
  room.off(RoomEvent.ParticipantConnected, handleParticipantConnected);
@@ -57513,12 +57521,27 @@ var ThunderPhone = (() => {
57513
57521
  }
57514
57522
 
57515
57523
  // src/useThunderPhone.ts
57516
- var DEFAULT_RINGTONE_URL = "https://cdn.thunderphone.com/widget/assets/ringtone-default.mp3";
57524
+ var DEFAULT_RINGTONE_URL = "https://storage.googleapis.com/thunderphone-widget-cdn/widget/assets/ringtone-default.mp3";
57517
57525
  function resolveRingtoneUrl(ringtone) {
57518
57526
  if (ringtone === true || ringtone === "default") return DEFAULT_RINGTONE_URL;
57519
57527
  if (typeof ringtone === "string" && ringtone.length > 0) return ringtone;
57520
57528
  return null;
57521
57529
  }
57530
+ function fadeOutAndStop(audio) {
57531
+ if (audio.paused) return;
57532
+ const fadeInterval = setInterval(() => {
57533
+ const next = audio.volume - 0.1;
57534
+ if (next <= 0) {
57535
+ clearInterval(fadeInterval);
57536
+ audio.pause();
57537
+ audio.currentTime = 0;
57538
+ audio.volume = 1;
57539
+ } else {
57540
+ audio.volume = next;
57541
+ }
57542
+ }, 20);
57543
+ return fadeInterval;
57544
+ }
57522
57545
  function useThunderPhone(opts) {
57523
57546
  const [state, setState] = (0, import_react3.useState)("idle");
57524
57547
  const [session, setSession] = (0, import_react3.useState)(null);
@@ -57526,15 +57549,18 @@ var ThunderPhone = (() => {
57526
57549
  const [error, setError] = (0, import_react3.useState)();
57527
57550
  const ringtoneUrl = resolveRingtoneUrl(opts.ringtone);
57528
57551
  const ringtoneRef = (0, import_react3.useRef)(null);
57552
+ const fadeRef = (0, import_react3.useRef)(void 0);
57529
57553
  (0, import_react3.useEffect)(() => {
57530
57554
  if (!ringtoneUrl) {
57531
57555
  ringtoneRef.current = null;
57532
57556
  return;
57533
57557
  }
57534
- const audio2 = new Audio(ringtoneUrl);
57558
+ const audio2 = new Audio();
57559
+ audio2.crossOrigin = "anonymous";
57535
57560
  audio2.loop = true;
57536
57561
  audio2.preload = "auto";
57537
57562
  audio2.volume = 1;
57563
+ audio2.src = ringtoneUrl;
57538
57564
  ringtoneRef.current = audio2;
57539
57565
  return () => {
57540
57566
  audio2.pause();
@@ -57543,32 +57569,18 @@ var ThunderPhone = (() => {
57543
57569
  };
57544
57570
  }, [ringtoneUrl]);
57545
57571
  (0, import_react3.useEffect)(() => {
57546
- const audio2 = ringtoneRef.current;
57547
- if (!audio2) return;
57548
- let fadeInterval;
57549
- if (state === "connecting") {
57550
- audio2.currentTime = 0;
57551
- audio2.volume = 1;
57552
- audio2.play().catch(() => {
57553
- });
57554
- } else {
57555
- if (!audio2.paused) {
57556
- fadeInterval = setInterval(() => {
57557
- const next = audio2.volume - 0.1;
57558
- if (next <= 0) {
57559
- clearInterval(fadeInterval);
57560
- fadeInterval = void 0;
57561
- audio2.pause();
57562
- audio2.currentTime = 0;
57563
- audio2.volume = 1;
57564
- } else {
57565
- audio2.volume = next;
57566
- }
57567
- }, 20);
57572
+ if (state !== "connecting") {
57573
+ const audio2 = ringtoneRef.current;
57574
+ if (audio2 && !audio2.paused) {
57575
+ if (fadeRef.current) clearInterval(fadeRef.current);
57576
+ fadeRef.current = fadeOutAndStop(audio2);
57568
57577
  }
57569
57578
  }
57570
57579
  return () => {
57571
- if (fadeInterval) clearInterval(fadeInterval);
57580
+ if (fadeRef.current) {
57581
+ clearInterval(fadeRef.current);
57582
+ fadeRef.current = void 0;
57583
+ }
57572
57584
  };
57573
57585
  }, [state]);
57574
57586
  const handleDisconnect = (0, import_react3.useCallback)(() => {
@@ -57586,6 +57598,17 @@ var ThunderPhone = (() => {
57586
57598
  if (state === "connecting" || state === "connected") return;
57587
57599
  setState("connecting");
57588
57600
  setError(void 0);
57601
+ const ringtoneAudio = ringtoneRef.current;
57602
+ if (ringtoneAudio) {
57603
+ if (fadeRef.current) {
57604
+ clearInterval(fadeRef.current);
57605
+ fadeRef.current = void 0;
57606
+ }
57607
+ ringtoneAudio.currentTime = 0;
57608
+ ringtoneAudio.volume = 1;
57609
+ ringtoneAudio.play().catch(() => {
57610
+ });
57611
+ }
57589
57612
  navigator.mediaDevices.getUserMedia({ audio: true }).then(
57590
57613
  (stream) => {
57591
57614
  stream.getTracks().forEach((t) => t.stop());