@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.cjs CHANGED
@@ -117,12 +117,22 @@ function InternalProvider({
117
117
  const sessionId = auth.sessionKey || null;
118
118
  const latestRef = (0, import_react.useRef)({ auth, visitorId, sessionId });
119
119
  latestRef.current = { auth, visitorId, sessionId };
120
- const sendEvent = (0, import_react.useCallback)((kind, fields = {}) => {
120
+ const sendEvent = (0, import_react.useCallback)((event, properties = {}) => {
121
121
  const { auth: a, visitorId: v, sessionId: sid } = latestRef.current;
122
- fetch(`${a.apiBaseUrl}/v1/nudge-events`, {
122
+ if (!v) return;
123
+ const { conversation_id: conversationId, ...rest } = properties;
124
+ fetch(`${a.apiBaseUrl}/v1/events`, {
123
125
  method: "POST",
124
- headers: { "Content-Type": "application/json" },
125
- body: JSON.stringify({ app_id: a.appId, kind, visitor_id: v || null, session_id: sid, ...fields })
126
+ keepalive: true,
127
+ headers: { "Content-Type": "application/json", Authorization: `Bearer ${a.apiKey}` },
128
+ body: JSON.stringify({
129
+ app_id: a.appId,
130
+ event,
131
+ visitor_id: v,
132
+ session_id: sid,
133
+ conversation_id: conversationId ?? null,
134
+ properties: { screen: a.screen, ...rest }
135
+ })
126
136
  }).catch(() => {
127
137
  });
128
138
  }, []);
@@ -132,8 +142,7 @@ function InternalProvider({
132
142
  const send = () => {
133
143
  if (widgetViewSentRef.current) return;
134
144
  widgetViewSentRef.current = true;
135
- const screen = latestRef.current.auth.screen;
136
- sendEvent("widget_view", screen ? { payload: { screen } } : {});
145
+ sendEvent("widget_view");
137
146
  };
138
147
  if (sessionId) {
139
148
  send();
@@ -1322,6 +1331,7 @@ function useXbNovaChat(options) {
1322
1331
  }
1323
1332
  );
1324
1333
  if (!res.ok) throw new Error(`${res.status}`);
1334
+ sendEvent("feedback_submit", { conversation_id: conversationId, message_id: messageId, rating, reason });
1325
1335
  options?.onFeedback?.({ conversationId, messageId, rating, reason });
1326
1336
  } catch (e) {
1327
1337
  console.warn("[xbnova] \uD53C\uB4DC\uBC31 \uC800\uC7A5 \uC2E4\uD328", e);
@@ -1331,7 +1341,7 @@ function useXbNovaChat(options) {
1331
1341
  function postClientActionEvent(kind, action, label) {
1332
1342
  const conversationId = conversationIdRef.current;
1333
1343
  if (!conversationId) return;
1334
- sendEvent(kind, { conversation_id: conversationId, payload: label ? { action, label } : { action } });
1344
+ sendEvent(kind, { conversation_id: conversationId, action, label });
1335
1345
  }
1336
1346
  function trackClientActionClick(action, label) {
1337
1347
  postClientActionEvent("cta_click", action, label);
@@ -1410,6 +1420,11 @@ function useXbNovaChat(options) {
1410
1420
  async function send(text) {
1411
1421
  const trimmed = text.trim();
1412
1422
  if (!trimmed || busy) return;
1423
+ sendEvent("message_send", {
1424
+ conversation_id: conversationIdRef.current,
1425
+ is_first: messages.length === 0,
1426
+ length: trimmed.length
1427
+ });
1413
1428
  setError(null);
1414
1429
  setBusy(true);
1415
1430
  setMessages((prev) => [
@@ -1847,12 +1862,17 @@ function XbNovaChat({
1847
1862
  markDisclosureSeen();
1848
1863
  }
1849
1864
  setOpen(true);
1865
+ sendEvent("chat_open", { from_nudge: Boolean(consumed) });
1850
1866
  try {
1851
1867
  onOpen?.({ fromNudge: Boolean(consumed), hintText: consumed?.text });
1852
1868
  } catch (err) {
1853
1869
  console.warn("[XbNova] onOpen \uCF5C\uBC31 \uC624\uB958", err);
1854
1870
  }
1855
1871
  }
1872
+ function closeSheet() {
1873
+ setOpen(false);
1874
+ sendEvent("chat_close");
1875
+ }
1856
1876
  if (!open) {
1857
1877
  if (barHidden) return null;
1858
1878
  const showHint = Boolean(hint && hintsEnabled);
@@ -1957,7 +1977,7 @@ function XbNovaChat({
1957
1977
  transition: "opacity 260ms ease-out",
1958
1978
  ...backdropStyle
1959
1979
  },
1960
- children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { className: "h-full w-full bg-black/45", onClick: () => setOpen(false) })
1980
+ children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { className: "h-full w-full bg-black/45", onClick: closeSheet })
1961
1981
  }
1962
1982
  ) : null,
1963
1983
  /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
@@ -1993,7 +2013,7 @@ function XbNovaChat({
1993
2013
  "button",
1994
2014
  {
1995
2015
  type: "button",
1996
- onClick: () => setOpen(false),
2016
+ onClick: closeSheet,
1997
2017
  "aria-label": "\uCC44\uD305 \uB2EB\uAE30",
1998
2018
  className: "shrink-0 text-neutral-400 hover:text-neutral-700 dark:hover:text-neutral-200",
1999
2019
  children: "\xD7"