@getwidgets/live-chat-widget 1.0.6 → 1.0.8

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.
@@ -19710,14 +19710,16 @@
19710
19710
  function LuSend(props) {
19711
19711
  return GenIcon({ "attr": { "viewBox": "0 0 24 24", "fill": "none", "stroke": "currentColor", "strokeWidth": "2", "strokeLinecap": "round", "strokeLinejoin": "round" }, "child": [{ "tag": "path", "attr": { "d": "m22 2-7 20-4-9-9-4Z" } }, { "tag": "path", "attr": { "d": "M22 2 11 13" } }] })(props);
19712
19712
  }
19713
+ const API_BASE_URL = "https://api.getwidgets.app";
19714
+ const WS_BASE_URL = "wss://api.getwidgets.app";
19713
19715
  const fetchWidgetConfig = async (widgetId) => {
19714
- const res = await fetch(`https://api.getwidgets.app/api/widgets/user-widgets/${widgetId}/config`);
19716
+ const res = await fetch(`${API_BASE_URL}/api/widgets/user-widgets/${widgetId}/config`);
19715
19717
  if (!res.ok) throw new Error("Failed to fetch widget config");
19716
19718
  return res.json();
19717
19719
  };
19718
19720
  const checkSessionStatus = async (widgetId, sessionId) => {
19719
19721
  try {
19720
- const url = `https://api.getwidgets.app/api/widgets/livechat/${widgetId}/status/?session_id=${sessionId}`;
19722
+ const url = `${API_BASE_URL}/api/widgets/livechat/${widgetId}/status/?session_id=${sessionId}`;
19721
19723
  const res = await fetch(url);
19722
19724
  if (!res.ok) {
19723
19725
  const txt = await res.text().catch(() => null);
@@ -19748,7 +19750,7 @@
19748
19750
  }
19749
19751
  return sessionId;
19750
19752
  };
19751
- const LiveChatWidget = ({ widgetId, apiKey }) => {
19753
+ const LiveChatWidget = ({ widgetId, apiKey, name: name2 = null, email = null, unique_id = null }) => {
19752
19754
  var _a, _b;
19753
19755
  const [config, setConfig] = reactExports.useState(null);
19754
19756
  const [messages, setMessages] = reactExports.useState([]);
@@ -19773,6 +19775,23 @@
19773
19775
  const [windowWidth, setWindowWidth] = reactExports.useState(typeof window !== "undefined" ? window.innerWidth : 1024);
19774
19776
  const isOpenRef = reactExports.useRef(false);
19775
19777
  const ICON_MAP = { IoIosSend, IoSendOutline, MdOutlineSend, LuSend };
19778
+ const [authToken, setAuthToken] = reactExports.useState(null);
19779
+ const [localStorageToken, setLocalStorageToken] = reactExports.useState(null);
19780
+ const [contactName, setContactName] = reactExports.useState("");
19781
+ const [contactEmail, setContactEmail] = reactExports.useState("");
19782
+ const [showContactFields, setShowContactFields] = reactExports.useState(false);
19783
+ const [showContactErrors, setShowContactErrors] = reactExports.useState(false);
19784
+ const base64UrlEncode = (value) => {
19785
+ const json = typeof value === "string" ? value : JSON.stringify(value);
19786
+ const base64 = btoa(unescape(encodeURIComponent(json)));
19787
+ return base64.replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
19788
+ };
19789
+ const buildUnsignedJwt = (payload) => {
19790
+ const header2 = { alg: "none", typ: "JWT" };
19791
+ const encodedHeader = base64UrlEncode(header2);
19792
+ const encodedPayload = base64UrlEncode(payload);
19793
+ return `${encodedHeader}.${encodedPayload}`;
19794
+ };
19776
19795
  const normalizeIncomingMessage = (m2) => {
19777
19796
  if (!m2) return { id: void 0, role: "agent", content: "", timestamp: (/* @__PURE__ */ new Date()).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }), created_at: Date.now() };
19778
19797
  if (m2.sent_by) {
@@ -19818,6 +19837,54 @@
19818
19837
  const id2 = getSessionId();
19819
19838
  setSessionId(id2);
19820
19839
  }, [widgetId]);
19840
+ reactExports.useEffect(() => {
19841
+ if (typeof window === "undefined") return;
19842
+ try {
19843
+ const stored = localStorage.getItem("widgetkraft_livechat_authtoken");
19844
+ setLocalStorageToken(stored);
19845
+ if (stored && !authToken) setAuthToken(stored);
19846
+ } catch (e) {
19847
+ }
19848
+ const handleStorageChange = (e) => {
19849
+ if (e.key === "widgetkraft_livechat_authtoken" || e.key === null) {
19850
+ try {
19851
+ const stored = localStorage.getItem("widgetkraft_livechat_authtoken");
19852
+ setLocalStorageToken(stored);
19853
+ if (stored && !authToken) setAuthToken(stored);
19854
+ } catch (err) {
19855
+ }
19856
+ }
19857
+ };
19858
+ window.addEventListener("storage", handleStorageChange);
19859
+ return () => window.removeEventListener("storage", handleStorageChange);
19860
+ }, [authToken]);
19861
+ reactExports.useEffect(() => {
19862
+ const buildAuthToken = async () => {
19863
+ const hasIdentity = Boolean(name2 || email || unique_id);
19864
+ if (!hasIdentity || typeof window === "undefined") {
19865
+ return;
19866
+ }
19867
+ try {
19868
+ const payload = { name: name2, email, unique_id };
19869
+ const token = buildUnsignedJwt(payload);
19870
+ if (token) {
19871
+ try {
19872
+ localStorage.setItem("widgetkraft_livechat_authtoken", token);
19873
+ } catch (e) {
19874
+ }
19875
+ setAuthToken(token);
19876
+ }
19877
+ } catch (e) {
19878
+ console.warn("Failed to create auth token", e);
19879
+ try {
19880
+ localStorage.removeItem("widgetkraft_livechat_authtoken");
19881
+ } catch (err) {
19882
+ }
19883
+ setAuthToken(null);
19884
+ }
19885
+ };
19886
+ buildAuthToken();
19887
+ }, [name2, email, unique_id]);
19821
19888
  reactExports.useEffect(() => {
19822
19889
  const dismissalKey = `live-chat-notification-dismissed/${widgetId}`;
19823
19890
  const dismissalTime = localStorage.getItem(dismissalKey);
@@ -19877,11 +19944,12 @@
19877
19944
  }, []);
19878
19945
  const startLiveSession = async (widgetUuid, currentSessionId) => {
19879
19946
  try {
19880
- const url = `https://api.getwidgets.app/api/widgets/livechat/start/${widgetUuid}/`;
19947
+ const url = `${API_BASE_URL}/api/widgets/livechat/start/${widgetUuid}/`;
19948
+ const storedAuthToken = authToken || (typeof window !== "undefined" ? localStorage.getItem("widgetkraft_livechat_authtoken") : null);
19881
19949
  const res = await fetch(url, {
19882
19950
  method: "POST",
19883
19951
  headers: { "Content-Type": "application/json" },
19884
- body: JSON.stringify({ session_id: currentSessionId })
19952
+ body: JSON.stringify({ session_id: currentSessionId, auth_token: storedAuthToken || null })
19885
19953
  });
19886
19954
  if (!res.ok) {
19887
19955
  const txt = await res.text().catch(() => null);
@@ -19904,7 +19972,7 @@
19904
19972
  }
19905
19973
  wsRef.current = null;
19906
19974
  }
19907
- const url = `wss://api.getwidgets.app/ws/livechat/${sid}`;
19975
+ const url = `${WS_BASE_URL}/ws/livechat/${sid}`;
19908
19976
  const ws = new WebSocket(url);
19909
19977
  wsRef.current = ws;
19910
19978
  ws.onopen = () => {
@@ -20141,8 +20209,8 @@
20141
20209
  localStorage.setItem(dismissalKey, Date.now().toString());
20142
20210
  setShowNotificationPreview(false);
20143
20211
  };
20144
- if (!config) return null;
20145
- const { header, chat_area, input_area, appearance } = ((_a = config.config) == null ? void 0 : _a.chat_widget) || {};
20212
+ const widgetConfig = ((_a = config == null ? void 0 : config.config) == null ? void 0 : _a.chat_widget) || {};
20213
+ const { header, chat_area, input_area, appearance } = widgetConfig;
20146
20214
  const headerConfig = header || {};
20147
20215
  const appearanceConfig = appearance || {};
20148
20216
  const chatAreaConfig = chat_area || {};
@@ -20210,7 +20278,58 @@
20210
20278
  const luminance = (0.299 * r2 + 0.587 * g + 0.114 * b) / 255;
20211
20279
  return luminance > 0.5 ? "#000000" : "#FFFFFF";
20212
20280
  };
20281
+ const getMessageTime = (message) => {
20282
+ if (!message) return "";
20283
+ const raw = message.timestamp ?? message.created_at;
20284
+ if (!raw) return "";
20285
+ if (typeof raw === "string") {
20286
+ const looksLikeTimeOnly = /:\d{2}/.test(raw) && !/\d{4}-\d{2}-\d{2}|T|Z/i.test(raw);
20287
+ if (looksLikeTimeOnly) return raw;
20288
+ const parsed2 = new Date(raw);
20289
+ if (!Number.isNaN(parsed2.getTime())) {
20290
+ return parsed2.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
20291
+ }
20292
+ return "";
20293
+ }
20294
+ const parsed = new Date(raw);
20295
+ if (Number.isNaN(parsed.getTime())) return "";
20296
+ return parsed.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
20297
+ };
20298
+ const timeTextColor = getContrastColor(appearanceConfig.background_color || "#F9FAFB");
20213
20299
  const isMobile = windowWidth < 768;
20300
+ reactExports.useEffect(() => {
20301
+ if (inputAreaConfig == null ? void 0 : inputAreaConfig.allow_chat_with_email) {
20302
+ const hasToken = authToken || localStorageToken;
20303
+ setShowContactFields(!hasToken);
20304
+ } else {
20305
+ setShowContactFields(false);
20306
+ }
20307
+ }, [inputAreaConfig == null ? void 0 : inputAreaConfig.allow_chat_with_email, authToken, localStorageToken, isOpen]);
20308
+ const isContactValid = !(inputAreaConfig == null ? void 0 : inputAreaConfig.allow_chat_with_email) || (!showContactFields ? true : contactName.trim() && contactEmail.trim());
20309
+ const createAndStoreAuthToken = () => {
20310
+ const payload = { name: contactName.trim(), email: contactEmail.trim(), unique_id };
20311
+ const token = buildUnsignedJwt(payload);
20312
+ try {
20313
+ localStorage.setItem("widgetkraft_livechat_authtoken", token);
20314
+ } catch (e) {
20315
+ }
20316
+ setAuthToken(token);
20317
+ return token;
20318
+ };
20319
+ const performSendWithContact = async () => {
20320
+ if ((inputAreaConfig == null ? void 0 : inputAreaConfig.allow_chat_with_email) && !authToken) {
20321
+ const missingName = !contactName.trim();
20322
+ const missingEmail = !contactEmail.trim();
20323
+ if (missingName || missingEmail) {
20324
+ setShowContactErrors(true);
20325
+ return;
20326
+ }
20327
+ createAndStoreAuthToken();
20328
+ setShowContactFields(false);
20329
+ }
20330
+ await performSend();
20331
+ };
20332
+ if (!config) return null;
20214
20333
  return /* @__PURE__ */ React.createElement("div", { style: getPositionStyles() }, !isOpen && showNotificationPreview && /* @__PURE__ */ React.createElement(
20215
20334
  "div",
20216
20335
  {
@@ -20428,23 +20547,25 @@
20428
20547
  }
20429
20548
  },
20430
20549
  inputAreaConfig.first_ai_message || "Hello! How can I assist you today?"
20431
- )) : combinedMessages.map((message) => /* @__PURE__ */ React.createElement("div", { key: message.id, className: `flex ${message.role === "user" ? "justify-end" : "justify-start"}` }, /* @__PURE__ */ React.createElement(
20432
- "div",
20433
- {
20434
- className: `p-3 inline-block rounded-lg`,
20435
- style: {
20436
- backgroundColor: message.role === "user" ? chatAreaConfig.user_response_color || "#4ADE80" : message.isError ? "#FEE2E2" : chatAreaConfig.ai_response_color || "#E2E8F0",
20437
- borderRadius: chatAreaConfig.chat_bubble_radius || "16px",
20438
- fontSize: isMobile ? "13px" : "14px",
20439
- color: message.role === "user" ? chatAreaConfig.user_font_color || "#111827" : message.isError ? "#991B1B" : chatAreaConfig.ai_font_color || "#111827",
20440
- marginBottom: isMobile ? "4px" : "2px",
20441
- maxWidth: isMobile ? "85%" : "80%",
20442
- wordBreak: "break-word"
20443
- }
20444
- },
20445
- message.role !== "system" ? /* @__PURE__ */ React.createElement(Markdown, { remarkPlugins: [remarkGfm] }, message.content) : /* @__PURE__ */ React.createElement("em", { className: "text-xs opacity-70" }, message.content),
20446
- chatAreaConfig.show_timestamps && message.timestamp && /* @__PURE__ */ React.createElement("div", { className: "text-xs opacity-70 mt-1 text-right" }, message.timestamp)
20447
- ))), localLoading && /* @__PURE__ */ React.createElement("div", { className: "flex justify-start" }, /* @__PURE__ */ React.createElement(
20550
+ )) : combinedMessages.map((message) => {
20551
+ const timeText = getMessageTime(message);
20552
+ return /* @__PURE__ */ React.createElement("div", { key: message.id, className: `flex flex-col ${message.role === "user" ? "items-end" : "items-start"}` }, /* @__PURE__ */ React.createElement(
20553
+ "div",
20554
+ {
20555
+ className: `p-3 inline-block rounded-lg`,
20556
+ style: {
20557
+ backgroundColor: message.role === "user" ? chatAreaConfig.user_response_color || "#4ADE80" : message.isError ? "#FEE2E2" : chatAreaConfig.ai_response_color || "#E2E8F0",
20558
+ borderRadius: chatAreaConfig.chat_bubble_radius || "16px",
20559
+ fontSize: isMobile ? "13px" : "14px",
20560
+ color: message.role === "user" ? chatAreaConfig.user_font_color || "#111827" : message.isError ? "#991B1B" : chatAreaConfig.ai_font_color || "#111827",
20561
+ marginBottom: isMobile ? "4px" : "2px",
20562
+ maxWidth: isMobile ? "85%" : "80%",
20563
+ wordBreak: "break-word"
20564
+ }
20565
+ },
20566
+ message.role !== "system" ? /* @__PURE__ */ React.createElement(Markdown, { remarkPlugins: [remarkGfm] }, message.content) : /* @__PURE__ */ React.createElement("em", { className: "text-xs opacity-70" }, message.content)
20567
+ ), message.role !== "system" && timeText && /* @__PURE__ */ React.createElement("div", { className: "text-xs opacity-70", style: { color: timeTextColor, marginBottom: isMobile ? "6px" : "4px" } }, timeText));
20568
+ }), localLoading && /* @__PURE__ */ React.createElement("div", { className: "flex justify-start" }, /* @__PURE__ */ React.createElement(
20448
20569
  "div",
20449
20570
  {
20450
20571
  className: "text-left p-3 inline-block max-w-[85%]",
@@ -20461,7 +20582,7 @@
20461
20582
  /* @__PURE__ */ React.createElement(
20462
20583
  "div",
20463
20584
  {
20464
- className: "flex items-center gap-2 border-t flex-shrink-0",
20585
+ className: "flex flex-col gap-2 border-t flex-shrink-0",
20465
20586
  style: {
20466
20587
  backgroundColor: inputAreaConfig.background_color || "#FFF",
20467
20588
  borderColor: inputAreaConfig.border_color || "#D1D5DB",
@@ -20471,7 +20592,7 @@
20471
20592
  gap: isMobile ? "8px" : "12px"
20472
20593
  }
20473
20594
  },
20474
- /* @__PURE__ */ React.createElement(
20595
+ /* @__PURE__ */ React.createElement("div", { className: "flex items-center gap-2 w-full" }, /* @__PURE__ */ React.createElement(
20475
20596
  "input",
20476
20597
  {
20477
20598
  ref: inputRef,
@@ -20491,11 +20612,10 @@
20491
20612
  },
20492
20613
  placeholder: inputAreaConfig.placeholder_text || "Type your message..."
20493
20614
  }
20494
- ),
20495
- /* @__PURE__ */ React.createElement(
20615
+ ), /* @__PURE__ */ React.createElement(
20496
20616
  "button",
20497
20617
  {
20498
- onClick: performSend,
20618
+ onClick: performSendWithContact,
20499
20619
  disabled: localLoading || !inputMessage.trim(),
20500
20620
  className: "rounded-full flex items-center justify-center flex-shrink-0 hover:opacity-90 transition-opacity",
20501
20621
  style: {
@@ -20504,7 +20624,7 @@
20504
20624
  height: `${sendButtonSize}px`,
20505
20625
  minWidth: `${sendButtonSize}px`,
20506
20626
  minHeight: `${sendButtonSize}px`,
20507
- opacity: localLoading || !inputMessage.trim() ? 0.5 : 1,
20627
+ opacity: localLoading || !inputMessage.trim() || !isContactValid ? 0.5 : 1,
20508
20628
  transition: "opacity 0.2s",
20509
20629
  border: "none",
20510
20630
  cursor: "pointer"
@@ -20533,7 +20653,47 @@
20533
20653
  if (iconUrl) return /* @__PURE__ */ React.createElement("img", { src: iconUrl, alt: "send", style: { width: "60%", height: "60%" } });
20534
20654
  return /* @__PURE__ */ React.createElement(Send, { className: "w-4 h-4", style: { color: "#FFFFFF" } });
20535
20655
  })()
20536
- )
20656
+ )),
20657
+ showContactFields && /* @__PURE__ */ React.createElement("div", { className: "p-3 border rounded-lg mt-2", style: {
20658
+ borderColor: showContactErrors && (!contactName.trim() || !contactEmail.trim()) ? "#EF4444" : inputAreaConfig.border_color || "#D1D5DB",
20659
+ backgroundColor: inputAreaConfig.background_color || "#FFF"
20660
+ } }, /* @__PURE__ */ React.createElement("div", { className: "text-sm font-semibold mb-2", style: { color: inputAreaConfig.text_color || "#111827" } }, "How should we reach you?"), /* @__PURE__ */ React.createElement("div", { className: "space-y-2" }, /* @__PURE__ */ React.createElement(
20661
+ "input",
20662
+ {
20663
+ type: "text",
20664
+ placeholder: "Name",
20665
+ value: contactName,
20666
+ onChange: (e) => {
20667
+ setContactName(e.target.value);
20668
+ if (showContactErrors) setShowContactErrors(false);
20669
+ },
20670
+ className: "w-full px-3 py-1.5 border rounded-md",
20671
+ style: {
20672
+ borderColor: showContactErrors && !contactName.trim() ? "#EF4444" : inputAreaConfig.border_color || "#D1D5DB",
20673
+ color: inputAreaConfig.text_color || "#111827",
20674
+ fontSize: "13px",
20675
+ backgroundColor: inputAreaConfig.background_color || "#FFF"
20676
+ }
20677
+ }
20678
+ ), /* @__PURE__ */ React.createElement(
20679
+ "input",
20680
+ {
20681
+ type: "email",
20682
+ placeholder: "Email address",
20683
+ value: contactEmail,
20684
+ onChange: (e) => {
20685
+ setContactEmail(e.target.value);
20686
+ if (showContactErrors) setShowContactErrors(false);
20687
+ },
20688
+ className: "w-full px-3 py-1.5 border rounded-md",
20689
+ style: {
20690
+ borderColor: showContactErrors && !contactEmail.trim() ? "#EF4444" : inputAreaConfig.border_color || "#D1D5DB",
20691
+ color: inputAreaConfig.text_color || "#111827",
20692
+ fontSize: "13px",
20693
+ backgroundColor: inputAreaConfig.background_color || "#FFF"
20694
+ }
20695
+ }
20696
+ ), showContactErrors && (!contactName.trim() || !contactEmail.trim()) && /* @__PURE__ */ React.createElement("div", { className: "text-xs", style: { color: "#EF4444" } }, `${!contactName.trim() ? "Name is required. " : ""}${!contactEmail.trim() ? "Email is required." : ""}`)))
20537
20697
  ),
20538
20698
  inputAreaConfig.show_branding !== false && /* @__PURE__ */ React.createElement(
20539
20699
  "div",
@@ -20629,7 +20789,7 @@
20629
20789
  }
20630
20790
  `));
20631
20791
  };
20632
- function init({ widgetId, apiKey, mode = "inline" }) {
20792
+ function init({ widgetId, apiKey, mode = "inline", name: name2 = null, email = null, unique_id = null }) {
20633
20793
  const container = document.getElementById("livechat-root");
20634
20794
  if (!container) {
20635
20795
  console.error("Live Chat Widget root element (#livechat-root) not found.");
@@ -20649,7 +20809,16 @@
20649
20809
  const root2 = client.createRoot(mountPoint);
20650
20810
  try {
20651
20811
  root2.render(
20652
- /* @__PURE__ */ React.createElement(React.StrictMode, null, /* @__PURE__ */ React.createElement(LiveChatWidget, { widgetId, apiKey }))
20812
+ /* @__PURE__ */ React.createElement(React.StrictMode, null, /* @__PURE__ */ React.createElement(
20813
+ LiveChatWidget,
20814
+ {
20815
+ widgetId,
20816
+ apiKey,
20817
+ name: name2,
20818
+ email,
20819
+ unique_id
20820
+ }
20821
+ ))
20653
20822
  );
20654
20823
  container.__livechat_mounted = true;
20655
20824
  } catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getwidgets/live-chat-widget",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "main": "dist/live-chat-widget.umd.js",
5
5
  "unpkg": "dist/live-chat-widget.umd.js",
6
6
  "type": "module",