@thunderphone/widget 0.4.1 → 1.0.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.
@@ -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);
@@ -57492,15 +57500,15 @@ var ThunderPhone = (() => {
57492
57500
  this.name = "WidgetAPIError";
57493
57501
  }
57494
57502
  };
57495
- async function createWidgetSession(apiKey, agentId, apiBase) {
57503
+ async function createWidgetSession(publishableKey, apiBase) {
57496
57504
  const base = apiBase || DEFAULT_API_BASE;
57497
57505
  const response = await fetch(`${base}/widget/session`, {
57498
57506
  method: "POST",
57499
57507
  headers: {
57500
57508
  "Content-Type": "application/json",
57501
- "X-API-Key": apiKey
57509
+ "X-API-Key": publishableKey
57502
57510
  },
57503
- body: JSON.stringify({ agent_id: agentId })
57511
+ body: JSON.stringify({})
57504
57512
  });
57505
57513
  if (!response.ok) {
57506
57514
  const data = await response.json().catch(() => ({
@@ -57609,7 +57617,7 @@ var ThunderPhone = (() => {
57609
57617
  }
57610
57618
  );
57611
57619
  try {
57612
- const sess = await createWidgetSession(opts.apiKey, opts.agentId, opts.apiBase);
57620
+ const sess = await createWidgetSession(opts.publishableKey, opts.apiBase);
57613
57621
  setSession(sess);
57614
57622
  } catch (err) {
57615
57623
  setState("error");
@@ -57621,7 +57629,7 @@ var ThunderPhone = (() => {
57621
57629
  opts.onError?.({ error: "unknown", message: "Unable to connect." });
57622
57630
  }
57623
57631
  }
57624
- }, [opts.apiKey, opts.agentId, opts.apiBase, state, opts.onError]);
57632
+ }, [opts.publishableKey, opts.apiBase, state, opts.onError]);
57625
57633
  const disconnect = (0, import_react3.useCallback)(() => {
57626
57634
  handleDisconnect();
57627
57635
  }, [handleDisconnect]);
@@ -57655,8 +57663,7 @@ var ThunderPhone = (() => {
57655
57663
  // src/ThunderPhoneWidget.tsx
57656
57664
  var import_jsx_runtime4 = __toESM(require_jsx_runtime());
57657
57665
  function ThunderPhoneWidget({
57658
- apiKey,
57659
- agentId,
57666
+ publishableKey,
57660
57667
  apiBase,
57661
57668
  onConnect,
57662
57669
  onDisconnect,
@@ -57664,7 +57671,7 @@ var ThunderPhone = (() => {
57664
57671
  className,
57665
57672
  ringtone
57666
57673
  }) {
57667
- const phone = useThunderPhone({ apiKey, agentId, apiBase, onConnect, onDisconnect, onError, ringtone });
57674
+ const phone = useThunderPhone({ publishableKey, apiBase, onConnect, onDisconnect, onError, ringtone });
57668
57675
  const handleClick = () => {
57669
57676
  if (phone.state === "connected") {
57670
57677
  phone.disconnect();