@yext/chat-ui-react 0.1.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 (150) hide show
  1. package/LICENSE +33 -0
  2. package/README.md +44 -0
  3. package/THIRD-PARTY-NOTICES +2106 -0
  4. package/lib/bundle.css +1 -0
  5. package/lib/commonjs/components/ChatHeader.d.ts +51 -0
  6. package/lib/commonjs/components/ChatHeader.d.ts.map +1 -0
  7. package/lib/commonjs/components/ChatHeader.js +42 -0
  8. package/lib/commonjs/components/ChatHeader.js.map +1 -0
  9. package/lib/commonjs/components/ChatInput.d.ts +48 -0
  10. package/lib/commonjs/components/ChatInput.d.ts.map +1 -0
  11. package/lib/commonjs/components/ChatInput.js +50 -0
  12. package/lib/commonjs/components/ChatInput.js.map +1 -0
  13. package/lib/commonjs/components/ChatPanel.d.ts +39 -0
  14. package/lib/commonjs/components/ChatPanel.d.ts.map +1 -0
  15. package/lib/commonjs/components/ChatPanel.js +54 -0
  16. package/lib/commonjs/components/ChatPanel.js.map +1 -0
  17. package/lib/commonjs/components/ChatPopUp.d.ts +42 -0
  18. package/lib/commonjs/components/ChatPopUp.d.ts.map +1 -0
  19. package/lib/commonjs/components/ChatPopUp.js +45 -0
  20. package/lib/commonjs/components/ChatPopUp.js.map +1 -0
  21. package/lib/commonjs/components/LoadingDots.d.ts +9 -0
  22. package/lib/commonjs/components/LoadingDots.d.ts.map +1 -0
  23. package/lib/commonjs/components/LoadingDots.js +15 -0
  24. package/lib/commonjs/components/LoadingDots.js.map +1 -0
  25. package/lib/commonjs/components/Markdown.d.ts +13 -0
  26. package/lib/commonjs/components/Markdown.d.ts.map +1 -0
  27. package/lib/commonjs/components/Markdown.js +32 -0
  28. package/lib/commonjs/components/Markdown.js.map +1 -0
  29. package/lib/commonjs/components/MessageBubble.d.ts +49 -0
  30. package/lib/commonjs/components/MessageBubble.d.ts.map +1 -0
  31. package/lib/commonjs/components/MessageBubble.js +54 -0
  32. package/lib/commonjs/components/MessageBubble.js.map +1 -0
  33. package/lib/commonjs/components/index.d.ts +11 -0
  34. package/lib/commonjs/components/index.d.ts.map +1 -0
  35. package/lib/commonjs/components/index.js +14 -0
  36. package/lib/commonjs/components/index.js.map +1 -0
  37. package/lib/commonjs/hooks/index.d.ts +2 -0
  38. package/lib/commonjs/hooks/index.d.ts.map +1 -0
  39. package/lib/commonjs/hooks/index.js +6 -0
  40. package/lib/commonjs/hooks/index.js.map +1 -0
  41. package/lib/commonjs/hooks/useComposedCssClasses.d.ts +30 -0
  42. package/lib/commonjs/hooks/useComposedCssClasses.d.ts.map +1 -0
  43. package/lib/commonjs/hooks/useComposedCssClasses.js +60 -0
  44. package/lib/commonjs/hooks/useComposedCssClasses.js.map +1 -0
  45. package/lib/commonjs/icons/Arrow.d.ts +3 -0
  46. package/lib/commonjs/icons/Arrow.d.ts.map +1 -0
  47. package/lib/commonjs/icons/Arrow.js +9 -0
  48. package/lib/commonjs/icons/Arrow.js.map +1 -0
  49. package/lib/commonjs/icons/Cross.d.ts +3 -0
  50. package/lib/commonjs/icons/Cross.d.ts.map +1 -0
  51. package/lib/commonjs/icons/Cross.js +9 -0
  52. package/lib/commonjs/icons/Cross.js.map +1 -0
  53. package/lib/commonjs/icons/DualSync.d.ts +3 -0
  54. package/lib/commonjs/icons/DualSync.d.ts.map +1 -0
  55. package/lib/commonjs/icons/DualSync.js +9 -0
  56. package/lib/commonjs/icons/DualSync.js.map +1 -0
  57. package/lib/commonjs/icons/Message.d.ts +3 -0
  58. package/lib/commonjs/icons/Message.d.ts.map +1 -0
  59. package/lib/commonjs/icons/Message.js +9 -0
  60. package/lib/commonjs/icons/Message.js.map +1 -0
  61. package/lib/commonjs/index.d.ts +3 -0
  62. package/lib/commonjs/index.d.ts.map +1 -0
  63. package/lib/commonjs/index.js +19 -0
  64. package/lib/commonjs/index.js.map +1 -0
  65. package/lib/commonjs/utils/defaultHandleError.d.ts +9 -0
  66. package/lib/commonjs/utils/defaultHandleError.d.ts.map +1 -0
  67. package/lib/commonjs/utils/defaultHandleError.js +15 -0
  68. package/lib/commonjs/utils/defaultHandleError.js.map +1 -0
  69. package/lib/esm/components/ChatHeader.d.ts +51 -0
  70. package/lib/esm/components/ChatHeader.d.ts.map +1 -0
  71. package/lib/esm/components/ChatHeader.js +38 -0
  72. package/lib/esm/components/ChatHeader.js.map +1 -0
  73. package/lib/esm/components/ChatInput.d.ts +48 -0
  74. package/lib/esm/components/ChatInput.d.ts.map +1 -0
  75. package/lib/esm/components/ChatInput.js +43 -0
  76. package/lib/esm/components/ChatInput.js.map +1 -0
  77. package/lib/esm/components/ChatPanel.d.ts +39 -0
  78. package/lib/esm/components/ChatPanel.d.ts.map +1 -0
  79. package/lib/esm/components/ChatPanel.js +50 -0
  80. package/lib/esm/components/ChatPanel.js.map +1 -0
  81. package/lib/esm/components/ChatPopUp.d.ts +42 -0
  82. package/lib/esm/components/ChatPopUp.d.ts.map +1 -0
  83. package/lib/esm/components/ChatPopUp.js +41 -0
  84. package/lib/esm/components/ChatPopUp.js.map +1 -0
  85. package/lib/esm/components/LoadingDots.d.ts +9 -0
  86. package/lib/esm/components/LoadingDots.d.ts.map +1 -0
  87. package/lib/esm/components/LoadingDots.js +11 -0
  88. package/lib/esm/components/LoadingDots.js.map +1 -0
  89. package/lib/esm/components/Markdown.d.ts +13 -0
  90. package/lib/esm/components/Markdown.d.ts.map +1 -0
  91. package/lib/esm/components/Markdown.js +25 -0
  92. package/lib/esm/components/Markdown.js.map +1 -0
  93. package/lib/esm/components/MessageBubble.d.ts +49 -0
  94. package/lib/esm/components/MessageBubble.d.ts.map +1 -0
  95. package/lib/esm/components/MessageBubble.js +50 -0
  96. package/lib/esm/components/MessageBubble.js.map +1 -0
  97. package/lib/esm/components/index.d.ts +11 -0
  98. package/lib/esm/components/index.d.ts.map +1 -0
  99. package/lib/esm/components/index.js +6 -0
  100. package/lib/esm/components/index.js.map +1 -0
  101. package/lib/esm/hooks/index.d.ts +2 -0
  102. package/lib/esm/hooks/index.d.ts.map +1 -0
  103. package/lib/esm/hooks/index.js +2 -0
  104. package/lib/esm/hooks/index.js.map +1 -0
  105. package/lib/esm/hooks/useComposedCssClasses.d.ts +30 -0
  106. package/lib/esm/hooks/useComposedCssClasses.d.ts.map +1 -0
  107. package/lib/esm/hooks/useComposedCssClasses.js +56 -0
  108. package/lib/esm/hooks/useComposedCssClasses.js.map +1 -0
  109. package/lib/esm/icons/Arrow.d.ts +3 -0
  110. package/lib/esm/icons/Arrow.d.ts.map +1 -0
  111. package/lib/esm/icons/Arrow.js +5 -0
  112. package/lib/esm/icons/Arrow.js.map +1 -0
  113. package/lib/esm/icons/Cross.d.ts +3 -0
  114. package/lib/esm/icons/Cross.d.ts.map +1 -0
  115. package/lib/esm/icons/Cross.js +5 -0
  116. package/lib/esm/icons/Cross.js.map +1 -0
  117. package/lib/esm/icons/DualSync.d.ts +3 -0
  118. package/lib/esm/icons/DualSync.d.ts.map +1 -0
  119. package/lib/esm/icons/DualSync.js +5 -0
  120. package/lib/esm/icons/DualSync.js.map +1 -0
  121. package/lib/esm/icons/Message.d.ts +3 -0
  122. package/lib/esm/icons/Message.d.ts.map +1 -0
  123. package/lib/esm/icons/Message.js +5 -0
  124. package/lib/esm/icons/Message.js.map +1 -0
  125. package/lib/esm/index.d.ts +266 -0
  126. package/lib/esm/index.d.ts.map +1 -0
  127. package/lib/esm/index.js +3 -0
  128. package/lib/esm/index.js.map +1 -0
  129. package/lib/esm/tsdoc-metadata.json +11 -0
  130. package/lib/esm/utils/defaultHandleError.d.ts +9 -0
  131. package/lib/esm/utils/defaultHandleError.d.ts.map +1 -0
  132. package/lib/esm/utils/defaultHandleError.js +11 -0
  133. package/lib/esm/utils/defaultHandleError.js.map +1 -0
  134. package/package.json +95 -0
  135. package/src/components/ChatHeader.tsx +118 -0
  136. package/src/components/ChatInput.tsx +129 -0
  137. package/src/components/ChatPanel.tsx +108 -0
  138. package/src/components/ChatPopUp.tsx +112 -0
  139. package/src/components/LoadingDots.tsx +22 -0
  140. package/src/components/Markdown.tsx +36 -0
  141. package/src/components/MessageBubble.tsx +125 -0
  142. package/src/components/index.ts +17 -0
  143. package/src/hooks/index.ts +1 -0
  144. package/src/hooks/useComposedCssClasses.ts +62 -0
  145. package/src/icons/Arrow.tsx +8 -0
  146. package/src/icons/Cross.tsx +8 -0
  147. package/src/icons/DualSync.tsx +8 -0
  148. package/src/icons/Message.tsx +10 -0
  149. package/src/index.ts +2 -0
  150. package/src/utils/defaultHandleError.ts +10 -0
