Skip to content

Commit

Permalink
resolve conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
petia182 committed Sep 21, 2023
2 parents b2f1c13 + 38947e2 commit b84632f
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const config: StorybookConfig = {
},
docs: {
autodocs: true,
defaultName: "Documentation"
defaultName: "Documentation",
},
viteFinal: (config, _options) => {
if (!config.css) {
Expand Down
4 changes: 2 additions & 2 deletions .storybook/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const preview: Preview = {
docs: {
story: {
// https://storybook.js.org/docs/react/api/doc-block-story#autoplay
autoplay: true
}
autoplay: true,
},
},
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yext/chat-ui-react",
"version": "0.7.1",
"version": "0.7.2",
"description": "A library of React Components for powering Yext Chat integrations.",
"author": "[email protected]",
"main": "./lib/commonjs/src/index.js",
Expand Down
4 changes: 2 additions & 2 deletions sites-config/ci_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"buildCmd": "npm run build-storybook"
},
"livePreview": {
"serveCmd": "npm run storybook -- -p 8080"
"serveCmd": "npm run storybook -- -p 8080"
}
}
}
43 changes: 31 additions & 12 deletions src/components/ChatPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useRef } from "react";
import ReactMarkdown from "react-markdown";
import React, { useCallback, useEffect, useRef } from "react";
import { useChatState, useChatActions } from "@yext/chat-headless-react";
import {
MessageBubble,
Expand Down Expand Up @@ -97,29 +97,48 @@ export function ChatPanel(props: ChatPanelProps) {
res.catch((e) => (handleError ? handleError(e) : defaultHandleApiError(e)));
}, [chat, props, messages, defaultHandleApiError, canSendMessage]);

const messagesRef = useRef<HTMLDivElement>(null);
const messagesRef = useRef<Array<HTMLDivElement | null>>([]);
const messagesContainer = useRef<HTMLDivElement>(null);

// Scroll to the bottom of the chat when the messages change
// Handle scrolling when messages change
useEffect(() => {
messagesRef.current?.scroll({
top: messagesRef.current?.scrollHeight,
let scrollTop = 0;
messagesRef.current = messagesRef.current.slice(0, messages.length);

// Sums up scroll heights of all messages except the last one
if (messagesRef?.current.length > 1) {
scrollTop = messagesRef.current
.slice(0, -1)
.map((elem, _) => elem?.scrollHeight ?? 0)
.reduce((total, height) => total + height);
}

// Scroll to the top of the last message
messagesContainer.current?.scroll({
top: scrollTop,
behavior: "smooth",
});
}, [messages]);

const setMessagesRef = useCallback((index) => {
if (!messagesRef?.current) return null;
return (message) => (messagesRef.current[index] = message);
}, []);

return (
<div className="yext-chat w-full h-full">
<div className={cssClasses.container}>
{header}
<div className={cssClasses.messagesScrollContainer}>
<div ref={messagesRef} className={cssClasses.messagesContainer}>
<div ref={messagesContainer} className={cssClasses.messagesContainer}>
{messages.map((message, index) => (
<MessageBubble
{...props}
customCssClasses={cssClasses.messageBubbleCssClasses}
key={index}
message={message}
/>
<div key={index} ref={setMessagesRef(index)}>
<MessageBubble
{...props}
customCssClasses={cssClasses.messageBubbleCssClasses}
message={message}
/>
</div>
))}
{loading && <LoadingDots />}
</div>
Expand Down
2 changes: 1 addition & 1 deletion test-site/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions test-site/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ function App() {
return (
<div>
<div className="h-screen w-screen flex justify-center items-center bg-red-400">
{/* <h1 className="external-element">External Element!</h1> */}
{/* <h1 className="external-element">External Element!</h1> */}
<ChatHeadlessProvider config={config}>
<div className="h-5/6 w-1/2">
<ChatPanel
header={
<ChatHeader title="Clippy's Chatbot" showRestartButton={true} />
}
/>
</div>
</ChatHeadlessProvider>
</div>
<ChatHeadlessProvider config={config}>
<div className="h-5/6 w-1/2">
<ChatPanel
header={
<ChatHeader title="Clippy's Chatbot" showRestartButton={true} />
}
/>
</div>
<ChatPopUp title="Clippy" />
</ChatHeadlessProvider>
</div>
<ChatHeadlessProvider config={config}>
<ChatPopUp title="Clippy" />
</ChatHeadlessProvider>
</div>
);
}

Expand Down
13 changes: 8 additions & 5 deletions tests/__utils__/stories.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { ChatHeadlessProvider, HeadlessConfig } from "@yext/chat-headless-react";
import {
ChatHeadlessProvider,
HeadlessConfig,
} from "@yext/chat-headless-react";

const config: HeadlessConfig = {
botId: "DUMMY_BOT_ID",
apiKey: "DUMMY_API_KEY",
};

export function DummyChatHeadlessProvider({ children }) {
return <ChatHeadlessProvider config={config}>
{children}
</ChatHeadlessProvider>
}
return (
<ChatHeadlessProvider config={config}>{children}</ChatHeadlessProvider>
);
}
8 changes: 5 additions & 3 deletions tests/components/MessageBubble.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ const meta: Meta<typeof MessageBubble> = {
export default meta;

const Base: StoryObj<typeof meta> = {
render: (args) => <DummyChatHeadlessProvider>
<MessageBubble {...args} />
</DummyChatHeadlessProvider>,
render: (args) => (
<DummyChatHeadlessProvider>
<MessageBubble {...args} />
</DummyChatHeadlessProvider>
),
};

export const BotMessage: StoryObj<typeof meta> = {
Expand Down

0 comments on commit b84632f

Please sign in to comment.