@yext/chat-ui-react 0.10.1 → 0.11.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.
Files changed (68) hide show
  1. package/THIRD-PARTY-NOTICES +3 -3
  2. package/lib/bundle.css +1 -1
  3. package/lib/commonjs/package.json.js +1 -1
  4. package/lib/commonjs/src/components/ChatInput.d.ts.map +1 -1
  5. package/lib/commonjs/src/components/ChatInput.js +9 -2
  6. package/lib/commonjs/src/components/ChatInput.js.map +1 -1
  7. package/lib/commonjs/src/components/ChatPanel.d.ts +4 -0
  8. package/lib/commonjs/src/components/ChatPanel.d.ts.map +1 -1
  9. package/lib/commonjs/src/components/ChatPanel.js +7 -7
  10. package/lib/commonjs/src/components/ChatPanel.js.map +1 -1
  11. package/lib/commonjs/src/components/ChatPopUp.d.ts.map +1 -1
  12. package/lib/commonjs/src/components/ChatPopUp.js +19 -8
  13. package/lib/commonjs/src/components/ChatPopUp.js.map +1 -1
  14. package/lib/commonjs/src/components/Markdown.d.ts +3 -1
  15. package/lib/commonjs/src/components/Markdown.d.ts.map +1 -1
  16. package/lib/commonjs/src/components/Markdown.js +3 -2
  17. package/lib/commonjs/src/components/Markdown.js.map +1 -1
  18. package/lib/commonjs/src/components/MessageBubble.d.ts +3 -1
  19. package/lib/commonjs/src/components/MessageBubble.d.ts.map +1 -1
  20. package/lib/commonjs/src/components/MessageBubble.js +3 -3
  21. package/lib/commonjs/src/components/MessageBubble.js.map +1 -1
  22. package/lib/commonjs/src/components/MessageSuggestions.d.ts.map +1 -1
  23. package/lib/commonjs/src/components/MessageSuggestions.js +8 -1
  24. package/lib/commonjs/src/components/MessageSuggestions.js.map +1 -1
  25. package/lib/commonjs/src/hooks/useFetchInitialMessage.d.ts.map +1 -1
  26. package/lib/commonjs/src/hooks/useFetchInitialMessage.js +9 -1
  27. package/lib/commonjs/src/hooks/useFetchInitialMessage.js.map +1 -1
  28. package/lib/commonjs/src/hooks/useSendMessageWithRetries.d.ts.map +1 -1
  29. package/lib/commonjs/src/hooks/useSendMessageWithRetries.js +3 -1
  30. package/lib/commonjs/src/hooks/useSendMessageWithRetries.js.map +1 -1
  31. package/lib/esm/index.d.ts +7 -1
  32. package/lib/esm/package.json.mjs +1 -1
  33. package/lib/esm/src/components/ChatInput.d.ts.map +1 -1
  34. package/lib/esm/src/components/ChatInput.mjs +9 -2
  35. package/lib/esm/src/components/ChatInput.mjs.map +1 -1
  36. package/lib/esm/src/components/ChatPanel.d.ts +4 -0
  37. package/lib/esm/src/components/ChatPanel.d.ts.map +1 -1
  38. package/lib/esm/src/components/ChatPanel.mjs +7 -7
  39. package/lib/esm/src/components/ChatPanel.mjs.map +1 -1
  40. package/lib/esm/src/components/ChatPopUp.d.ts.map +1 -1
  41. package/lib/esm/src/components/ChatPopUp.mjs +19 -8
  42. package/lib/esm/src/components/ChatPopUp.mjs.map +1 -1
  43. package/lib/esm/src/components/Markdown.d.ts +3 -1
  44. package/lib/esm/src/components/Markdown.d.ts.map +1 -1
  45. package/lib/esm/src/components/Markdown.mjs +3 -2
  46. package/lib/esm/src/components/Markdown.mjs.map +1 -1
  47. package/lib/esm/src/components/MessageBubble.d.ts +3 -1
  48. package/lib/esm/src/components/MessageBubble.d.ts.map +1 -1
  49. package/lib/esm/src/components/MessageBubble.mjs +3 -3
  50. package/lib/esm/src/components/MessageBubble.mjs.map +1 -1
  51. package/lib/esm/src/components/MessageSuggestions.d.ts.map +1 -1
  52. package/lib/esm/src/components/MessageSuggestions.mjs +8 -1
  53. package/lib/esm/src/components/MessageSuggestions.mjs.map +1 -1
  54. package/lib/esm/src/hooks/useFetchInitialMessage.d.ts.map +1 -1
  55. package/lib/esm/src/hooks/useFetchInitialMessage.mjs +9 -1
  56. package/lib/esm/src/hooks/useFetchInitialMessage.mjs.map +1 -1
  57. package/lib/esm/src/hooks/useSendMessageWithRetries.d.ts.map +1 -1
  58. package/lib/esm/src/hooks/useSendMessageWithRetries.mjs +3 -1
  59. package/lib/esm/src/hooks/useSendMessageWithRetries.mjs.map +1 -1
  60. package/package.json +3 -3
  61. package/src/components/ChatInput.tsx +16 -7
  62. package/src/components/ChatPanel.tsx +35 -16
  63. package/src/components/ChatPopUp.tsx +27 -8
  64. package/src/components/Markdown.tsx +5 -1
  65. package/src/components/MessageBubble.tsx +5 -1
  66. package/src/components/MessageSuggestions.tsx +12 -5
  67. package/src/hooks/useFetchInitialMessage.ts +11 -3
  68. package/src/hooks/useSendMessageWithRetries.ts +34 -27
