@thunderphone/widget 0.3.1 → 0.4.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.
@@ -1371,11 +1371,11 @@ var ThunderPhone = (() => {
1371
1371
  var dispatcher = resolveDispatcher();
1372
1372
  return dispatcher.useReducer(reducer, initialArg, init);
1373
1373
  }
1374
- function useRef3(initialValue) {
1374
+ function useRef4(initialValue) {
1375
1375
  var dispatcher = resolveDispatcher();
1376
1376
  return dispatcher.useRef(initialValue);
1377
1377
  }
1378
- function useEffect4(create, deps) {
1378
+ function useEffect5(create, deps) {
1379
1379
  var dispatcher = resolveDispatcher();
1380
1380
  return dispatcher.useEffect(create, deps);
1381
1381
  }
@@ -2158,14 +2158,14 @@ var ThunderPhone = (() => {
2158
2158
  exports.useContext = useContext2;
2159
2159
  exports.useDebugValue = useDebugValue;
2160
2160
  exports.useDeferredValue = useDeferredValue;
2161
- exports.useEffect = useEffect4;
2161
+ exports.useEffect = useEffect5;
2162
2162
  exports.useId = useId;
2163
2163
  exports.useImperativeHandle = useImperativeHandle;
2164
2164
  exports.useInsertionEffect = useInsertionEffect;
2165
2165
  exports.useLayoutEffect = useLayoutEffect;
2166
2166
  exports.useMemo = useMemo2;
2167
2167
  exports.useReducer = useReducer2;
2168
- exports.useRef = useRef3;
2168
+ exports.useRef = useRef4;
2169
2169
  exports.useState = useState4;
2170
2170
  exports.useSyncExternalStore = useSyncExternalStore;
2171
2171
  exports.useTransition = useTransition;
@@ -57448,25 +57448,32 @@ var ThunderPhone = (() => {
57448
57448
  const handleDisconnect = () => onDisconnected();
57449
57449
  const attachTrack = (track, _pub, _participant) => {
57450
57450
  if (track.kind === Track.Kind.Audio && audioRef.current) {
57451
- const stream = new MediaStream([track.mediaStreamTrack]);
57452
- audioRef.current.srcObject = stream;
57453
- audioRef.current.play().catch(() => {
57454
- });
57451
+ const existing = audioRef.current.srcObject;
57452
+ if (existing) {
57453
+ existing.addTrack(track.mediaStreamTrack);
57454
+ } else {
57455
+ audioRef.current.srcObject = new MediaStream([track.mediaStreamTrack]);
57456
+ audioRef.current.play().catch(() => {
57457
+ });
57458
+ }
57455
57459
  }
57456
57460
  };
57457
57461
  room.on(RoomEvent.ParticipantConnected, handleParticipantConnected);
57458
57462
  room.on(RoomEvent.Disconnected, handleDisconnect);
57459
57463
  room.on(RoomEvent.TrackSubscribed, attachTrack);
57464
+ const initialTracks = [];
57460
57465
  for (const participant of room.remoteParticipants.values()) {
57461
57466
  for (const pub of participant.trackPublications.values()) {
57462
- if (pub.track && pub.isSubscribed && pub.track.kind === Track.Kind.Audio && audioRef.current) {
57463
- const stream = new MediaStream([pub.track.mediaStreamTrack]);
57464
- audioRef.current.srcObject = stream;
57465
- audioRef.current.play().catch(() => {
57466
- });
57467
+ if (pub.track && pub.isSubscribed && pub.track.kind === Track.Kind.Audio) {
57468
+ initialTracks.push(pub.track.mediaStreamTrack);
57467
57469
  }
57468
57470
  }
57469
57471
  }
57472
+ if (initialTracks.length > 0 && audioRef.current) {
57473
+ audioRef.current.srcObject = new MediaStream(initialTracks);
57474
+ audioRef.current.play().catch(() => {
57475
+ });
57476
+ }
57470
57477
  return () => {
57471
57478
  room.off(RoomEvent.ParticipantConnected, handleParticipantConnected);
57472
57479
  room.off(RoomEvent.Disconnected, handleDisconnect);
@@ -57506,11 +57513,64 @@ var ThunderPhone = (() => {
57506
57513
  }
57507
57514
 
57508
57515
  // src/useThunderPhone.ts
57516
+ var DEFAULT_RINGTONE_URL = "https://cdn.thunderphone.com/widget/assets/ringtone-default.mp3";
57517
+ function resolveRingtoneUrl(ringtone) {
57518
+ if (ringtone === true || ringtone === "default") return DEFAULT_RINGTONE_URL;
57519
+ if (typeof ringtone === "string" && ringtone.length > 0) return ringtone;
57520
+ return null;
57521
+ }
57509
57522
  function useThunderPhone(opts) {
57510
57523
  const [state, setState] = (0, import_react3.useState)("idle");
57511
57524
  const [session, setSession] = (0, import_react3.useState)(null);
57512
57525
  const [muted, setMuted] = (0, import_react3.useState)(false);
57513
57526
  const [error, setError] = (0, import_react3.useState)();
57527
+ const ringtoneUrl = resolveRingtoneUrl(opts.ringtone);
57528
+ const ringtoneRef = (0, import_react3.useRef)(null);
57529
+ (0, import_react3.useEffect)(() => {
57530
+ if (!ringtoneUrl) {
57531
+ ringtoneRef.current = null;
57532
+ return;
57533
+ }
57534
+ const audio2 = new Audio(ringtoneUrl);
57535
+ audio2.loop = true;
57536
+ audio2.preload = "auto";
57537
+ audio2.volume = 1;
57538
+ ringtoneRef.current = audio2;
57539
+ return () => {
57540
+ audio2.pause();
57541
+ audio2.src = "";
57542
+ ringtoneRef.current = null;
57543
+ };
57544
+ }, [ringtoneUrl]);
57545
+ (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);
57568
+ }
57569
+ }
57570
+ return () => {
57571
+ if (fadeInterval) clearInterval(fadeInterval);
57572
+ };
57573
+ }, [state]);
57514
57574
  const handleDisconnect = (0, import_react3.useCallback)(() => {
57515
57575
  setState("disconnected");
57516
57576
  setSession(null);
@@ -57526,6 +57586,13 @@ var ThunderPhone = (() => {
57526
57586
  if (state === "connecting" || state === "connected") return;
57527
57587
  setState("connecting");
57528
57588
  setError(void 0);
57589
+ navigator.mediaDevices.getUserMedia({ audio: true }).then(
57590
+ (stream) => {
57591
+ stream.getTracks().forEach((t) => t.stop());
57592
+ },
57593
+ () => {
57594
+ }
57595
+ );
57529
57596
  try {
57530
57597
  const sess = await createWidgetSession(opts.apiKey, opts.agentId, opts.apiBase);
57531
57598
  setSession(sess);
@@ -57579,9 +57646,10 @@ var ThunderPhone = (() => {
57579
57646
  onConnect,
57580
57647
  onDisconnect,
57581
57648
  onError,
57582
- className
57649
+ className,
57650
+ ringtone
57583
57651
  }) {
57584
- const phone = useThunderPhone({ apiKey, agentId, apiBase, onConnect, onDisconnect, onError });
57652
+ const phone = useThunderPhone({ apiKey, agentId, apiBase, onConnect, onDisconnect, onError, ringtone });
57585
57653
  const handleClick = () => {
57586
57654
  if (phone.state === "connected") {
57587
57655
  phone.disconnect();