@@ -0,0 +1,108 @@
1
+ import { useEffect, useRef } from "react";
2
+ import { useChatState, useChatActions } from "@yext/chat-headless-react";
3
+ import {
4
+ MessageBubble,
5
+ MessageBubbleCssClasses,
6
+ MessageBubbleProps,
7
+ } from "./MessageBubble";
8
+ import { ChatInput, ChatInputCssClasses, ChatInputProps } from "./ChatInput";
9
+ import { LoadingDots } from "./LoadingDots";
10
+ import { useComposedCssClasses } from "../hooks";
11
+ import { defaultHandleApiError } from "../utils/defaultHandleError";
12
+
13
+ /**
14
+ * The CSS class interface for the {@link ChatPanel} component.
15
+ *
16
+ * @public
17
+ */
18
+ export interface ChatPanelCssClasses {
19
+ container?: string;
20
+ messagesContainer?: string;
21
+ inputContainer?: string;
22
+ inputCssClasses?: ChatInputCssClasses;
23
+ messageBubbleCssClasses?: MessageBubbleCssClasses;
24
+ }
25
+
26
+ const builtInCssClasses: ChatPanelCssClasses = {
27
+ container:
28
+ "h-full w-full flex flex-col relative rounded-3xl shadow-2xl bg-white",
29
+ messagesContainer:
30
+ "flex flex-col gap-y-1 mt-auto px-4 pb-[85px] overflow-auto",
31
+ inputContainer: "w-full absolute bottom-0 p-4 rounded-b-3xl backdrop-blur-lg",
32
+ };
33
+
34
+ /**
35
+ * The props for the {@link ChatPanel} component.
36
+ *
37
+ * @public
38
+ */
39
+ export interface ChatPanelProps
40
+ extends Omit<MessageBubbleProps, "customCssClasses" | "message">,
41
+ Omit<ChatInputProps, "customCssClasses"> {
42
+ /** A header to render at the top of the panel. */
43
+ header?: JSX.Element;
44
+ /**
45
+ * CSS classes for customizing the component styling.
46
+ */
47
+ customCssClasses?: ChatPanelCssClasses;
48
+ }
49
+
50
+ /**
51
+ * A component that renders a full panel for chat bot interactions. This includes
52
+ * the message bubbles for the conversation, input box with send button, and header
53
+ * (if provided).
54
+ *
55
+ * @public
56
+ *
57
+ * @param props - {@link ChatPanelProps}
58
+ */
59
+ export function ChatPanel(props: ChatPanelProps) {
60
+ const { header, customCssClasses } = props;
61
+ const chat = useChatActions();
62
+ const messages = useChatState((state) => state.conversation.messages);
63
+ const loading = useChatState((state) => state.conversation.isLoading);
64
+ const canSendMessage = useChatState(
65
+ (state) => state.conversation.canSendMessage
66
+ );
67
+ const cssClasses = useComposedCssClasses(builtInCssClasses, customCssClasses);
68
+
69
+ // Fetch the first message on load, if there are no existing messages or a request being processed
70
+ useEffect(() => {
71
+ if (messages.length !== 0 || !canSendMessage) {
72
+ return;
73
+ }
74
+ const { stream = true, handleError } = props;
75
+ const res = stream ? chat.streamNextMessage() : chat.getNextMessage();
76
+ res.catch((e) => (handleError ? handleError(e) : defaultHandleApiError(e)));
77
+ }, [chat, props, messages, canSendMessage]);
78
+
79
+ const messagesRef = useRef<HTMLDivElement>(null);
80
+
81
+ // Scroll to the bottom of the chat when the messages change
82
+ useEffect(() => {
83
+ messagesRef.current?.scroll({
84
+ top: messagesRef.current?.scrollHeight,
85
+ behavior: 'smooth',
86
+ });
87
+ }, [messages]);
88
+
89
+ return (
90
+ <div className={cssClasses.container}>
91
+ {header}
92
+ <div ref={messagesRef} className={cssClasses.messagesContainer}>
93
+ {messages.map((message, index) => (
94
+ <MessageBubble
95
+ {...props}
96
+ customCssClasses={cssClasses.messageBubbleCssClasses}
97
+ key={index}
98
+ message={message}
99
+ />
100
+ ))}
101
+ {loading && <LoadingDots />}
102
+ </div>
103
+ <div className={cssClasses.inputContainer}>
104
+ <ChatInput {...props} customCssClasses={cssClasses.inputCssClasses} />
105
+ </div>
106
+ </div>
107
+ );
108
+ }
@@ -0,0 +1,112 @@
1
+ import { useCallback, useState } from "react";
2
+ import { MessageIcon } from '../icons/Message'
3
+ import { ChatPanel, ChatPanelCssClasses, ChatPanelProps } from "./ChatPanel";
4
+ import { ChatHeader, ChatHeaderCssClasses, ChatHeaderProps } from "./ChatHeader";
5
+ import { twMerge } from "tailwind-merge";
6
+ import { useComposedCssClasses } from "../hooks";
7
+
8
+ /**
9
+ * The CSS class interface for the {@link ChatPopUp} component.
10
+ *
11
+ * @public
12
+ */
13
+ export interface ChatPopUpCssClasses {
14
+ container?: string;
15
+ panel?: string;
16
+ panel__display?: string;
17
+ panel__hidden?: string;
18
+ button?: string;
19
+ button__display?: string;
20
+ button__hidden?: string;
21
+ headerCssClasses?: ChatHeaderCssClasses,
22
+ panelCssClasses?: ChatPanelCssClasses,
23
+ }
24
+
25
+ const fixedPosition = "fixed bottom-6 right-4 lg:bottom-14 lg:right-10 "
26
+ const builtInCssClasses: ChatPopUpCssClasses = {
27
+ container: "transition-all",
28
+ panel: fixedPosition + "w-80 lg:w-96 h-[75vh]",
29
+ panel__display: "duration-300 translate-y-0",
30
+ panel__hidden: "duration-300 translate-y-[20%] opacity-0 invisible",
31
+ button: fixedPosition + "p-2 w-12 h-12 lg:w-16 lg:h-16 flex justify-center items-center text-white shadow-xl rounded-full bg-gradient-to-br from-blue-600 to-blue-700 hover:-translate-y-2 duration-150",
32
+ button__display: "duration-300 transform translate-y-0",
33
+ button__hidden: "duration-300 transform translate-y-[20%] opacity-0 invisible",
34
+ };
35
+
36
+ /**
37
+ * The props for the {@link ChatPopUp} component.
38
+ *
39
+ * @public
40
+ */
41
+ export interface ChatPopUpProps extends
42
+ Omit<ChatHeaderProps, "showCloseButton" | "customCssClasses">,
43
+ Omit<ChatPanelProps, "header" | "customCssClasses">
44
+ {
45
+ /** Custom icon for the popup button to open the panel. */
46
+ openPanelButtonIcon?: JSX.Element;
47
+ /**
48
+ * CSS classes for customizing the component styling.
49
+ */
50
+ customCssClasses?: ChatPopUpCssClasses;
51
+ }
52
+
53
+ /**
54
+ * A component that renders a popup button that displays and hides
55
+ * a panel for chat bot interactions.
56
+ *
57
+ * @public
58
+ *
59
+ * @param props - {@link ChatPanelProps}
60
+ */
61
+ export function ChatPopUp(props: ChatPopUpProps) {
62
+ const {
63
+ openPanelButtonIcon = <MessageIcon />,
64
+ customCssClasses,
65
+ showRestartButton = true,
66
+ onClose:customOnClose,
67
+ title
68
+ } = props
69
+ const [showChat, setShowChat] = useState(false);
70
+ const onClick = useCallback(() => {
71
+ setShowChat(!showChat);
72
+ }, [showChat]);
73
+
74
+ const onClose = useCallback(() => {
75
+ setShowChat(false);
76
+ customOnClose?.();
77
+ }, [customOnClose])
78
+
79
+ const cssClasses = useComposedCssClasses(builtInCssClasses, customCssClasses);
80
+ const panelCssClasses = twMerge(
81
+ cssClasses.panel,
82
+ showChat ? cssClasses.panel__display : cssClasses.panel__hidden
83
+ );
84
+ const buttonCssClasses = twMerge(
85
+ cssClasses.button,
86
+ showChat ? cssClasses.button__hidden : cssClasses.button__display
87
+ );
88
+
89
+ return (
90
+ <div className={cssClasses.container}>
91
+ <div className={panelCssClasses} aria-label="Chat Popup Panel">
92
+ <ChatPanel
93
+ {...props}
94
+ customCssClasses={cssClasses.panelCssClasses}
95
+ header={<ChatHeader
96
+ title={title}
97
+ showRestartButton={showRestartButton}
98
+ showCloseButton={true}
99
+ onClose={onClose}
100
+ customCssClasses={cssClasses.headerCssClasses}/>
101
+ } />
102
+ </div>
103
+ <button
104
+ aria-label="Chat Popup Button"
105
+ onClick={onClick}
106
+ className={buttonCssClasses}
107
+ >
108
+ {openPanelButtonIcon}
109
+ </button>
110
+ </div>
111
+ );
112
+ }
@@ -0,0 +1,22 @@
1
+ import { twMerge } from "tailwind-merge";
2
+
3
+ /**
4
+ * A component that signifies loading status.
5
+ *
6
+ * @internal
7
+ */
8
+ export function LoadingDots({ className }: { className?: string }) {
9
+ return (
10
+ <div
11
+ aria-label="Loading Indicator"
12
+ className={twMerge(
13
+ "flex gap-1 p-2 animate-fade-in",
14
+ className
15
+ )}
16
+ >
17
+ <div className="w-2 h-2 bg-slate-500 rounded-full animate-[bounce_1s_infinite]" />
18
+ <div className="w-2 h-2 bg-slate-500 rounded-full animate-[bounce_1s_infinite_0.3s]" />
19
+ <div className="w-2 h-2 bg-slate-500 rounded-full animate-[bounce_1s_infinite_0.6s]" />
20
+ </div>
21
+ );
22
+ }
@@ -0,0 +1,36 @@
1
+ import ReactMarkdown, { PluggableList } from "react-markdown";
2
+ import remarkGfm from "remark-gfm";
3
+ import rehypeRaw from "rehype-raw";
4
+ import rehypeSanitize from "rehype-sanitize";
5
+
6
+ // The Remark and Rehype plugins to use in conjunction with ReactMarkdown.
7
+ const unifiedPlugins: { remark?: PluggableList; rehype: PluggableList } = {
8
+ remark: [
9
+ remarkGfm //renders Github-Flavored Markdown
10
+ ],
11
+ rehype: [
12
+ rehypeRaw, //to support HTML embedded in markdown
13
+ rehypeSanitize, //to sanitize HTML content
14
+ ],
15
+ };
16
+
17
+ interface MarkdownProps {
18
+ /** Stringified markdown. */
19
+ content: string;
20
+ }
21
+
22
+ /**
23
+ * Renders Github-Flavored Markdown from the Knowledge Graph. This Markdown can include
24
+ * arbitrary HTML. Any HTML will be sanitized according to Rehype's default Schema.
25
+ *
26
+ * @public
27
+ */
28
+ export function Markdown({ content }: MarkdownProps) {
29
+ return (
30
+ <ReactMarkdown
31
+ children={content}
32
+ remarkPlugins={unifiedPlugins.remark}
33
+ rehypePlugins={unifiedPlugins.rehype}
34
+ />
35
+ );
36
+ }
@@ -0,0 +1,125 @@
1
+ import { Message, MessageSource } from "@yext/chat-headless-react";
2
+ import { useComposedCssClasses } from "../hooks";
3
+ import { twMerge } from "tailwind-merge";
4
+ import { Markdown } from "./Markdown";
5
+
6
+ /**
7
+ * The CSS class interface for the {@link MessageBubble} component.
8
+ *
9
+ * @public
10
+ */
11
+ export interface MessageBubbleCssClasses {
12
+ topContainer?: string;
13
+ subContainer?: string;
14
+ subContainer__bot?: string;
15
+ subContainer__user?: string;
16
+ message?: string;
17
+ message__bot?: string;
18
+ message__user?: string;
19
+ timestamp?: string;
20
+ timestamp__bot?: string;
21
+ timestamp__user?: string;
22
+ }
23
+
24
+ const builtInCssClasses: MessageBubbleCssClasses = {
25
+ topContainer: "w-full animate-fade-in @container",
26
+ subContainer:
27
+ "flex flex-col @lg:flex-row @lg:items-center @lg:gap-x-2 @lg:m-1",
28
+ subContainer__user: "@lg:flex-row-reverse",
29
+ message:
30
+ "peer rounded-2xl text-[13px] @[480px]:text-base p-4 w-fit max-w-[80%] prose overflow-x-auto",
31
+ message__bot: "text-slate-900 bg-gradient-to-tr from-slate-50 to-slate-100",
32
+ message__user:
33
+ "ml-auto @lg:ml-0 text-white bg-gradient-to-tr from-blue-600 to-blue-700",
34
+ timestamp:
35
+ "w-fit my-0.5 text-slate-400 text-[13px] opacity-0 peer-hover:opacity-100 duration-200 whitespace-pre-wrap",
36
+ timestamp__user: "ml-auto",
37
+ };
38
+
39
+ /**
40
+ * The props for the {@link MessageBubble} component.
41
+ *
42
+ * @public
43
+ */
44
+ export interface MessageBubbleProps {
45
+ /** The message to display. */
46
+ message: Message;
47
+ /**
48
+ * Whether to show the timestamp of the message with the message bubble.
49
+ * Defaults to true.
50
+ */
51
+ showTimestamp?: boolean;
52
+ /**
53
+ * A function which is called to format the message's timestamp given in
54
+ * ISO format (e.g. "2023-05-18T19:33:34.553Z").
55
+ * Defaults to "HH:MM:SS A" (e.g. "7:33:34 PM").
56
+ */
57
+ formatTimestamp?: (timestamp: string) => string;
58
+ /** CSS classes for customizing the component styling. */
59
+ customCssClasses?: MessageBubbleCssClasses;
60
+ }
61
+
62
+ /**
63
+ * A component that displays the provided message.
64
+ *
65
+ * @public
66
+ *
67
+ * @param props - {@link MessageBubbleProps}
68
+ */
69
+ export function MessageBubble({
70
+ message,
71
+ showTimestamp = true,
72
+ customCssClasses,
73
+ formatTimestamp = defaultFormatTimestamp,
74
+ }: MessageBubbleProps) {
75
+ const cssClasses = useComposedCssClasses(builtInCssClasses, customCssClasses);
76
+ const messageCssClasses = twMerge(
77
+ cssClasses.message,
78
+ message.source === MessageSource.USER
79
+ ? cssClasses.message__user
80
+ : cssClasses.message__bot
81
+ );
82
+ const subContainerCssClasses = twMerge(
83
+ cssClasses.subContainer,
84
+ message.source === MessageSource.USER
85
+ ? cssClasses.subContainer__user
86
+ : cssClasses.subContainer__bot
87
+ );
88
+ const timestampCssClasses = twMerge(
89
+ cssClasses.timestamp,
90
+ message.source === MessageSource.USER
91
+ ? cssClasses.timestamp__user
92
+ : cssClasses.timestamp__bot
93
+ );
94
+
95
+ return (
96
+ <div className={cssClasses.topContainer}>
97
+ <div className={subContainerCssClasses}>
98
+ <div className={messageCssClasses}>
99
+ <Markdown content={message.text} />
100
+ </div>
101
+ {/* fallback on empty space here to perserve the height for timestamp div */}
102
+ {showTimestamp && (
103
+ <div className={timestampCssClasses}>
104
+ {message.timestamp ? formatTimestamp(message.timestamp) : " "}
105
+ </div>
106
+ )}
107
+ </div>
108
+ </div>
109
+ );
110
+ }
111
+
112
+ /**
113
+ * Formats message's timestamp from "2023-05-18T19:33:34.553Z" to "7:33:34 PM"
114
+ *
115
+ * @param timestamp - the timestamp to convert from
116
+ * @returns formatted timestamp
117
+ */
118
+ function defaultFormatTimestamp(timestamp: string): string {
119
+ return new Date(timestamp).toLocaleString(undefined, {
120
+ hour: "numeric",
121
+ minute: "numeric",
122
+ second: "numeric",
123
+ hour12: true,
124
+ });
125
+ }
@@ -0,0 +1,17 @@
1
+ export { ChatInput } from "./ChatInput";
2
+ export type { ChatInputCssClasses, ChatInputProps } from "./ChatInput";
3
+
4
+ export { ChatHeader } from "./ChatHeader";
5
+ export type { ChatHeaderCssClasses, ChatHeaderProps } from "./ChatHeader";
6
+
7
+ export { MessageBubble } from "./MessageBubble";
8
+ export type {
9
+ MessageBubbleCssClasses,
10
+ MessageBubbleProps,
11
+ } from "./MessageBubble";
12
+
13
+ export { ChatPanel } from "./ChatPanel";
14
+ export type { ChatPanelCssClasses, ChatPanelProps } from "./ChatPanel";
15
+
16
+ export { ChatPopUp } from "./ChatPopUp";
17
+ export type { ChatPopUpCssClasses, ChatPopUpProps } from "./ChatPopUp";
@@ -0,0 +1 @@
1
+ export { useComposedCssClasses } from "./useComposedCssClasses";
@@ -0,0 +1,62 @@
1
+ import { useMemo } from "react";
2
+ import { twMerge } from "tailwind-merge";
3
+
4
+ /**
5
+ * useComposedCssClasses merges a component's built-in tailwind classes with custom tailwind classes.
6
+ *
7
+ * @remarks
8
+ * Tailwind classes will be merged without conflict, with custom classes having higher priority
9
+ * than built-in ones.
10
+ *
11
+ * @example
12
+ * Suppose a component has built-in classes of `{ container: 'px-4 text-slate-700' }`.
13
+ *
14
+ * Passing in the custom classes:
15
+ *
16
+ * ```ts
17
+ * { container: 'text-red-200 mb-3' }
18
+ * ```
19
+ *
20
+ * results in the merged classes of:
21
+ *
22
+ * ```ts
23
+ * { container: 'px-4 text-red-200 mb-3' }
24
+ * ```
25
+ *
26
+ * @public
27
+ *
28
+ * @param builtInClasses - The component's built-in tailwind classes
29
+ * @param customClasses - The custom tailwind classes to merge with the built-in ones
30
+ * @returns The composed CSS classes
31
+ */
32
+ export function useComposedCssClasses<
33
+ ClassInterface extends Partial<Record<keyof ClassInterface, string | object>>
34
+ >(
35
+ builtInClasses: Readonly<ClassInterface>,
36
+ customClasses?: Partial<ClassInterface>
37
+ ): ClassInterface {
38
+ return useMemo(() => {
39
+ const mergeCssClasses = (
40
+ builtInClasses: Readonly<ClassInterface>,
41
+ customClasses?: Partial<ClassInterface>
42
+ ) => {
43
+ const mergedCssClasses = { ...builtInClasses };
44
+ if (!customClasses) {
45
+ return mergedCssClasses;
46
+ }
47
+ Object.keys(customClasses).forEach((key) => {
48
+ const builtIn = builtInClasses[key];
49
+ const custom = customClasses[key];
50
+ if (!builtIn || !custom) {
51
+ mergedCssClasses[key] = custom || builtIn;
52
+ } else if (typeof builtIn === "object" && typeof custom === "object") {
53
+ mergedCssClasses[key] = mergeCssClasses(builtIn, custom);
54
+ } else if (typeof builtIn === "string" && typeof custom === "string") {
55
+ mergedCssClasses[key] = twMerge(builtIn, custom);
56
+ }
57
+ });
58
+ return mergedCssClasses;
59
+ };
60
+ return mergeCssClasses(builtInClasses, customClasses);
61
+ }, [builtInClasses, customClasses]);
62
+ }
@@ -0,0 +1,8 @@
1
+ export function ArrowIcon(): JSX.Element {
2
+ return (
3
+ <svg width="100%" height="100%" viewBox="0 0 20 20" fill='none' stroke="currentColor" strokeLinecap="square" xmlns="http://www.w3.org/2000/svg">
4
+ <path d="M10 5V15"/>
5
+ <path d="M15 8.84615L10 4L5 8.84615"/>
6
+ </svg>
7
+ );
8
+ }
@@ -0,0 +1,8 @@
1
+ export function CrossIcon(): JSX.Element {
2
+ return (
3
+ <svg width="100%" height="100%" viewBox="0 0 20 20" stroke="currentColor" xmlns="http://www.w3.org/2000/svg">
4
+ <path d="M14 14L6 6" strokeLinecap="square" strokeLinejoin="round"/>
5
+ <path d="M14 6L6 14" strokeLinecap="square" strokeLinejoin="round"/>
6
+ </svg>
7
+ );
8
+ }
@@ -0,0 +1,8 @@
1
+ export function DualSyncIcon(): JSX.Element {
2
+ return (
3
+ <svg width="100%" height="100%" viewBox="0 0 20 20" fill='currentColor' stroke="currentColor" xmlns="http://www.w3.org/2000/svg">
4
+ <path d="M14.3148 6.47397C14.9074 7.36266 15.2593 8.46858 15.2593 9.63375C15.2593 12.675 12.9259 15.1634 10.0741 15.1634C9.11111 15.1634 8.2037 14.8671 7.42593 14.3734L9.05556 13.8995L8.87037 13.1293L6 13.939L6.75926 17L7.48148 16.8025L7.03704 15.0449C7.92593 15.6176 8.96296 15.9336 10.0741 15.9336C13.3519 15.9336 16 13.1095 16 9.614C16 8.2711 15.6111 7.02693 14.9259 6L14.3148 6.47397Z"/>
5
+ <path d="M9.92593 4.68459C10.8889 4.68459 11.7963 4.95341 12.5741 5.40143L10.9444 5.81362L11.1296 6.51254L14 5.77778L13.2407 3L12.5185 3.17921L12.963 4.77419C12.0741 4.25448 11.037 3.96774 9.92593 3.96774C6.64815 3.96774 4 6.53047 4 9.70251C4 10.9211 4.40741 12.0681 5.07407 13L5.68518 12.5878C5.09259 11.7634 4.74074 10.7778 4.74074 9.70251C4.74074 6.94265 7.07407 4.68459 9.92593 4.68459Z"/>
6
+ </svg>
7
+ );
8
+ }
@@ -0,0 +1,10 @@
1
+ export function MessageIcon(): JSX.Element {
2
+ return (
3
+ <svg width="100%" height="100%" viewBox="0 0 20 20" fill='currentColor' xmlns="http://www.w3.org/2000/svg">
4
+ <path d="M3.37424 17.1166L6.31902 14.1718H16.6258V4.84663H3.37424V17.1166ZM3.86503 5.33742H16.135V13.681H6.31902H6.11043L5.96319 13.8282L3.85276 15.9386V5.33742H3.86503Z"/>
5
+ <path d="M14.4172 9.2638H6.07362V9.7546H14.4172V9.2638Z" />
6
+ <path d="M13.9264 10.7362H6.07362V11.227H13.9264V10.7362Z" />
7
+ <path d="M13.4356 7.79141H6.07362V8.28221H13.4356V7.79141Z" />
8
+ </svg>
9
+ );
10
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./components";
2
+ export * from "./hooks";
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Default handler function for API errors.
3
+ *
4
+ * @internal
5
+ *
6
+ * @param e - error returned from api request
7
+ */
8
+ export function defaultHandleApiError(e: unknown) {
9
+ console.error(e);
10
+ }