@@ -43,7 +43,7 @@ const builtInCssClasses: MessageBubbleCssClasses =
43
43
  text__bot: "text-slate-900",
44
44
  text__user: "text-white break-words",
45
45
  timestamp:
46
- "w-fit my-0.5 ml-4 @lg:ml-0 text-slate-400 text-[10px] @[480px]:text-[13px] opacity-0 peer-hover:opacity-100 duration-200 whitespace-pre-wrap",
46
+ "w-fit ml-4 @lg:ml-0 text-slate-400 text-[10px] @[480px]:text-[13px] opacity-0 peer-hover:opacity-100 duration-200 whitespace-pre-wrap",
47
47
  timestamp__bot: "",
48
48
  timestamp__user: "ml-auto",
49
49
  feedbackButtonsCssClasses: {},
@@ -75,6 +75,8 @@ export interface MessageBubbleProps {
75
75
  formatTimestamp?: (timestamp: string) => string;
76
76
  /** CSS classes for customizing the component styling. */
77
77
  customCssClasses?: MessageBubbleCssClasses;
78
+ /** Link target open behavior on click. */
79
+ linkTarget?: string;
78
80
  /** A callback which is called when user clicks a link. */
79
81
  onLinkClick?: (href?: string) => void;
80
82
  }
@@ -92,6 +94,7 @@ export function MessageBubble({
92
94
  showTimestamp = true,
93
95
  customCssClasses,
94
96
  formatTimestamp = defaultFormatTimestamp,
97
+ linkTarget,
95
98
  onLinkClick,
96
99
  }: MessageBubbleProps) {
97
100
  const cssClasses = useComposedCssClasses(builtInCssClasses, customCssClasses);
@@ -141,6 +144,7 @@ export function MessageBubble({
141
144
  content={message.text}
142
145
  responseId={message.responseId}
143
146
  customCssClasses={markdownCssClasses}
147
+ linkTarget={linkTarget}
144
148
  onLinkClick={onLinkClick}
145
149
  />
146
150
  </div>
@@ -36,7 +36,7 @@ export interface MessageSuggestionsProps {
36
36
  /** {@inheritdoc ChatInputProps.onSend} */
37
37
  onSend?: (message: string) => void;
38
38
  /** {@inheritdoc ChatInputProps.onRetry} */
39
- onRetry?: (e: unknown) => void
39
+ onRetry?: (e: unknown) => void;
40
40
  }
41
41
 
42
42
  const defaultClassnames: MessageSuggestionCssClasses = withStylelessCssClasses(
@@ -65,7 +65,7 @@ export const MessageSuggestions: React.FC<MessageSuggestionsProps> = ({
65
65
  const actions = useChatActions();
66
66
  const notes = useChatState((state) => state.conversation.notes);
67
67
  const defaultHandleApiError = useDefaultHandleApiError();
68
- const sendMessageWithRetries = useSendMessageWithRetries(stream, 1, onRetry)
68
+ const sendMessageWithRetries = useSendMessageWithRetries(stream, 1, onRetry);
69
69
  const sendMsg = useCallback(
70
70
  (msg: string) => {
71
71
  const newNotes = {
@@ -76,10 +76,17 @@ export const MessageSuggestions: React.FC<MessageSuggestionsProps> = ({
76
76
  sendMessageWithRetries(msg)
77
77
  .catch(handleError ?? defaultHandleApiError)
78
78
  .finally(() => {
79
- onSend?.(msg)
80
- })
79
+ onSend?.(msg);
80
+ });
81
81
  },
82
- [notes, actions, sendMessageWithRetries, handleError, defaultHandleApiError, onSend]
82
+ [
83
+ notes,
84
+ actions,
85
+ sendMessageWithRetries,
86
+ handleError,
87
+ defaultHandleApiError,
88
+ onSend,
89
+ ]
83
90
  );
84
91
 
85
92
  const classes = useComposedCssClasses(defaultClassnames, customCssClasses);
@@ -16,7 +16,7 @@ import { useDefaultHandleApiError } from "../hooks/useDefaultHandleApiError";
16
16
  export function useFetchInitialMessage(
17
17
  handleError?: (e: unknown) => void,
18
18
  stream = false,
19
- customCondition = true,
19
+ customCondition = true
20
20
  ) {
21
21
  const chat = useChatActions();
22
22
  const defaultHandleApiError = useDefaultHandleApiError();
@@ -24,12 +24,20 @@ export function useFetchInitialMessage(
24
24
  const canSendMessage = useChatState(
25
25
  (state) => state.conversation.canSendMessage
26
26
  );
27
-
27
+
28
28
  useEffect(() => {
29
29
  if (messages.length !== 0 || !canSendMessage || !customCondition) {
30
30
  return;
31
31
  }
32
32
  const res = stream ? chat.streamNextMessage() : chat.getNextMessage();
33
33
  res.catch((e) => (handleError ? handleError(e) : defaultHandleApiError(e)));
34
- }, [chat, stream, handleError, defaultHandleApiError, canSendMessage, customCondition, messages.length]);
34
+ }, [
35
+ chat,
36
+ stream,
37
+ handleError,
38
+ defaultHandleApiError,
39
+ canSendMessage,
40
+ customCondition,
41
+ messages.length,
42
+ ]);
35
43
  }
@@ -4,46 +4,53 @@ import { useCallback } from "react";
4
4
  /**
5
5
  * Returns a function that sends a message to the chat API with retries
6
6
  * if the API returns a 5xx status code.
7
- *
7
+ *
8
8
  * @remarks
9
9
  * The function will throw the error if the maximum number of retries is reached
10
10
  * or if the error is not a 5xx status code.
11
- *
11
+ *
12
12
  * @internal
13
- *
13
+ *
14
14
  * @param stream - If true, use streaming API
15
15
  * @param maxRetries - Maximum number of retries
16
16
  * @param onRetry - Callback to handle error on each retry
17
- *
17
+ *
18
18
  */
19
19
  export function useSendMessageWithRetries(
20
20
  stream = false,
21
21
  maxRetries = 0,
22
22
  onRetry?: (e: unknown) => void
23
23
  ): (input: string) => Promise<void> {
24
- const chat = useChatActions()
25
- return useCallback(async (input: string) => {
26
- let err: unknown;
27
- let text = input;
28
- for (let numRetries = 0; numRetries <= maxRetries; numRetries++) {
29
- if (numRetries > 0 && !!err) {
30
- if (err instanceof ApiError && !!err.statusCode && err.statusCode >= 500) {
31
- onRetry?.(err)
32
- // avoid re-adding user message to conversation history on retry
33
- text = "";
34
- } else {
35
- throw err;
24
+ const chat = useChatActions();
25
+ return useCallback(
26
+ async (input: string) => {
27
+ let err: unknown;
28
+ let text = input;
29
+ for (let numRetries = 0; numRetries <= maxRetries; numRetries++) {
30
+ if (numRetries > 0 && !!err) {
31
+ if (
32
+ err instanceof ApiError &&
33
+ !!err.statusCode &&
34
+ err.statusCode >= 500
35
+ ) {
36
+ onRetry?.(err);
37
+ // avoid re-adding user message to conversation history on retry
38
+ text = "";
39
+ } else {
40
+ throw err;
41
+ }
42
+ }
43
+ try {
44
+ await (stream
45
+ ? chat.streamNextMessage(text)
46
+ : chat.getNextMessage(text));
47
+ return;
48
+ } catch (e) {
49
+ err = e;
36
50
  }
37
51
  }
38
- try {
39
- await (stream
40
- ? chat.streamNextMessage(text)
41
- : chat.getNextMessage(text));
42
- return;
43
- } catch (e) {
44
- err = e;
45
- }
46
- }
47
- throw err
48
- }, [chat, maxRetries, onRetry, stream])
52
+ throw err;
53
+ },
54
+ [chat, maxRetries, onRetry, stream]
55
+ );
49
56
  }