@yext/chat-ui-react 0.8.1-alpha.53.2 → 0.8.2
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/lib/bundle.css +1 -1
- package/lib/commonjs/package.json.js +1 -1
- package/lib/commonjs/src/components/ChatInput.js +1 -1
- package/lib/commonjs/src/components/ChatPanel.d.ts +10 -5
- package/lib/commonjs/src/components/ChatPanel.d.ts.map +1 -1
- package/lib/commonjs/src/components/ChatPanel.js +41 -8
- package/lib/commonjs/src/components/ChatPanel.js.map +1 -1
- package/lib/commonjs/src/components/ChatPopUp.d.ts +1 -1
- package/lib/commonjs/src/components/ChatPopUp.d.ts.map +1 -1
- package/lib/commonjs/src/components/ChatPopUp.js +2 -2
- package/lib/commonjs/src/components/ChatPopUp.js.map +1 -1
- package/lib/commonjs/src/components/FeedbackButtons.d.ts +4 -3
- package/lib/commonjs/src/components/FeedbackButtons.d.ts.map +1 -1
- package/lib/commonjs/src/components/FeedbackButtons.js +1 -2
- package/lib/commonjs/src/components/FeedbackButtons.js.map +1 -1
- package/lib/commonjs/src/components/Markdown.d.ts +3 -7
- package/lib/commonjs/src/components/Markdown.d.ts.map +1 -1
- package/lib/commonjs/src/components/Markdown.js +1 -1
- package/lib/commonjs/src/components/Markdown.js.map +1 -1
- package/lib/commonjs/src/components/MessageSuggestions.d.ts +27 -0
- package/lib/commonjs/src/components/MessageSuggestions.d.ts.map +1 -0
- package/lib/commonjs/src/components/MessageSuggestions.js +35 -0
- package/lib/commonjs/src/components/MessageSuggestions.js.map +1 -0
- package/lib/commonjs/src/components/index.d.ts +2 -4
- package/lib/commonjs/src/components/index.d.ts.map +1 -1
- package/lib/commonjs/src/hooks/index.d.ts +0 -3
- package/lib/commonjs/src/hooks/index.d.ts.map +1 -1
- package/lib/commonjs/src/hooks/useDefaultHandleApiError.d.ts +1 -1
- package/lib/commonjs/src/hooks/useDefaultHandleApiError.js +1 -1
- package/lib/commonjs/src/hooks/useDefaultHandleApiError.js.map +1 -1
- package/lib/commonjs/src/hooks/useFetchInitialMessage.d.ts +1 -1
- package/lib/commonjs/src/hooks/useFetchInitialMessage.js +1 -1
- package/lib/commonjs/src/hooks/useFetchInitialMessage.js.map +1 -1
- package/lib/commonjs/src/hooks/useReportAnalyticsEvent.d.ts +0 -2
- package/lib/commonjs/src/hooks/useReportAnalyticsEvent.d.ts.map +1 -1
- package/lib/commonjs/src/hooks/useReportAnalyticsEvent.js +0 -2
- package/lib/commonjs/src/hooks/useReportAnalyticsEvent.js.map +1 -1
- package/lib/commonjs/src/index.js +0 -10
- package/lib/commonjs/src/index.js.map +1 -1
- package/lib/esm/index.d.ts +30 -87
- package/lib/esm/package.json.mjs +1 -1
- package/lib/esm/src/components/ChatInput.mjs +1 -1
- package/lib/esm/src/components/ChatPanel.d.ts +10 -5
- package/lib/esm/src/components/ChatPanel.d.ts.map +1 -1
- package/lib/esm/src/components/ChatPanel.mjs +42 -9
- package/lib/esm/src/components/ChatPanel.mjs.map +1 -1
- package/lib/esm/src/components/ChatPopUp.d.ts +1 -1
- package/lib/esm/src/components/ChatPopUp.d.ts.map +1 -1
- package/lib/esm/src/components/ChatPopUp.mjs +2 -2
- package/lib/esm/src/components/ChatPopUp.mjs.map +1 -1
- package/lib/esm/src/components/FeedbackButtons.d.ts +4 -3
- package/lib/esm/src/components/FeedbackButtons.d.ts.map +1 -1
- package/lib/esm/src/components/FeedbackButtons.mjs +1 -2
- package/lib/esm/src/components/FeedbackButtons.mjs.map +1 -1
- package/lib/esm/src/components/Markdown.d.ts +3 -7
- package/lib/esm/src/components/Markdown.d.ts.map +1 -1
- package/lib/esm/src/components/Markdown.mjs +1 -1
- package/lib/esm/src/components/Markdown.mjs.map +1 -1
- package/lib/esm/src/components/MessageSuggestions.d.ts +27 -0
- package/lib/esm/src/components/MessageSuggestions.d.ts.map +1 -0
- package/lib/esm/src/components/MessageSuggestions.mjs +29 -0
- package/lib/esm/src/components/MessageSuggestions.mjs.map +1 -0
- package/lib/esm/src/components/index.d.ts +2 -4
- package/lib/esm/src/components/index.d.ts.map +1 -1
- package/lib/esm/src/hooks/index.d.ts +0 -3
- package/lib/esm/src/hooks/index.d.ts.map +1 -1
- package/lib/esm/src/hooks/useDefaultHandleApiError.d.ts +1 -1
- package/lib/esm/src/hooks/useDefaultHandleApiError.mjs +1 -1
- package/lib/esm/src/hooks/useDefaultHandleApiError.mjs.map +1 -1
- package/lib/esm/src/hooks/useFetchInitialMessage.d.ts +1 -1
- package/lib/esm/src/hooks/useFetchInitialMessage.mjs +1 -1
- package/lib/esm/src/hooks/useFetchInitialMessage.mjs.map +1 -1
- package/lib/esm/src/hooks/useReportAnalyticsEvent.d.ts +0 -2
- package/lib/esm/src/hooks/useReportAnalyticsEvent.d.ts.map +1 -1
- package/lib/esm/src/hooks/useReportAnalyticsEvent.mjs +0 -2
- package/lib/esm/src/hooks/useReportAnalyticsEvent.mjs.map +1 -1
- package/lib/esm/src/index.mjs +0 -5
- package/lib/esm/src/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/ChatPanel.tsx +72 -16
- package/src/components/ChatPopUp.tsx +1 -1
- package/src/components/FeedbackButtons.tsx +3 -3
- package/src/components/Markdown.tsx +2 -7
- package/src/components/MessageSuggestions.tsx +71 -0
- package/src/components/index.ts +6 -6
- package/src/hooks/index.ts +0 -3
- package/src/hooks/useDefaultHandleApiError.ts +1 -1
- package/src/hooks/useFetchInitialMessage.ts +1 -1
- package/src/hooks/useReportAnalyticsEvent.ts +0 -2
- package/lib/commonjs/src/hooks/useScrollToLastMessage.d.ts +0 -15
- package/lib/commonjs/src/hooks/useScrollToLastMessage.d.ts.map +0 -1
- package/lib/commonjs/src/hooks/useScrollToLastMessage.js +0 -45
- package/lib/commonjs/src/hooks/useScrollToLastMessage.js.map +0 -1
- package/lib/esm/src/hooks/useScrollToLastMessage.d.ts +0 -15
- package/lib/esm/src/hooks/useScrollToLastMessage.d.ts.map +0 -1
- package/lib/esm/src/hooks/useScrollToLastMessage.mjs +0 -43
- package/lib/esm/src/hooks/useScrollToLastMessage.mjs.map +0 -1
- package/src/hooks/useScrollToLastMessage.ts +0 -47
|
@@ -39,9 +39,9 @@ const builtInCssClasses: FeedbackButtonsCssClasses =
|
|
|
39
39
|
/**
|
|
40
40
|
* The props for the FeedbackButtons component.
|
|
41
41
|
*
|
|
42
|
-
* @
|
|
42
|
+
* @internal
|
|
43
43
|
*/
|
|
44
|
-
|
|
44
|
+
interface FeedbackButtonsProps {
|
|
45
45
|
/** The response ID correlates to the current message to give feedback on. */
|
|
46
46
|
responseId?: string;
|
|
47
47
|
/** CSS classes for customizing the component styling. */
|
|
@@ -52,7 +52,7 @@ export interface FeedbackButtonsProps {
|
|
|
52
52
|
* Displays feedback buttons (e.g. thumbs up and thumbs down) that will
|
|
53
53
|
* report analytic events on click.
|
|
54
54
|
*
|
|
55
|
-
* @
|
|
55
|
+
* @internal
|
|
56
56
|
*/
|
|
57
57
|
export function FeedbackButtons({
|
|
58
58
|
customCssClasses,
|
|
@@ -19,12 +19,7 @@ const unifiedPlugins: { remark?: PluggableList; rehype: PluggableList } = {
|
|
|
19
19
|
],
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
* The props for the {@link Markdown} component.
|
|
24
|
-
*
|
|
25
|
-
* @public
|
|
26
|
-
*/
|
|
27
|
-
export interface MarkdownProps {
|
|
22
|
+
interface MarkdownProps {
|
|
28
23
|
/** Stringified markdown. */
|
|
29
24
|
content: string;
|
|
30
25
|
/** The response ID correlates to the current message. */
|
|
@@ -40,7 +35,7 @@ export interface MarkdownProps {
|
|
|
40
35
|
* @remarks
|
|
41
36
|
* A link click will send a CHAT_LINK_CLICK analytics event
|
|
42
37
|
*
|
|
43
|
-
* @
|
|
38
|
+
* @internal
|
|
44
39
|
*/
|
|
45
40
|
export function Markdown({ content, responseId, className }: MarkdownProps) {
|
|
46
41
|
const reportAnalyticsEvent = useReportAnalyticsEvent();
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import React, { useCallback } from "react";
|
|
2
|
+
import { useChatActions } from "@yext/chat-headless-react";
|
|
3
|
+
import { useDefaultHandleApiError } from "../hooks/useDefaultHandleApiError";
|
|
4
|
+
import { withStylelessCssClasses } from "../utils/withStylelessCssClasses";
|
|
5
|
+
import { useComposedCssClasses } from "../hooks";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The CSS class interface for the MessageSuggestion component.
|
|
9
|
+
*
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
export interface MessageSuggestionCssClasses {
|
|
13
|
+
container?: string;
|
|
14
|
+
suggestion?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The props for the MessageSuggestions component.
|
|
19
|
+
*
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export interface MessageSuggestionsProps {
|
|
23
|
+
suggestions: string[];
|
|
24
|
+
customCssClasses?: MessageSuggestionCssClasses;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const defaultClassnames: MessageSuggestionCssClasses = withStylelessCssClasses(
|
|
28
|
+
"Suggestions",
|
|
29
|
+
{
|
|
30
|
+
container: "flex gap-2 mt-4 w-full overflow-x-auto flex-wrap",
|
|
31
|
+
suggestion:
|
|
32
|
+
"hover:cursor-pointer px-2 py-1 bg-white hover:bg-slate-300 rounded-full text-sm text-blue-700 border border-blue-700 hover:underline",
|
|
33
|
+
}
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A component that displays a list of suggested messages
|
|
38
|
+
* to the user, which they can click to send the message to the bot.
|
|
39
|
+
*
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
export const MessageSuggestions: React.FC<MessageSuggestionsProps> = ({
|
|
43
|
+
suggestions,
|
|
44
|
+
customCssClasses,
|
|
45
|
+
}) => {
|
|
46
|
+
const actions = useChatActions();
|
|
47
|
+
const defaultHandleApiError = useDefaultHandleApiError();
|
|
48
|
+
const sendMsg = useCallback(
|
|
49
|
+
(msg: string) => {
|
|
50
|
+
const res = actions.getNextMessage(msg);
|
|
51
|
+
res.catch(defaultHandleApiError);
|
|
52
|
+
},
|
|
53
|
+
[defaultHandleApiError, actions]
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const classes = useComposedCssClasses(defaultClassnames, customCssClasses);
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<div className={classes.container}>
|
|
60
|
+
{suggestions.map((suggestion, index) => (
|
|
61
|
+
<button
|
|
62
|
+
key={index}
|
|
63
|
+
className={classes.suggestion}
|
|
64
|
+
onClick={() => sendMsg(suggestion)}
|
|
65
|
+
>
|
|
66
|
+
{suggestion}
|
|
67
|
+
</button>
|
|
68
|
+
))}
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
};
|
package/src/components/index.ts
CHANGED
|
@@ -16,10 +16,10 @@ export type { ChatPanelCssClasses, ChatPanelProps } from "./ChatPanel";
|
|
|
16
16
|
export { ChatPopUp } from "./ChatPopUp";
|
|
17
17
|
export type { ChatPopUpCssClasses, ChatPopUpProps } from "./ChatPopUp";
|
|
18
18
|
|
|
19
|
-
export {
|
|
20
|
-
export type { FeedbackButtonsCssClasses, FeedbackButtonsProps } from "./FeedbackButtons";
|
|
21
|
-
|
|
22
|
-
export { Markdown } from "./Markdown";
|
|
23
|
-
export { MarkdownProps } from "./Markdown";
|
|
24
|
-
|
|
19
|
+
export type { FeedbackButtonsCssClasses } from "./FeedbackButtons";
|
|
25
20
|
export type { InitialMessagePopUpCssClasses } from "./InitialMessagePopUp";
|
|
21
|
+
|
|
22
|
+
export type {
|
|
23
|
+
MessageSuggestionCssClasses,
|
|
24
|
+
MessageSuggestionsProps,
|
|
25
|
+
} from "./MessageSuggestions";
|
package/src/hooks/index.ts
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
1
|
export { useComposedCssClasses } from "./useComposedCssClasses";
|
|
2
|
-
export { useDefaultHandleApiError } from "./useDefaultHandleApiError";
|
|
3
|
-
export { useFetchInitialMessage } from "./useFetchInitialMessage";
|
|
4
2
|
export { useReportAnalyticsEvent } from "./useReportAnalyticsEvent";
|
|
5
|
-
export { useScrollToLastMessage } from "./useScrollToLastMessage";
|
|
@@ -5,7 +5,7 @@ import { useCallback } from "react";
|
|
|
5
5
|
* Returns a default handler function for API errors. It will log the error and
|
|
6
6
|
* add a default error message to state.
|
|
7
7
|
*
|
|
8
|
-
* @
|
|
8
|
+
* @internal
|
|
9
9
|
*/
|
|
10
10
|
export function useDefaultHandleApiError() {
|
|
11
11
|
const chat = useChatActions();
|
|
@@ -6,7 +6,7 @@ import { useDefaultHandleApiError } from "../hooks/useDefaultHandleApiError";
|
|
|
6
6
|
* Sends a request to Chat API to fetch the initial message when the
|
|
7
7
|
* conversation first start or when the message history is reset.
|
|
8
8
|
*
|
|
9
|
-
* @
|
|
9
|
+
* @internal
|
|
10
10
|
*
|
|
11
11
|
* @param handleError - A function which is called when an error occurs while fetching for initial message.
|
|
12
12
|
* By default, the error is logged to the console and an error message is added to state.
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
/**
|
|
3
|
-
* Scroll the messsages container to the top of the last message whenever there's
|
|
4
|
-
* an update to the messages state.
|
|
5
|
-
*
|
|
6
|
-
* @public
|
|
7
|
-
*
|
|
8
|
-
* @returns a ref to set on the messages container element and
|
|
9
|
-
* a function to set ref on individual message element
|
|
10
|
-
*/
|
|
11
|
-
export declare function useScrollToLastMessage(): [
|
|
12
|
-
React.RefObject<HTMLDivElement>,
|
|
13
|
-
(index: number) => ((message: HTMLDivElement) => void) | undefined
|
|
14
|
-
];
|
|
15
|
-
//# sourceMappingURL=useScrollToLastMessage.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useScrollToLastMessage.d.ts","sourceRoot":"","sources":["../../../../src/hooks/useScrollToLastMessage.ts"],"names":[],"mappings":"AACA,OAAO,KAAyC,MAAM,OAAO,CAAC;AAE9D;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,IAAI;IACxC,KAAK,CAAC,SAAS,CAAC,cAAc,CAAC;IAC/B,CAAC,KAAK,EAAE,MAAM,KAAK,CAAC,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,CAAC,GAAG,SAAS;CACnE,CA+BA"}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var chatHeadlessReact = require('@yext/chat-headless-react');
|
|
4
|
-
var React = require('react');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Scroll the messsages container to the top of the last message whenever there's
|
|
8
|
-
* an update to the messages state.
|
|
9
|
-
*
|
|
10
|
-
* @public
|
|
11
|
-
*
|
|
12
|
-
* @returns a ref to set on the messages container element and
|
|
13
|
-
* a function to set ref on individual message element
|
|
14
|
-
*/
|
|
15
|
-
function useScrollToLastMessage() {
|
|
16
|
-
const messagesRef = React.useRef([]);
|
|
17
|
-
const messagesContainerRef = React.useRef(null);
|
|
18
|
-
const messages = chatHeadlessReact.useChatState((state) => state.conversation.messages);
|
|
19
|
-
// Handle scrolling when messages change
|
|
20
|
-
React.useEffect(() => {
|
|
21
|
-
let scrollTop = 0;
|
|
22
|
-
messagesRef.current = messagesRef.current.slice(0, messages.length);
|
|
23
|
-
// Sums up scroll heights of all messages except the last one
|
|
24
|
-
if (messagesRef?.current.length > 1) {
|
|
25
|
-
scrollTop = messagesRef.current
|
|
26
|
-
.slice(0, -1)
|
|
27
|
-
.map((elem, _) => elem?.scrollHeight ?? 0)
|
|
28
|
-
.reduce((total, height) => total + height);
|
|
29
|
-
}
|
|
30
|
-
// Scroll to the top of the last message
|
|
31
|
-
messagesContainerRef.current?.scroll({
|
|
32
|
-
top: scrollTop,
|
|
33
|
-
behavior: "smooth",
|
|
34
|
-
});
|
|
35
|
-
}, [messages]);
|
|
36
|
-
const setMessageRef = React.useCallback((index) => {
|
|
37
|
-
if (!messagesRef?.current)
|
|
38
|
-
return undefined;
|
|
39
|
-
return (message) => (messagesRef.current[index] = message);
|
|
40
|
-
}, []);
|
|
41
|
-
return [messagesContainerRef, setMessageRef];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
exports.useScrollToLastMessage = useScrollToLastMessage;
|
|
45
|
-
//# sourceMappingURL=useScrollToLastMessage.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useScrollToLastMessage.js","sources":["../../../../src/hooks/useScrollToLastMessage.ts"],"sourcesContent":["import { useChatState } from \"@yext/chat-headless-react\";\nimport React, { useCallback, useEffect, useRef } from \"react\";\n\n/**\n * Scroll the messsages container to the top of the last message whenever there's\n * an update to the messages state.\n * \n * @public\n * \n * @returns a ref to set on the messages container element and\n * a function to set ref on individual message element\n */\nexport function useScrollToLastMessage(): [\n React.RefObject<HTMLDivElement>,\n (index: number) => ((message: HTMLDivElement) => void) | undefined\n] {\n const messagesRef = useRef<Array<HTMLDivElement | null>>([]);\n const messagesContainerRef = useRef<HTMLDivElement>(null);\n const messages = useChatState((state) => state.conversation.messages);\n\n // Handle scrolling when messages change\n useEffect(() => {\n let scrollTop = 0;\n messagesRef.current = messagesRef.current.slice(0, messages.length);\n\n // Sums up scroll heights of all messages except the last one\n if (messagesRef?.current.length > 1) {\n scrollTop = messagesRef.current\n .slice(0, -1)\n .map((elem, _) => elem?.scrollHeight ?? 0)\n .reduce((total, height) => total + height);\n }\n\n // Scroll to the top of the last message\n messagesContainerRef.current?.scroll({\n top: scrollTop,\n behavior: \"smooth\",\n });\n }, [messages]);\n\n const setMessageRef = useCallback((index) => {\n if (!messagesRef?.current) return undefined;\n return (message: HTMLDivElement) => (messagesRef.current[index] = message);\n }, []);\n\n return [messagesContainerRef, setMessageRef]\n}"],"names":["useRef","useChatState","useEffect","useCallback"],"mappings":";;;;;AAGA;;;;;;;;AAQG;SACa,sBAAsB,GAAA;AAIpC,IAAA,MAAM,WAAW,GAAGA,YAAM,CAA+B,EAAE,CAAC,CAAC;AAC7D,IAAA,MAAM,oBAAoB,GAAGA,YAAM,CAAiB,IAAI,CAAC,CAAC;AAC1D,IAAA,MAAM,QAAQ,GAAGC,8BAAY,CAAC,CAAC,KAAK,KAAK,KAAK,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;;IAGtEC,eAAS,CAAC,MAAK;QACb,IAAI,SAAS,GAAG,CAAC,CAAC;AAClB,QAAA,WAAW,CAAC,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;;AAGpE,QAAA,IAAI,WAAW,EAAE,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE;YACnC,SAAS,GAAG,WAAW,CAAC,OAAO;AAC5B,iBAAA,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACZ,iBAAA,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,EAAE,YAAY,IAAI,CAAC,CAAC;AACzC,iBAAA,MAAM,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,KAAK,GAAG,MAAM,CAAC,CAAC;AAC9C,SAAA;;AAGD,QAAA,oBAAoB,CAAC,OAAO,EAAE,MAAM,CAAC;AACnC,YAAA,GAAG,EAAE,SAAS;AACd,YAAA,QAAQ,EAAE,QAAQ;AACnB,SAAA,CAAC,CAAC;AACL,KAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;AAEf,IAAA,MAAM,aAAa,GAAGC,iBAAW,CAAC,CAAC,KAAK,KAAI;QAC1C,IAAI,CAAC,WAAW,EAAE,OAAO;AAAE,YAAA,OAAO,SAAS,CAAC;AAC5C,QAAA,OAAO,CAAC,OAAuB,MAAM,WAAW,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC;KAC5E,EAAE,EAAE,CAAC,CAAC;AAEP,IAAA,OAAO,CAAC,oBAAoB,EAAE,aAAa,CAAC,CAAA;AAC9C;;;;"}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
/**
|
|
3
|
-
* Scroll the messsages container to the top of the last message whenever there's
|
|
4
|
-
* an update to the messages state.
|
|
5
|
-
*
|
|
6
|
-
* @public
|
|
7
|
-
*
|
|
8
|
-
* @returns a ref to set on the messages container element and
|
|
9
|
-
* a function to set ref on individual message element
|
|
10
|
-
*/
|
|
11
|
-
export declare function useScrollToLastMessage(): [
|
|
12
|
-
React.RefObject<HTMLDivElement>,
|
|
13
|
-
(index: number) => ((message: HTMLDivElement) => void) | undefined
|
|
14
|
-
];
|
|
15
|
-
//# sourceMappingURL=useScrollToLastMessage.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useScrollToLastMessage.d.ts","sourceRoot":"","sources":["../../../../src/hooks/useScrollToLastMessage.ts"],"names":[],"mappings":"AACA,OAAO,KAAyC,MAAM,OAAO,CAAC;AAE9D;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,IAAI;IACxC,KAAK,CAAC,SAAS,CAAC,cAAc,CAAC;IAC/B,CAAC,KAAK,EAAE,MAAM,KAAK,CAAC,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,CAAC,GAAG,SAAS;CACnE,CA+BA"}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { useChatState } from '@yext/chat-headless-react';
|
|
2
|
-
import { useRef, useEffect, useCallback } from 'react';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Scroll the messsages container to the top of the last message whenever there's
|
|
6
|
-
* an update to the messages state.
|
|
7
|
-
*
|
|
8
|
-
* @public
|
|
9
|
-
*
|
|
10
|
-
* @returns a ref to set on the messages container element and
|
|
11
|
-
* a function to set ref on individual message element
|
|
12
|
-
*/
|
|
13
|
-
function useScrollToLastMessage() {
|
|
14
|
-
const messagesRef = useRef([]);
|
|
15
|
-
const messagesContainerRef = useRef(null);
|
|
16
|
-
const messages = useChatState((state) => state.conversation.messages);
|
|
17
|
-
// Handle scrolling when messages change
|
|
18
|
-
useEffect(() => {
|
|
19
|
-
let scrollTop = 0;
|
|
20
|
-
messagesRef.current = messagesRef.current.slice(0, messages.length);
|
|
21
|
-
// Sums up scroll heights of all messages except the last one
|
|
22
|
-
if (messagesRef?.current.length > 1) {
|
|
23
|
-
scrollTop = messagesRef.current
|
|
24
|
-
.slice(0, -1)
|
|
25
|
-
.map((elem, _) => elem?.scrollHeight ?? 0)
|
|
26
|
-
.reduce((total, height) => total + height);
|
|
27
|
-
}
|
|
28
|
-
// Scroll to the top of the last message
|
|
29
|
-
messagesContainerRef.current?.scroll({
|
|
30
|
-
top: scrollTop,
|
|
31
|
-
behavior: "smooth",
|
|
32
|
-
});
|
|
33
|
-
}, [messages]);
|
|
34
|
-
const setMessageRef = useCallback((index) => {
|
|
35
|
-
if (!messagesRef?.current)
|
|
36
|
-
return undefined;
|
|
37
|
-
return (message) => (messagesRef.current[index] = message);
|
|
38
|
-
}, []);
|
|
39
|
-
return [messagesContainerRef, setMessageRef];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export { useScrollToLastMessage };
|
|
43
|
-
//# sourceMappingURL=useScrollToLastMessage.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useScrollToLastMessage.mjs","sources":["../../../../src/hooks/useScrollToLastMessage.ts"],"sourcesContent":["import { useChatState } from \"@yext/chat-headless-react\";\nimport React, { useCallback, useEffect, useRef } from \"react\";\n\n/**\n * Scroll the messsages container to the top of the last message whenever there's\n * an update to the messages state.\n * \n * @public\n * \n * @returns a ref to set on the messages container element and\n * a function to set ref on individual message element\n */\nexport function useScrollToLastMessage(): [\n React.RefObject<HTMLDivElement>,\n (index: number) => ((message: HTMLDivElement) => void) | undefined\n] {\n const messagesRef = useRef<Array<HTMLDivElement | null>>([]);\n const messagesContainerRef = useRef<HTMLDivElement>(null);\n const messages = useChatState((state) => state.conversation.messages);\n\n // Handle scrolling when messages change\n useEffect(() => {\n let scrollTop = 0;\n messagesRef.current = messagesRef.current.slice(0, messages.length);\n\n // Sums up scroll heights of all messages except the last one\n if (messagesRef?.current.length > 1) {\n scrollTop = messagesRef.current\n .slice(0, -1)\n .map((elem, _) => elem?.scrollHeight ?? 0)\n .reduce((total, height) => total + height);\n }\n\n // Scroll to the top of the last message\n messagesContainerRef.current?.scroll({\n top: scrollTop,\n behavior: \"smooth\",\n });\n }, [messages]);\n\n const setMessageRef = useCallback((index) => {\n if (!messagesRef?.current) return undefined;\n return (message: HTMLDivElement) => (messagesRef.current[index] = message);\n }, []);\n\n return [messagesContainerRef, setMessageRef]\n}"],"names":[],"mappings":";;;AAGA;;;;;;;;AAQG;SACa,sBAAsB,GAAA;AAIpC,IAAA,MAAM,WAAW,GAAG,MAAM,CAA+B,EAAE,CAAC,CAAC;AAC7D,IAAA,MAAM,oBAAoB,GAAG,MAAM,CAAiB,IAAI,CAAC,CAAC;AAC1D,IAAA,MAAM,QAAQ,GAAG,YAAY,CAAC,CAAC,KAAK,KAAK,KAAK,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;;IAGtE,SAAS,CAAC,MAAK;QACb,IAAI,SAAS,GAAG,CAAC,CAAC;AAClB,QAAA,WAAW,CAAC,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;;AAGpE,QAAA,IAAI,WAAW,EAAE,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE;YACnC,SAAS,GAAG,WAAW,CAAC,OAAO;AAC5B,iBAAA,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACZ,iBAAA,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,EAAE,YAAY,IAAI,CAAC,CAAC;AACzC,iBAAA,MAAM,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,KAAK,GAAG,MAAM,CAAC,CAAC;AAC9C,SAAA;;AAGD,QAAA,oBAAoB,CAAC,OAAO,EAAE,MAAM,CAAC;AACnC,YAAA,GAAG,EAAE,SAAS;AACd,YAAA,QAAQ,EAAE,QAAQ;AACnB,SAAA,CAAC,CAAC;AACL,KAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;AAEf,IAAA,MAAM,aAAa,GAAG,WAAW,CAAC,CAAC,KAAK,KAAI;QAC1C,IAAI,CAAC,WAAW,EAAE,OAAO;AAAE,YAAA,OAAO,SAAS,CAAC;AAC5C,QAAA,OAAO,CAAC,OAAuB,MAAM,WAAW,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC;KAC5E,EAAE,EAAE,CAAC,CAAC;AAEP,IAAA,OAAO,CAAC,oBAAoB,EAAE,aAAa,CAAC,CAAA;AAC9C;;;;"}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { useChatState } from "@yext/chat-headless-react";
|
|
2
|
-
import React, { useCallback, useEffect, useRef } from "react";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Scroll the messsages container to the top of the last message whenever there's
|
|
6
|
-
* an update to the messages state.
|
|
7
|
-
*
|
|
8
|
-
* @public
|
|
9
|
-
*
|
|
10
|
-
* @returns a ref to set on the messages container element and
|
|
11
|
-
* a function to set ref on individual message element
|
|
12
|
-
*/
|
|
13
|
-
export function useScrollToLastMessage(): [
|
|
14
|
-
React.RefObject<HTMLDivElement>,
|
|
15
|
-
(index: number) => ((message: HTMLDivElement) => void) | undefined
|
|
16
|
-
] {
|
|
17
|
-
const messagesRef = useRef<Array<HTMLDivElement | null>>([]);
|
|
18
|
-
const messagesContainerRef = useRef<HTMLDivElement>(null);
|
|
19
|
-
const messages = useChatState((state) => state.conversation.messages);
|
|
20
|
-
|
|
21
|
-
// Handle scrolling when messages change
|
|
22
|
-
useEffect(() => {
|
|
23
|
-
let scrollTop = 0;
|
|
24
|
-
messagesRef.current = messagesRef.current.slice(0, messages.length);
|
|
25
|
-
|
|
26
|
-
// Sums up scroll heights of all messages except the last one
|
|
27
|
-
if (messagesRef?.current.length > 1) {
|
|
28
|
-
scrollTop = messagesRef.current
|
|
29
|
-
.slice(0, -1)
|
|
30
|
-
.map((elem, _) => elem?.scrollHeight ?? 0)
|
|
31
|
-
.reduce((total, height) => total + height);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Scroll to the top of the last message
|
|
35
|
-
messagesContainerRef.current?.scroll({
|
|
36
|
-
top: scrollTop,
|
|
37
|
-
behavior: "smooth",
|
|
38
|
-
});
|
|
39
|
-
}, [messages]);
|
|
40
|
-
|
|
41
|
-
const setMessageRef = useCallback((index) => {
|
|
42
|
-
if (!messagesRef?.current) return undefined;
|
|
43
|
-
return (message: HTMLDivElement) => (messagesRef.current[index] = message);
|
|
44
|
-
}, []);
|
|
45
|
-
|
|
46
|
-
return [messagesContainerRef, setMessageRef]
|
|
47
|
-
}
|