@xbrain-swe/nova-chat-widget 0.2.4-dev.15 → 0.3.0-dev.16

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.
package/dist/index.js CHANGED
@@ -70,12 +70,22 @@ function InternalProvider({
70
70
  const sessionId = auth.sessionKey || null;
71
71
  const latestRef = useRef({ auth, visitorId, sessionId });
72
72
  latestRef.current = { auth, visitorId, sessionId };
73
- const sendEvent = useCallback((kind, fields = {}) => {
73
+ const sendEvent = useCallback((event, properties = {}) => {
74
74
  const { auth: a, visitorId: v, sessionId: sid } = latestRef.current;
75
- fetch(`${a.apiBaseUrl}/v1/nudge-events`, {
75
+ if (!v) return;
76
+ const { conversation_id: conversationId, ...rest } = properties;
77
+ fetch(`${a.apiBaseUrl}/v1/events`, {
76
78
  method: "POST",
77
- headers: { "Content-Type": "application/json" },
78
- body: JSON.stringify({ app_id: a.appId, kind, visitor_id: v || null, session_id: sid, ...fields })
79
+ keepalive: true,
80
+ headers: { "Content-Type": "application/json", Authorization: `Bearer ${a.apiKey}` },
81
+ body: JSON.stringify({
82
+ app_id: a.appId,
83
+ event,
84
+ visitor_id: v,
85
+ session_id: sid,
86
+ conversation_id: conversationId ?? null,
87
+ properties: { screen: a.screen, ...rest }
88
+ })
79
89
  }).catch(() => {
80
90
  });
81
91
  }, []);
@@ -85,8 +95,7 @@ function InternalProvider({
85
95
  const send = () => {
86
96
  if (widgetViewSentRef.current) return;
87
97
  widgetViewSentRef.current = true;
88
- const screen = latestRef.current.auth.screen;
89
- sendEvent("widget_view", screen ? { payload: { screen } } : {});
98
+ sendEvent("widget_view");
90
99
  };
91
100
  if (sessionId) {
92
101
  send();
@@ -1274,6 +1283,7 @@ function useXbNovaChat(options) {
1274
1283
  }
1275
1284
  );
1276
1285
  if (!res.ok) throw new Error(`${res.status}`);
1286
+ sendEvent("feedback_submit", { conversation_id: conversationId, message_id: messageId, rating, reason });
1277
1287
  options?.onFeedback?.({ conversationId, messageId, rating, reason });
1278
1288
  } catch (e) {
1279
1289
  console.warn("[xbnova] \uD53C\uB4DC\uBC31 \uC800\uC7A5 \uC2E4\uD328", e);
@@ -1283,7 +1293,7 @@ function useXbNovaChat(options) {
1283
1293
  function postClientActionEvent(kind, action, label) {
1284
1294
  const conversationId = conversationIdRef.current;
1285
1295
  if (!conversationId) return;
1286
- sendEvent(kind, { conversation_id: conversationId, payload: label ? { action, label } : { action } });
1296
+ sendEvent(kind, { conversation_id: conversationId, action, label });
1287
1297
  }
1288
1298
  function trackClientActionClick(action, label) {
1289
1299
  postClientActionEvent("cta_click", action, label);
@@ -1362,6 +1372,11 @@ function useXbNovaChat(options) {
1362
1372
  async function send(text) {
1363
1373
  const trimmed = text.trim();
1364
1374
  if (!trimmed || busy) return;
1375
+ sendEvent("message_send", {
1376
+ conversation_id: conversationIdRef.current,
1377
+ is_first: messages.length === 0,
1378
+ length: trimmed.length
1379
+ });
1365
1380
  setError(null);
1366
1381
  setBusy(true);
1367
1382
  setMessages((prev) => [
@@ -1799,12 +1814,17 @@ function XbNovaChat({
1799
1814
  markDisclosureSeen();
1800
1815
  }
1801
1816
  setOpen(true);
1817
+ sendEvent("chat_open", { from_nudge: Boolean(consumed) });
1802
1818
  try {
1803
1819
  onOpen?.({ fromNudge: Boolean(consumed), hintText: consumed?.text });
1804
1820
  } catch (err) {
1805
1821
  console.warn("[XbNova] onOpen \uCF5C\uBC31 \uC624\uB958", err);
1806
1822
  }
1807
1823
  }
1824
+ function closeSheet() {
1825
+ setOpen(false);
1826
+ sendEvent("chat_close");
1827
+ }
1808
1828
  if (!open) {
1809
1829
  if (barHidden) return null;
1810
1830
  const showHint = Boolean(hint && hintsEnabled);
@@ -1909,7 +1929,7 @@ function XbNovaChat({
1909
1929
  transition: "opacity 260ms ease-out",
1910
1930
  ...backdropStyle
1911
1931
  },
1912
- children: /* @__PURE__ */ jsx7("div", { className: "h-full w-full bg-black/45", onClick: () => setOpen(false) })
1932
+ children: /* @__PURE__ */ jsx7("div", { className: "h-full w-full bg-black/45", onClick: closeSheet })
1913
1933
  }
1914
1934
  ) : null,
1915
1935
  /* @__PURE__ */ jsx7(
@@ -1945,7 +1965,7 @@ function XbNovaChat({
1945
1965
  "button",
1946
1966
  {
1947
1967
  type: "button",
1948
- onClick: () => setOpen(false),
1968
+ onClick: closeSheet,
1949
1969
  "aria-label": "\uCC44\uD305 \uB2EB\uAE30",
1950
1970
  className: "shrink-0 text-neutral-400 hover:text-neutral-700 dark:hover:text-neutral-200",
1951
1971
  children: "\xD7"