@asgard-js/react 0.0.8 → 0.0.9

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.
@@ -1 +1 @@
1
- {"version":3,"file":"hint-template.d.ts","sourceRoot":"","sources":["../../../../src/components/templates/hint-template/hint-template.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAGlC,OAAO,EAAE,mBAAmB,EAAuB,MAAM,iBAAiB,CAAC;AAG3E,UAAU,iBAAiB;IACzB,OAAO,EAAE,mBAAmB,CAAC;CAC9B;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG,SAAS,CAuChE"}
1
+ {"version":3,"file":"hint-template.d.ts","sourceRoot":"","sources":["../../../../src/components/templates/hint-template/hint-template.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAe,MAAM,OAAO,CAAC;AAG/C,OAAO,EAEL,mBAAmB,EAEpB,MAAM,iBAAiB,CAAC;AAGzB,UAAU,iBAAiB;IACzB,OAAO,EAAE,mBAAmB,CAAC;CAC9B;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG,SAAS,CA2ChE"}
@@ -2,12 +2,12 @@ import { ConversationErrorMessage } from '@asgard-js/core';
2
2
  import { PropsWithChildren, ReactNode } from 'react';
3
3
 
4
4
  export interface AsgardTemplateContextValue {
5
- onErrorClick?: () => void;
5
+ onErrorClick?: (message: ConversationErrorMessage) => void;
6
6
  errorMessageRenderer?: (message: ConversationErrorMessage) => ReactNode;
7
7
  }
8
8
  export declare const AsgardTemplateContext: import('react').Context<AsgardTemplateContextValue>;
9
9
  interface AsgardTemplateContextProviderProps extends PropsWithChildren {
10
- onErrorClick?: () => void;
10
+ onErrorClick?: (message: ConversationErrorMessage) => void;
11
11
  errorMessageRenderer?: (message: ConversationErrorMessage) => ReactNode;
12
12
  }
13
13
  export declare function AsgardTemplateContextProvider(props: AsgardTemplateContextProviderProps): ReactNode;
@@ -1 +1 @@
1
- {"version":3,"file":"asgard-template-context.d.ts","sourceRoot":"","sources":["../../src/context/asgard-template-context.tsx"],"names":[],"mappings":"AAAA,OAAO,EAEL,iBAAiB,EACjB,SAAS,EAGV,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAC;AAE3D,MAAM,WAAW,0BAA0B;IACzC,YAAY,CAAC,EAAE,MAAM,IAAI,CAAC;IAC1B,oBAAoB,CAAC,EAAE,CAAC,OAAO,EAAE,wBAAwB,KAAK,SAAS,CAAC;CACzE;AAED,eAAO,MAAM,qBAAqB,qDAGhC,CAAC;AAEH,UAAU,kCAAmC,SAAQ,iBAAiB;IACpE,YAAY,CAAC,EAAE,MAAM,IAAI,CAAC;IAC1B,oBAAoB,CAAC,EAAE,CAAC,OAAO,EAAE,wBAAwB,KAAK,SAAS,CAAC;CACzE;AAED,wBAAgB,6BAA6B,CAC3C,KAAK,EAAE,kCAAkC,GACxC,SAAS,CAaX;AAED,wBAAgB,wBAAwB,IAAI,0BAA0B,CAErE"}
1
+ {"version":3,"file":"asgard-template-context.d.ts","sourceRoot":"","sources":["../../src/context/asgard-template-context.tsx"],"names":[],"mappings":"AAAA,OAAO,EAEL,iBAAiB,EACjB,SAAS,EAGV,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAC;AAE3D,MAAM,WAAW,0BAA0B;IACzC,YAAY,CAAC,EAAE,CAAC,OAAO,EAAE,wBAAwB,KAAK,IAAI,CAAC;IAC3D,oBAAoB,CAAC,EAAE,CAAC,OAAO,EAAE,wBAAwB,KAAK,SAAS,CAAC;CACzE;AAED,eAAO,MAAM,qBAAqB,qDAGhC,CAAC;AAEH,UAAU,kCAAmC,SAAQ,iBAAiB;IACpE,YAAY,CAAC,EAAE,CAAC,OAAO,EAAE,wBAAwB,KAAK,IAAI,CAAC;IAC3D,oBAAoB,CAAC,EAAE,CAAC,OAAO,EAAE,wBAAwB,KAAK,SAAS,CAAC;CACzE;AAED,wBAAgB,6BAA6B,CAC3C,KAAK,EAAE,kCAAkC,GACxC,SAAS,CAaX;AAED,wBAAgB,wBAAwB,IAAI,0BAA0B,CAErE"}
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { jsx as c, jsxs as m, Fragment as mt } from "react/jsx-runtime";
2
2
  import * as g from "react";
3
- import { createContext as F, useMemo as v, useContext as W, useRef as R, useEffect as C, useState as w, useCallback as p, useLayoutEffect as rt, memo as ft } from "react";
4
- import { AsgardServiceClient as bt, EventType as H, Conversation as pt, Channel as Ct, MessageTemplateType as O } from "@asgard-js/core";
3
+ import { createContext as F, useMemo as v, useContext as W, useRef as R, useEffect as C, useState as w, useCallback as b, useLayoutEffect as rt, memo as ft } from "react";
4
+ import { AsgardServiceClient as bt, EventType as V, Conversation as pt, Channel as Ct, MessageTemplateType as O } from "@asgard-js/core";
5
5
  function X(n) {
6
6
  return n !== null && typeof n == "object" && !Array.isArray(n);
7
7
  }
@@ -52,15 +52,15 @@ function xt(n) {
52
52
  return i.current || (i.current = new bt(t)), C(() => () => {
53
53
  i.current && (i.current.close(), i.current = null);
54
54
  }, []), C(() => {
55
- !i.current || !o || i.current.on(H.INIT, o);
55
+ !i.current || !o || i.current.on(V.INIT, o);
56
56
  }, [o]), C(() => {
57
- !i.current || !e || i.current.on(H.PROCESS, e);
57
+ !i.current || !e || i.current.on(V.PROCESS, e);
58
58
  }, [e]), C(() => {
59
- !i.current || !r || i.current.on(H.MESSAGE, r);
59
+ !i.current || !r || i.current.on(V.MESSAGE, r);
60
60
  }, [r]), C(() => {
61
- !i.current || !s || i.current.on(H.DONE, s);
61
+ !i.current || !s || i.current.on(V.DONE, s);
62
62
  }, [s]), C(() => {
63
- !i.current || !a || i.current.on(H.ERROR, a);
63
+ !i.current || !a || i.current.on(V.ERROR, a);
64
64
  }, [a]), i.current;
65
65
  }
66
66
  function wt(n) {
@@ -69,10 +69,10 @@ function wt(n) {
69
69
  throw new Error("Client instance is required");
70
70
  if (!o)
71
71
  throw new Error("Custom channel id is required");
72
- const [s, a] = w(null), [i, u] = w(!0), [d, h] = w(!1), [_, l] = w(!1), [b, f] = w(null), x = p(async () => {
72
+ const [s, a] = w(null), [i, u] = w(!0), [d, h] = w(!1), [_, l] = w(!1), [p, f] = w(null), x = b(async () => {
73
73
  const k = new pt({
74
74
  messages: new Map(
75
- r == null ? void 0 : r.map((V) => [V.messageId, V])
75
+ r == null ? void 0 : r.map((H) => [H.messageId, H])
76
76
  )
77
77
  });
78
78
  h(!0), l(!0), f(k);
@@ -82,8 +82,8 @@ function wt(n) {
82
82
  customChannelId: o,
83
83
  customMessageId: e,
84
84
  conversation: k,
85
- statesObserver: (V) => {
86
- l(V.isConnecting), f(V.conversation);
85
+ statesObserver: (H) => {
86
+ l(H.isConnecting), f(H.conversation);
87
87
  }
88
88
  },
89
89
  {
@@ -96,9 +96,9 @@ function wt(n) {
96
96
  }
97
97
  );
98
98
  a(gt);
99
- }, [t, o, e, r]), N = p(() => {
99
+ }, [t, o, e, r]), N = b(() => {
100
100
  a((k) => (k == null || k.close(), null)), u(!1), h(!1), l(!1), f(null);
101
- }, []), K = p(
101
+ }, []), K = b(
102
102
  (k) => s == null ? void 0 : s.sendMessage({ text: k }),
103
103
  [s]
104
104
  );
@@ -109,7 +109,7 @@ function wt(n) {
109
109
  isOpen: i,
110
110
  isResetting: d,
111
111
  isConnecting: _,
112
- conversation: b,
112
+ conversation: p,
113
113
  sendMessage: K,
114
114
  resetChannel: x,
115
115
  closeChannel: N
@@ -118,7 +118,7 @@ function wt(n) {
118
118
  i,
119
119
  d,
120
120
  _,
121
- b,
121
+ p,
122
122
  K,
123
123
  x,
124
124
  N
@@ -139,7 +139,7 @@ function Tt() {
139
139
  return window.visualViewport ? [window.visualViewport.width, window.visualViewport.height] : [window.innerWidth, window.innerHeight];
140
140
  }
141
141
  function Et() {
142
- const [n, t] = w(), o = p(() => {
142
+ const [n, t] = w(), o = b(() => {
143
143
  const e = Tt();
144
144
  t(
145
145
  (r) => r && r[0] === e[0] && r[1] === e[1] ? r : e
@@ -231,7 +231,7 @@ function St(n) {
231
231
  }
232
232
  const D = typeof window < "u" ? rt : null;
233
233
  function Rt(n) {
234
- const t = p(() => {
234
+ const t = b(() => {
235
235
  const o = window.innerHeight * 0.01;
236
236
  n.current && n.current.style.setProperty("--vh", `${o}px`);
237
237
  }, [n]);
@@ -281,7 +281,7 @@ function Mt(n) {
281
281
  isResetting: h,
282
282
  isConnecting: _,
283
283
  conversation: l,
284
- sendMessage: b,
284
+ sendMessage: p,
285
285
  resetChannel: f,
286
286
  closeChannel: x
287
287
  } = wt({
@@ -296,7 +296,7 @@ function Mt(n) {
296
296
  isResetting: h,
297
297
  isConnecting: _,
298
298
  messages: (l == null ? void 0 : l.messages) ?? null,
299
- sendMessage: b,
299
+ sendMessage: p,
300
300
  resetChannel: f,
301
301
  closeChannel: x,
302
302
  botTypingPlaceholder: r,
@@ -309,7 +309,7 @@ function Mt(n) {
309
309
  h,
310
310
  _,
311
311
  l == null ? void 0 : l.messages,
312
- b,
312
+ p,
313
313
  f,
314
314
  x,
315
315
  r
@@ -331,11 +331,11 @@ function zt(n) {
331
331
  );
332
332
  return /* @__PURE__ */ c(ut.Provider, { value: r, children: t });
333
333
  }
334
- function Vt() {
334
+ function Ht() {
335
335
  return W(ut);
336
336
  }
337
- const Ht = "_chatbot_header_luxt4_1", At = "_chatbot_header__content_luxt4_4", Ot = "_chatbot_header__title_luxt4_12", Bt = "_chatbot_header__extra_luxt4_23", P = {
338
- chatbot_header: Ht,
337
+ const Vt = "_chatbot_header_luxt4_1", At = "_chatbot_header__content_luxt4_4", Ot = "_chatbot_header__title_luxt4_12", Bt = "_chatbot_header__extra_luxt4_23", P = {
338
+ chatbot_header: Vt,
339
339
  chatbot_header__content: At,
340
340
  chatbot_header__title: Ot,
341
341
  chatbot_header__extra: Bt
@@ -363,12 +363,12 @@ function Dt(n) {
363
363
  borderBottomColor: r == null ? void 0 : r.borderColor
364
364
  }),
365
365
  [r]
366
- ), h = p(
366
+ ), h = b(
367
367
  (l) => {
368
368
  a || (l.stopPropagation(), o == null || o(), i == null || i());
369
369
  },
370
370
  [a, o, i]
371
- ), _ = p(
371
+ ), _ = b(
372
372
  (l) => {
373
373
  a || (l.stopPropagation(), e == null || e(), u == null || u());
374
374
  },
@@ -432,7 +432,7 @@ const jt = "_template_box_content_fs2wk_1", Ut = "_content_fs2wk_8", G = {
432
432
  quick_reply: Xt
433
433
  };
434
434
  function Yt(n) {
435
- const { quickReplies: t } = n, { sendMessage: o, isConnecting: e } = T(), r = p(
435
+ const { quickReplies: t } = n, { sendMessage: o, isConnecting: e } = T(), r = b(
436
436
  (s) => {
437
437
  o == null || o(s);
438
438
  },
@@ -496,7 +496,7 @@ function _t(n) {
496
496
  default:
497
497
  return "1.51 / 1";
498
498
  }
499
- }, [t]), s = p(
499
+ }, [t]), s = b(
500
500
  (i) => function() {
501
501
  switch (i.type) {
502
502
  case "message":
@@ -601,7 +601,7 @@ function et(n) {
601
601
  ] });
602
602
  }
603
603
  function ht(n) {
604
- const { isTyping: t, typingText: o } = n, { messageBoxBottomRef: e, avatar: r } = T(), s = L(), a = R(null), i = p(() => {
604
+ const { isTyping: t, typingText: o } = n, { messageBoxBottomRef: e, avatar: r } = T(), s = L(), a = R(null), i = b(() => {
605
605
  var _;
606
606
  (_ = e.current) == null || _.scrollIntoView({ behavior: "smooth" });
607
607
  }, [e]);
@@ -673,7 +673,9 @@ const he = "_hint_root_1bw94_1", ge = "_error_hint_title_1bw94_16", me = "_error
673
673
  time: fe
674
674
  };
675
675
  function ot(n) {
676
- const { message: t } = n, { onErrorClick: o, errorMessageRenderer: e } = Vt();
676
+ const { message: t } = n, { onErrorClick: o, errorMessageRenderer: e } = Ht(), r = b(() => {
677
+ o == null || o(t);
678
+ }, [t, o]);
677
679
  if (t.type === "user") return null;
678
680
  if (t.type === "error")
679
681
  return /* @__PURE__ */ c("div", { className: z.hint_root, children: (e == null ? void 0 : e(t)) ?? /* @__PURE__ */ m(mt, { children: [
@@ -685,7 +687,7 @@ function ot(n) {
685
687
  "div",
686
688
  {
687
689
  className: z.error_hint_message,
688
- onClick: o,
690
+ onClick: r,
689
691
  children: [
690
692
  "Click ",
691
693
  /* @__PURE__ */ c("span", { children: "here" }),
@@ -694,10 +696,10 @@ function ot(n) {
694
696
  }
695
697
  )
696
698
  ] }) });
697
- const r = t.message.template;
698
- return r.type !== O.HINT ? null : /* @__PURE__ */ m("div", { className: z.hint_root, children: [
699
+ const s = t.message.template;
700
+ return s.type !== O.HINT ? null : /* @__PURE__ */ m("div", { className: z.hint_root, children: [
699
701
  /* @__PURE__ */ c("div", { className: z.time, children: $(t.time) }),
700
- r.text
702
+ s.text
701
703
  ] });
702
704
  }
703
705
  function be(n) {
@@ -769,17 +771,17 @@ function ke(n) {
769
771
  C(() => {
770
772
  const l = window.SpeechRecognition || window.webkitSpeechRecognition;
771
773
  if (!l) return;
772
- const b = new l();
773
- b.lang = "zh-TW", b.continuous = !0, b.interimResults = !0, b.onresult = (f) => {
774
+ const p = new l();
775
+ p.lang = "zh-TW", p.continuous = !0, p.interimResults = !0, p.onresult = (f) => {
774
776
  for (let x = f.resultIndex; x < f.results.length; x++)
775
777
  f.results[x].isFinal && t((N) => N + f.results[x][0].transcript);
776
- }, b.onerror = (f) => {
778
+ }, p.onerror = (f) => {
777
779
  alert(`語音識別錯誤: ${JSON.stringify(f.error)}`);
778
- }, b.onend = () => {
780
+ }, p.onend = () => {
779
781
  r(!1);
780
- }, s.current = b;
782
+ }, s.current = p;
781
783
  }, [t]);
782
- const a = p(() => {
784
+ const a = b(() => {
783
785
  if (!s.current) {
784
786
  alert("無法開始辨識: 語音識別器未初始化,請檢查是否有授權使用麥克風");
785
787
  return;
@@ -789,24 +791,24 @@ function ke(n) {
789
791
  } catch (l) {
790
792
  alert(`無法開始辨識: ${JSON.stringify(l)}`);
791
793
  }
792
- }, []), i = p(() => {
794
+ }, []), i = b(() => {
793
795
  s.current && (s.current.stop(), r(!1));
794
- }, []), u = p(
796
+ }, []), u = b(
795
797
  (l) => {
796
798
  e || (l.preventDefault(), a());
797
799
  },
798
800
  [e, a]
799
- ), d = p(
801
+ ), d = b(
800
802
  (l) => {
801
803
  e && (l.preventDefault(), i());
802
804
  },
803
805
  [e, i]
804
- ), h = p(
806
+ ), h = b(
805
807
  (l) => {
806
808
  e || (l.preventDefault(), a());
807
809
  },
808
810
  [e, a]
809
- ), _ = p(
811
+ ), _ = b(
810
812
  (l) => {
811
813
  e && (l.preventDefault(), i());
812
814
  },
@@ -834,19 +836,19 @@ function Se() {
834
836
  borderTopColor: o == null ? void 0 : o.borderColor
835
837
  }),
836
838
  [o]
837
- ), h = p(
838
- (b) => {
839
- const f = b.target, x = f.value;
840
- f.style.height = "36px", x && (f.style.height = `${f.scrollHeight}px`), r(b.target.value);
839
+ ), h = b(
840
+ (p) => {
841
+ const f = p.target, x = f.value;
842
+ f.style.height = "36px", x && (f.style.height = `${f.scrollHeight}px`), r(p.target.value);
841
843
  },
842
844
  []
843
- ), _ = p(() => {
845
+ ), _ = b(() => {
844
846
  !s && !t && (n == null || n(e), r(""), i.current && (i.current.style.height = "36px"));
845
- }, [s, t, n, e]), l = p(
846
- (b) => {
847
- if (b.key === "Enter" && !s && !t && e.trim()) {
847
+ }, [s, t, n, e]), l = b(
848
+ (p) => {
849
+ if (p.key === "Enter" && !s && !t && e.trim()) {
848
850
  n == null || n(e), r("");
849
- const f = b.target;
851
+ const f = p.target;
850
852
  f.style.height = "36px";
851
853
  }
852
854
  },
@@ -897,7 +899,7 @@ const Re = "_chatbot_root_1t5cx_1", Le = "_full_screen_1t5cx_122", Me = "_chatbo
897
899
  chatbot_container: Me,
898
900
  screen_keyboard_open: ze
899
901
  };
900
- function Ve(n) {
902
+ function He(n) {
901
903
  const { children: t } = n, o = R(null), e = L();
902
904
  St(o), kt();
903
905
  const [, r] = Et() ?? [], s = Nt(), a = v(() => {
@@ -922,11 +924,11 @@ function Ve(n) {
922
924
  }
923
925
  ) });
924
926
  }
925
- function He(n) {
927
+ function Ve(n) {
926
928
  const { fullScreen: t, children: o } = n, e = R(null);
927
929
  Rt(e);
928
930
  const r = L();
929
- return /* @__PURE__ */ c("div", { ref: e, className: B.chatbot_root, children: t ? /* @__PURE__ */ c(Ve, { children: o }) : /* @__PURE__ */ c("div", { className: B.chatbot_container, style: r == null ? void 0 : r.chatbot, children: o }) });
931
+ return /* @__PURE__ */ c("div", { ref: e, className: B.chatbot_root, children: t ? /* @__PURE__ */ c(He, { children: o }) : /* @__PURE__ */ c("div", { className: B.chatbot_container, style: r == null ? void 0 : r.chatbot, children: o }) });
930
932
  }
931
933
  function Ie(n) {
932
934
  const {
@@ -951,7 +953,7 @@ function Ie(n) {
951
953
  customChannelId: r,
952
954
  initMessages: s,
953
955
  botTypingPlaceholder: u,
954
- children: /* @__PURE__ */ m(He, { fullScreen: a, children: [
956
+ children: /* @__PURE__ */ m(Ve, { fullScreen: a, children: [
955
957
  /* @__PURE__ */ c(Dt, { title: t, onReset: d, onClose: h }),
956
958
  /* @__PURE__ */ c(
957
959
  zt,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asgard-js/react",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },