@thunderphone/widget 0.3.3 → 0.4.1

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;
@@ -57513,11 +57513,68 @@ var ThunderPhone = (() => {
57513
57513
  }
57514
57514
 
57515
57515
  // src/useThunderPhone.ts
57516
+ var DEFAULT_RINGTONE_URL = "https://storage.googleapis.com/thunderphone-widget-cdn/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
+ }
57522
+ function fadeOutAndStop(audio) {
57523
+ if (audio.paused) return;
57524
+ const fadeInterval = setInterval(() => {
57525
+ const next = audio.volume - 0.1;
57526
+ if (next <= 0) {
57527
+ clearInterval(fadeInterval);
57528
+ audio.pause();
57529
+ audio.currentTime = 0;
57530
+ audio.volume = 1;
57531
+ } else {
57532
+ audio.volume = next;
57533
+ }
57534
+ }, 20);
57535
+ return fadeInterval;
57536
+ }
57516
57537
  function useThunderPhone(opts) {
57517
57538
  const [state, setState] = (0, import_react3.useState)("idle");
57518
57539
  const [session, setSession] = (0, import_react3.useState)(null);
57519
57540
  const [muted, setMuted] = (0, import_react3.useState)(false);
57520
57541
  const [error, setError] = (0, import_react3.useState)();
57542
+ const ringtoneUrl = resolveRingtoneUrl(opts.ringtone);
57543
+ const ringtoneRef = (0, import_react3.useRef)(null);
57544
+ const fadeRef = (0, import_react3.useRef)(void 0);
57545
+ (0, import_react3.useEffect)(() => {
57546
+ if (!ringtoneUrl) {
57547
+ ringtoneRef.current = null;
57548
+ return;
57549
+ }
57550
+ const audio2 = new Audio();
57551
+ audio2.crossOrigin = "anonymous";
57552
+ audio2.loop = true;
57553
+ audio2.preload = "auto";
57554
+ audio2.volume = 1;
57555
+ audio2.src = ringtoneUrl;
57556
+ ringtoneRef.current = audio2;
57557
+ return () => {
57558
+ audio2.pause();
57559
+ audio2.src = "";
57560
+ ringtoneRef.current = null;
57561
+ };
57562
+ }, [ringtoneUrl]);
57563
+ (0, import_react3.useEffect)(() => {
57564
+ if (state !== "connecting") {
57565
+ const audio2 = ringtoneRef.current;
57566
+ if (audio2 && !audio2.paused) {
57567
+ if (fadeRef.current) clearInterval(fadeRef.current);
57568
+ fadeRef.current = fadeOutAndStop(audio2);
57569
+ }
57570
+ }
57571
+ return () => {
57572
+ if (fadeRef.current) {
57573
+ clearInterval(fadeRef.current);
57574
+ fadeRef.current = void 0;
57575
+ }
57576
+ };
57577
+ }, [state]);
57521
57578
  const handleDisconnect = (0, import_react3.useCallback)(() => {
57522
57579
  setState("disconnected");
57523
57580
  setSession(null);
@@ -57533,6 +57590,24 @@ var ThunderPhone = (() => {
57533
57590
  if (state === "connecting" || state === "connected") return;
57534
57591
  setState("connecting");
57535
57592
  setError(void 0);
57593
+ const ringtoneAudio = ringtoneRef.current;
57594
+ if (ringtoneAudio) {
57595
+ if (fadeRef.current) {
57596
+ clearInterval(fadeRef.current);
57597
+ fadeRef.current = void 0;
57598
+ }
57599
+ ringtoneAudio.currentTime = 0;
57600
+ ringtoneAudio.volume = 1;
57601
+ ringtoneAudio.play().catch(() => {
57602
+ });
57603
+ }
57604
+ navigator.mediaDevices.getUserMedia({ audio: true }).then(
57605
+ (stream) => {
57606
+ stream.getTracks().forEach((t) => t.stop());
57607
+ },
57608
+ () => {
57609
+ }
57610
+ );
57536
57611
  try {
57537
57612
  const sess = await createWidgetSession(opts.apiKey, opts.agentId, opts.apiBase);
57538
57613
  setSession(sess);
@@ -57586,9 +57661,10 @@ var ThunderPhone = (() => {
57586
57661
  onConnect,
57587
57662
  onDisconnect,
57588
57663
  onError,
57589
- className
57664
+ className,
57665
+ ringtone
57590
57666
  }) {
57591
- const phone = useThunderPhone({ apiKey, agentId, apiBase, onConnect, onDisconnect, onError });
57667
+ const phone = useThunderPhone({ apiKey, agentId, apiBase, onConnect, onDisconnect, onError, ringtone });
57592
57668
  const handleClick = () => {
57593
57669
  if (phone.state === "connected") {
57594
57670
  phone.disconnect();