Perplexica/ui/components/MessageBox.tsx

235 lines
9.2 KiB
TypeScript
Raw Normal View History

'use client';
2024-04-09 16:21:05 +05:30
/* eslint-disable @next/next/no-img-element */
import React, { MutableRefObject, useEffect, useState } from 'react';
import { Message } from './ChatWindow';
import { cn } from '@/lib/utils';
2024-05-18 13:11:15 +05:30
import {
BookCopy,
Disc3,
Volume2,
StopCircle,
Layers3,
Plus,
} from 'lucide-react';
2024-04-09 16:21:05 +05:30
import Markdown from 'markdown-to-jsx';
import Copy from './MessageActions/Copy';
import Rewrite from './MessageActions/Rewrite';
import MessageSources from './MessageSources';
import LegalSearch from './LegalSearch';
2024-04-30 14:31:32 +05:30
import SearchVideos from './SearchVideos';
import { useSpeech } from 'react-text-to-speech';
2024-04-09 16:21:05 +05:30
const MessageBox = ({
message,
messageIndex,
history,
loading,
dividerRef,
isLast,
rewrite,
2024-05-18 13:11:15 +05:30
sendMessage,
2024-04-09 16:21:05 +05:30
}: {
message: Message;
messageIndex: number;
history: Message[];
loading: boolean;
dividerRef?: MutableRefObject<HTMLDivElement | null>;
isLast: boolean;
rewrite: (messageId: string) => void;
2024-05-18 13:11:15 +05:30
sendMessage: (message: string) => void;
2024-04-09 16:21:05 +05:30
}) => {
const [parsedMessage, setParsedMessage] = useState(message.content);
const [speechMessage, setSpeechMessage] = useState(message.content);
2024-04-09 16:21:05 +05:30
useEffect(() => {
2024-05-04 10:48:42 +05:30
const regex = /\[(\d+)\]/g;
2024-04-09 16:21:05 +05:30
if (
message.role === 'assistant' &&
message?.sources &&
message.sources.length > 0
) {
return setParsedMessage(
message.content.replace(
regex,
(_, number) => {
const url = message.sources?.[number - 1]?.metadata?.url || '';
// Extraire le nom de domaine sans l'extension
const sourceName = url.replace(/^(?:https?:\/\/)?(?:www\.)?([^./]+).*$/, '$1');
return `<a href="${url}" target="_blank" class="ml-2 px-3 py-1 text-xs bg-blue-500 hover:bg-blue-600 text-white rounded-md transition-colors duration-200 no-underline inline-flex items-center">${sourceName}</a>`;
}
2024-04-09 16:21:05 +05:30
),
);
}
2024-05-04 10:48:42 +05:30
setSpeechMessage(message.content.replace(regex, ''));
2024-04-09 16:21:05 +05:30
setParsedMessage(message.content);
}, [message.content, message.sources, message.role]);
useEffect(() => {
}, [message.sources]);
const { speechStatus, start, stop } = useSpeech({ text: speechMessage });
2024-04-09 16:21:05 +05:30
return (
<div>
{message.role === 'user' && (
<div className={cn('w-full', messageIndex === 0 ? 'pt-16' : 'pt-8')}>
<h3 className="text-black dark:text-white font-medium text-3xl lg:w-9/12">
2024-04-09 16:21:05 +05:30
{message.content}
</h3>
2024-04-09 16:21:05 +05:30
</div>
)}
{message.role === 'assistant' && (
<div className="flex flex-col space-y-9 lg:space-y-0 lg:flex-row lg:justify-between lg:space-x-9">
<div
ref={dividerRef}
className="flex flex-col space-y-6 w-full lg:w-9/12"
>
{message.sources && message.sources[0]?.metadata?.illustrationImage && (
<div className="flex flex-col space-y-2 mb-6">
<div className="w-full aspect-[21/9] relative overflow-hidden rounded-xl shadow-lg">
<img
src={message.sources[0].metadata.illustrationImage}
alt="Illustration"
className="w-full h-full object-cover hover:scale-105 transition-transform duration-300"
onError={(e) => {
console.error("Erreur de chargement de l'image:", e);
(e.target as HTMLImageElement).style.display = 'none';
}}
/>
</div>
<p className="text-sm text-gray-500 dark:text-gray-400 italic mt-2">
{message.sources[0].metadata.title || 'Illustration du sujet'}
</p>
</div>
)}
2024-04-09 16:21:05 +05:30
{message.sources && message.sources.length > 0 && (
<div className="flex flex-col space-y-2">
<div className="flex flex-row items-center space-x-2">
2024-05-24 20:29:49 +08:00
<BookCopy className="text-black dark:text-white" size={20} />
<h3 className="text-black dark:text-white font-medium text-xl">
Sources
</h3>
2024-04-09 16:21:05 +05:30
</div>
<MessageSources sources={message.sources} />
</div>
)}
<div className="flex flex-col space-y-2">
<div className="flex flex-row items-center space-x-2">
<Disc3
className={cn(
2024-05-24 20:29:49 +08:00
'text-black dark:text-white',
2024-04-09 16:21:05 +05:30
isLast && loading ? 'animate-spin' : 'animate-none',
)}
size={20}
/>
2024-05-24 20:29:49 +08:00
<h3 className="text-black dark:text-white font-medium text-xl">
Question
2024-05-24 20:29:49 +08:00
</h3>
2024-04-09 16:21:05 +05:30
</div>
<Markdown
className={cn(
2024-12-05 20:19:41 +05:30
'prose prose-h1:mb-3 prose-h2:mb-2 prose-h2:mt-6 prose-h2:font-[800] prose-h3:mt-4 prose-h3:mb-1.5 prose-h3:font-[600] dark:prose-invert prose-p:leading-relaxed prose-pre:p-0 font-[400]',
'max-w-none break-words text-black dark:text-white',
)}
options={{
overrides: {
p: ({ children }) => {
return <p>{children}</p>;
},
},
}}
>
2024-04-09 16:21:05 +05:30
{parsedMessage}
</Markdown>
2024-05-04 10:48:42 +05:30
{loading && isLast ? null : (
2024-05-24 20:29:49 +08:00
<div className="flex flex-row items-center justify-between w-full text-black dark:text-white py-4 -mx-2">
2024-04-09 16:21:05 +05:30
<div className="flex flex-row items-center space-x-1">
2024-05-27 11:49:09 +08:00
{/* <button className="p-2 text-black/70 dark:text-white/70 rounded-xl hover:bg-light-secondary dark:hover:bg-dark-secondary transition duration-200 hover:text-black text-black dark:hover:text-white">
2024-04-09 16:21:05 +05:30
<Share size={18} />
2024-05-18 13:11:15 +05:30
</button> */}
2024-06-29 11:09:51 +05:30
<Rewrite rewrite={rewrite} messageId={message.messageId} />
2024-04-09 16:21:05 +05:30
</div>
<div className="flex flex-row items-center space-x-1">
<Copy initialMessage={message.content} message={message} />
<button
onClick={() => {
if (speechStatus === 'started') {
stop();
} else {
start();
}
}}
2024-05-27 11:49:09 +08:00
className="p-2 text-black/70 dark:text-white/70 rounded-xl hover:bg-light-secondary dark:hover:bg-dark-secondary transition duration-200 hover:text-black dark:hover:text-white"
>
{speechStatus === 'started' ? (
<StopCircle size={18} />
) : (
<Volume2 size={18} />
)}
2024-04-09 16:21:05 +05:30
</button>
</div>
</div>
)}
2024-05-18 13:11:15 +05:30
{isLast &&
message.suggestions &&
message.suggestions.length > 0 &&
message.role === 'assistant' &&
!loading && (
<>
2024-05-27 11:49:09 +08:00
<div className="h-px w-full bg-light-secondary dark:bg-dark-secondary" />
2024-05-24 20:29:49 +08:00
<div className="flex flex-col space-y-3 text-black dark:text-white">
2024-05-18 13:11:15 +05:30
<div className="flex flex-row items-center space-x-2 mt-4">
<Layers3 />
<h3 className="text-xl font-medium">Suggestions</h3>
2024-05-18 13:11:15 +05:30
</div>
<div className="flex flex-col space-y-3">
{message.suggestions.map((suggestion, i) => (
<div
className="flex flex-col space-y-3 text-sm"
key={i}
>
2024-05-27 11:49:09 +08:00
<div className="h-px w-full bg-light-secondary dark:bg-dark-secondary" />
2024-05-18 13:11:15 +05:30
<div
onClick={() => {
sendMessage(suggestion);
}}
className="cursor-pointer flex flex-row justify-between font-medium space-x-2 items-center"
>
<p className="transition duration-200 hover:text-[#24A0ED]">
{suggestion}
</p>
<Plus
size={20}
className="text-[#24A0ED] flex-shrink-0"
/>
2024-05-18 13:11:15 +05:30
</div>
</div>
))}
</div>
</div>
</>
)}
2024-04-09 16:21:05 +05:30
</div>
</div>
<div className="lg:sticky lg:top-20 flex flex-col items-center space-y-3 w-full lg:w-3/12 z-30 h-full pb-4">
<LegalSearch
query={history[messageIndex - 1].content}
chatHistory={history.slice(0, messageIndex - 1)}
/>
2024-04-30 14:31:32 +05:30
<SearchVideos
chatHistory={history.slice(0, messageIndex - 1)}
2024-04-30 14:31:32 +05:30
query={history[messageIndex - 1].content}
/>
2024-04-09 16:21:05 +05:30
</div>
</div>
)}
</div>
);
};
export default MessageBox;