2024-04-09 10:51:05 +00:00
|
|
|
'use client';
|
|
|
|
|
2024-05-08 01:57:11 +00:00
|
|
|
import { Fragment, useEffect, useRef, useState } from 'react';
|
2024-04-09 10:51:05 +00:00
|
|
|
import MessageInput from './MessageInput';
|
2024-11-23 09:34:19 +00:00
|
|
|
import { File, Message } from './ChatWindow';
|
2024-04-09 10:51:05 +00:00
|
|
|
import MessageBox from './MessageBox';
|
|
|
|
import MessageBoxLoading from './MessageBoxLoading';
|
|
|
|
|
|
|
|
const Chat = ({
|
|
|
|
loading,
|
|
|
|
messages,
|
|
|
|
sendMessage,
|
|
|
|
messageAppeared,
|
|
|
|
rewrite,
|
2024-11-23 09:34:19 +00:00
|
|
|
fileIds,
|
|
|
|
setFileIds,
|
|
|
|
files,
|
|
|
|
setFiles,
|
2024-04-09 10:51:05 +00:00
|
|
|
}: {
|
|
|
|
messages: Message[];
|
|
|
|
sendMessage: (message: string) => void;
|
|
|
|
loading: boolean;
|
|
|
|
messageAppeared: boolean;
|
|
|
|
rewrite: (messageId: string) => void;
|
2024-11-23 09:34:19 +00:00
|
|
|
fileIds: string[];
|
|
|
|
setFileIds: (fileIds: string[]) => void;
|
|
|
|
files: File[];
|
|
|
|
setFiles: (files: File[]) => void;
|
2024-04-09 10:51:05 +00:00
|
|
|
}) => {
|
|
|
|
const [dividerWidth, setDividerWidth] = useState(0);
|
|
|
|
const dividerRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
const messageEnd = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const updateDividerWidth = () => {
|
|
|
|
if (dividerRef.current) {
|
|
|
|
setDividerWidth(dividerRef.current.scrollWidth);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
updateDividerWidth();
|
|
|
|
|
|
|
|
window.addEventListener('resize', updateDividerWidth);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('resize', updateDividerWidth);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
messageEnd.current?.scrollIntoView({ behavior: 'smooth' });
|
|
|
|
|
|
|
|
if (messages.length === 1) {
|
|
|
|
document.title = `${messages[0].content.substring(0, 30)} - Perplexica`;
|
|
|
|
}
|
|
|
|
}, [messages]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="flex flex-col space-y-6 pt-8 pb-44 lg:pb-32 sm:mx-4 md:mx-8">
|
|
|
|
{messages.map((msg, i) => {
|
|
|
|
const isLast = i === messages.length - 1;
|
|
|
|
|
|
|
|
return (
|
2024-06-29 05:39:51 +00:00
|
|
|
<Fragment key={msg.messageId}>
|
2024-04-09 10:51:05 +00:00
|
|
|
<MessageBox
|
|
|
|
key={i}
|
|
|
|
message={msg}
|
|
|
|
messageIndex={i}
|
|
|
|
history={messages}
|
|
|
|
loading={loading}
|
|
|
|
dividerRef={isLast ? dividerRef : undefined}
|
|
|
|
isLast={isLast}
|
|
|
|
rewrite={rewrite}
|
2024-05-18 07:40:39 +00:00
|
|
|
sendMessage={sendMessage}
|
2024-04-09 10:51:05 +00:00
|
|
|
/>
|
|
|
|
{!isLast && msg.role === 'assistant' && (
|
2024-05-27 03:49:09 +00:00
|
|
|
<div className="h-px w-full bg-light-secondary dark:bg-dark-secondary" />
|
2024-04-09 10:51:05 +00:00
|
|
|
)}
|
2024-05-08 01:57:11 +00:00
|
|
|
</Fragment>
|
2024-04-09 10:51:05 +00:00
|
|
|
);
|
|
|
|
})}
|
|
|
|
{loading && !messageAppeared && <MessageBoxLoading />}
|
|
|
|
<div ref={messageEnd} className="h-0" />
|
|
|
|
{dividerWidth > 0 && (
|
|
|
|
<div
|
|
|
|
className="bottom-24 lg:bottom-10 fixed z-40"
|
|
|
|
style={{ width: dividerWidth }}
|
|
|
|
>
|
2024-11-23 09:34:19 +00:00
|
|
|
<MessageInput
|
|
|
|
loading={loading}
|
|
|
|
sendMessage={sendMessage}
|
|
|
|
fileIds={fileIds}
|
|
|
|
setFileIds={setFileIds}
|
|
|
|
files={files}
|
|
|
|
setFiles={setFiles}
|
|
|
|
/>
|
2024-04-09 10:51:05 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Chat;
|