2023-02-13 20:13:59 -05:00
|
|
|
import React, { useEffect } from 'react';
|
2023-02-07 16:22:35 -05:00
|
|
|
import { useSelector } from 'react-redux';
|
2023-02-08 09:44:10 -05:00
|
|
|
import GPTIcon from '../svg/GPTIcon';
|
2023-02-15 12:29:56 -05:00
|
|
|
import BingIcon from '../svg/BingIcon';
|
2023-02-05 19:41:24 -05:00
|
|
|
|
2023-02-13 20:13:59 -05:00
|
|
|
export default function Message({
|
|
|
|
|
sender,
|
|
|
|
|
text,
|
|
|
|
|
last = false,
|
|
|
|
|
error = false,
|
|
|
|
|
scrollToBottom
|
|
|
|
|
}) {
|
2023-02-07 16:22:35 -05:00
|
|
|
const { isSubmitting } = useSelector((state) => state.submit);
|
2023-02-15 12:29:56 -05:00
|
|
|
const blinker = isSubmitting && last && sender.toLowerCase() !== 'user';
|
2023-02-13 20:13:59 -05:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (blinker) {
|
|
|
|
|
scrollToBottom();
|
|
|
|
|
}
|
2023-02-21 19:40:28 -05:00
|
|
|
}, [isSubmitting, text, blinker, scrollToBottom]);
|
2023-02-13 20:13:59 -05:00
|
|
|
|
2023-02-06 18:25:11 -05:00
|
|
|
const props = {
|
|
|
|
|
className:
|
2023-02-08 09:15:47 -05:00
|
|
|
'w-full border-b border-black/10 dark:border-gray-900/50 text-gray-800 dark:text-gray-100 group dark:bg-gray-800'
|
2023-02-06 18:25:11 -05:00
|
|
|
};
|
|
|
|
|
|
2023-02-15 12:29:56 -05:00
|
|
|
if (sender.toLowerCase() !== 'user') {
|
2023-02-06 18:25:11 -05:00
|
|
|
props.className =
|
2023-02-08 15:26:42 -05:00
|
|
|
'w-full border-b border-black/10 dark:border-gray-900/50 text-gray-800 dark:text-gray-100 group bg-gray-100 dark:bg-[#444654]';
|
2023-02-06 18:25:11 -05:00
|
|
|
}
|
|
|
|
|
|
2023-02-15 12:29:56 -05:00
|
|
|
let icon = `${sender}:`;
|
2023-02-20 21:16:40 -05:00
|
|
|
const isGPT = sender === 'chatgpt' || sender === 'davinci' || sender === 'GPT';
|
2023-02-15 12:29:56 -05:00
|
|
|
|
|
|
|
|
if (sender.toLowerCase() !== 'user') {
|
|
|
|
|
icon = (
|
|
|
|
|
<div
|
|
|
|
|
style={ isGPT ? { backgroundColor: 'rgb(16, 163, 127)' } : {}}
|
|
|
|
|
className="relative flex h-[30px] w-[30px] items-center justify-center rounded-sm p-1 text-white"
|
|
|
|
|
>
|
|
|
|
|
{ sender === 'bingai' ? <BingIcon /> : <GPTIcon />}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-05 19:41:24 -05:00
|
|
|
return (
|
2023-02-06 18:25:11 -05:00
|
|
|
<div {...props}>
|
|
|
|
|
<div className="m-auto flex gap-4 p-4 text-base md:max-w-2xl md:gap-6 md:py-6 lg:max-w-2xl lg:px-0 xl:max-w-3xl">
|
2023-02-08 09:44:10 -05:00
|
|
|
<strong className="relative flex w-[30px] flex-col items-end">
|
2023-02-15 12:29:56 -05:00
|
|
|
{icon}
|
2023-02-08 09:44:10 -05:00
|
|
|
</strong>
|
2023-02-06 18:25:11 -05:00
|
|
|
<div className="relative flex w-[calc(100%-50px)] flex-col gap-1 whitespace-pre-wrap md:gap-3 lg:w-[calc(100%-115px)]">
|
2023-02-08 00:02:29 -05:00
|
|
|
<div className="flex flex-grow flex-col gap-3">
|
2023-02-21 19:40:28 -05:00
|
|
|
{error ? (
|
2023-02-13 20:13:59 -05:00
|
|
|
<div className="flex flex min-h-[20px] flex-row flex-col items-start gap-4 gap-2 whitespace-pre-wrap text-red-500">
|
2023-02-08 00:02:29 -05:00
|
|
|
<div className="rounded-md border border-red-500 bg-red-500/10 py-2 px-3 text-sm text-gray-600 dark:text-gray-100">
|
|
|
|
|
{text}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<span>
|
|
|
|
|
{text}
|
2023-02-13 13:32:54 -05:00
|
|
|
{blinker && <span className="result-streaming">█</span>}
|
2023-02-08 00:02:29 -05:00
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2023-02-06 18:25:11 -05:00
|
|
|
</div>
|
2023-02-05 19:41:24 -05:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|