2023-03-13 05:26:17 +08:00
|
|
|
import React, { useState, useEffect, useRef } from 'react';
|
2023-02-23 23:56:55 -05:00
|
|
|
import TextWrapper from './TextWrapper';
|
2023-03-13 05:26:17 +08:00
|
|
|
import { useSelector, useDispatch } 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-03-03 08:51:33 -05:00
|
|
|
import HoverButtons from './HoverButtons';
|
2023-03-11 18:39:46 -05:00
|
|
|
import Spinner from '../svg/Spinner';
|
2023-03-13 05:26:17 +08:00
|
|
|
import { setError } from '~/store/convoSlice';
|
|
|
|
|
import { setMessages } from '~/store/messageSlice';
|
|
|
|
|
import { setSubmitState, setSubmission } from '~/store/submitSlice';
|
|
|
|
|
import { setText } from '~/store/textSlice';
|
2023-02-05 19:41:24 -05:00
|
|
|
|
2023-02-13 20:13:59 -05:00
|
|
|
export default function Message({
|
2023-03-13 05:26:17 +08:00
|
|
|
message,
|
|
|
|
|
messages,
|
2023-02-13 20:13:59 -05:00
|
|
|
last = false,
|
2023-03-13 05:26:17 +08:00
|
|
|
scrollToBottom,
|
|
|
|
|
edit,
|
|
|
|
|
currentEditIdx,
|
|
|
|
|
enterEdit
|
2023-02-13 20:13:59 -05:00
|
|
|
}) {
|
2023-03-13 05:26:17 +08:00
|
|
|
const { isSubmitting, model, chatGptLabel, promptPrefix } = useSelector((state) => state.submit);
|
2023-02-21 21:31:36 -05:00
|
|
|
const [abortScroll, setAbort] = useState(false);
|
2023-03-13 05:26:17 +08:00
|
|
|
const { sender, text, isCreatedByUser, error } = message
|
|
|
|
|
const textEditor = useRef(null)
|
|
|
|
|
const convo = useSelector((state) => state.convo);
|
|
|
|
|
const { initial } = useSelector((state) => state.models);
|
|
|
|
|
const { error: convoError } = convo;
|
|
|
|
|
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
// const notUser = !isCreatedByUser; // sender.toLowerCase() !== 'user';
|
|
|
|
|
const blinker = isSubmitting && last && !isCreatedByUser;
|
2023-02-13 20:13:59 -05:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-02-21 21:31:36 -05:00
|
|
|
if (blinker && !abortScroll) {
|
2023-02-13 20:13:59 -05:00
|
|
|
scrollToBottom();
|
|
|
|
|
}
|
2023-02-21 21:31:36 -05:00
|
|
|
}, [isSubmitting, text, blinker, scrollToBottom, abortScroll]);
|
2023-03-11 18:39:46 -05:00
|
|
|
|
|
|
|
|
if (sender === '') {
|
|
|
|
|
return <Spinner />;
|
|
|
|
|
}
|
2023-02-21 21:31:36 -05:00
|
|
|
|
|
|
|
|
const handleWheel = () => {
|
|
|
|
|
if (blinker) {
|
|
|
|
|
setAbort(true);
|
|
|
|
|
} else {
|
|
|
|
|
setAbort(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-02-13 20:13:59 -05:00
|
|
|
|
2023-02-06 18:25:11 -05:00
|
|
|
const props = {
|
|
|
|
|
className:
|
2023-03-04 17:39:06 -05:00
|
|
|
'w-full border-b border-black/10 dark:border-gray-900/50 text-gray-800 bg-white dark:text-gray-100 group dark:bg-gray-800'
|
2023-02-06 18:25:11 -05:00
|
|
|
};
|
|
|
|
|
|
2023-03-01 21:39:57 -05:00
|
|
|
const bgColors = {
|
|
|
|
|
chatgpt: 'rgb(16, 163, 127)',
|
|
|
|
|
chatgptBrowser: 'rgb(25, 207, 207)',
|
2023-03-08 21:06:58 -05:00
|
|
|
bingai: '',
|
2023-03-11 17:36:10 -05:00
|
|
|
sydney: ''
|
2023-03-01 21:39:57 -05:00
|
|
|
};
|
|
|
|
|
|
2023-03-08 21:06:58 -05:00
|
|
|
const isBing = sender === 'bingai' || sender === 'sydney';
|
|
|
|
|
|
2023-02-15 12:29:56 -05:00
|
|
|
let icon = `${sender}:`;
|
2023-03-01 21:39:57 -05:00
|
|
|
let backgroundColor = bgColors[sender];
|
2023-02-15 12:29:56 -05:00
|
|
|
|
2023-03-13 05:26:17 +08:00
|
|
|
if (!isCreatedByUser) {
|
2023-02-23 23:56:55 -05:00
|
|
|
props.className =
|
2023-03-06 14:04:06 -05:00
|
|
|
'w-full border-b border-black/10 bg-gray-50 dark:border-gray-900/50 text-gray-800 dark:text-gray-100 group bg-gray-100 dark:bg-[#444654]';
|
2023-03-03 21:33:09 -05:00
|
|
|
}
|
|
|
|
|
|
2023-03-13 05:26:17 +08:00
|
|
|
if ((!isCreatedByUser && backgroundColor) || isBing) {
|
2023-02-15 12:29:56 -05:00
|
|
|
icon = (
|
|
|
|
|
<div
|
2023-03-11 17:36:10 -05:00
|
|
|
style={
|
|
|
|
|
isBing
|
|
|
|
|
? { background: 'radial-gradient(circle at 90% 110%, #F0F0FA, #D0E0F9)' }
|
|
|
|
|
: { backgroundColor }
|
|
|
|
|
}
|
2023-02-15 12:29:56 -05:00
|
|
|
className="relative flex h-[30px] w-[30px] items-center justify-center rounded-sm p-1 text-white"
|
|
|
|
|
>
|
2023-03-08 21:06:58 -05:00
|
|
|
{isBing ? <BingIcon /> : <GPTIcon />}
|
2023-03-06 14:04:06 -05:00
|
|
|
{error && (
|
|
|
|
|
<span className="absolute right-0 top-[20px] -mr-2 flex h-4 w-4 items-center justify-center rounded-full border border-white bg-red-500 text-[10px] text-white">
|
|
|
|
|
!
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
2023-02-15 12:29:56 -05:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 23:56:55 -05:00
|
|
|
const wrapText = (text) => <TextWrapper text={text} />;
|
2023-02-22 22:42:22 -05:00
|
|
|
|
2023-03-13 05:26:17 +08:00
|
|
|
const resubmitMessage = () => {
|
|
|
|
|
const text = textEditor.current.innerHTML
|
|
|
|
|
|
|
|
|
|
if (convoError) {
|
|
|
|
|
dispatch(setError(false));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!!isSubmitting || text.trim() === '') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const isCustomModel = model === 'chatgptCustom' || !initial[model];
|
|
|
|
|
const currentMsg = { ...message, sender: 'User', text: text.trim(), current: true, isCreatedByUser: true };
|
|
|
|
|
console.log(model)
|
|
|
|
|
const sender = model === 'chatgptCustom' ? chatGptLabel : model;
|
|
|
|
|
|
|
|
|
|
const initialResponse = { sender, text: '' };
|
|
|
|
|
|
|
|
|
|
dispatch(setSubmitState(true));
|
|
|
|
|
dispatch(setMessages([...messages.slice(0, currentEditIdx), currentMsg, initialResponse]));
|
|
|
|
|
dispatch(setText(''));
|
|
|
|
|
|
|
|
|
|
const submission = {
|
|
|
|
|
isCustomModel,
|
|
|
|
|
message: {
|
|
|
|
|
...message,
|
|
|
|
|
text: text.trim(),
|
|
|
|
|
model,
|
|
|
|
|
chatGptLabel,
|
|
|
|
|
promptPrefix,
|
|
|
|
|
},
|
|
|
|
|
messages: messages.slice(0, currentEditIdx),
|
|
|
|
|
currentMsg,
|
|
|
|
|
initialResponse,
|
|
|
|
|
sender,
|
|
|
|
|
};
|
|
|
|
|
console.log('User Input:', message);
|
|
|
|
|
// handleSubmit(submission);
|
|
|
|
|
dispatch(setSubmission(submission));
|
|
|
|
|
|
|
|
|
|
enterEdit(true);
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-05 19:41:24 -05:00
|
|
|
return (
|
2023-02-21 21:31:36 -05:00
|
|
|
<div
|
|
|
|
|
{...props}
|
|
|
|
|
onWheel={handleWheel}
|
|
|
|
|
>
|
2023-02-06 18:25:11 -05:00
|
|
|
<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-03-11 17:36:10 -05:00
|
|
|
<strong className="relative flex w-[30px] flex-col items-end text-right text-xs md:text-sm">
|
|
|
|
|
{typeof icon === 'string' && icon.match(/[^\u0000-\u007F]+/) ? (
|
|
|
|
|
<span className=" direction-rtl w-40 overflow-x-scroll">{icon}</span>
|
|
|
|
|
) : (
|
|
|
|
|
icon
|
|
|
|
|
)}
|
2023-03-03 15:52:06 -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-03-13 05:26:17 +08:00
|
|
|
<div className="flex flex min-h-[20px] flex-col flex-grow 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">
|
2023-02-23 23:56:55 -05:00
|
|
|
{text}
|
2023-02-08 00:02:29 -05:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2023-03-13 05:26:17 +08:00
|
|
|
) :
|
|
|
|
|
edit ? (
|
|
|
|
|
<div className="flex min-h-[20px] flex-col flex-grow items-start gap-4 whitespace-pre-wrap">
|
|
|
|
|
{/* <div className={`${blinker ? 'result-streaming' : ''} markdown prose dark:prose-invert light w-full break-words`}> */}
|
|
|
|
|
|
|
|
|
|
<div className="markdown prose dark:prose-invert light w-full break-words border-none"
|
|
|
|
|
contentEditable={true} ref={textEditor} suppressContentEditableWarning={true}>
|
|
|
|
|
{text}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="text-center mt-2 flex w-full justify-center">
|
|
|
|
|
<button
|
|
|
|
|
className="btn relative btn-primary mr-2"
|
|
|
|
|
disabled={isSubmitting}
|
|
|
|
|
onClick={resubmitMessage}
|
|
|
|
|
>
|
|
|
|
|
Save & Submit
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
className="btn relative btn-neutral"
|
|
|
|
|
onClick={() => enterEdit(true)}
|
|
|
|
|
>
|
|
|
|
|
Cancel
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2023-02-22 22:42:22 -05:00
|
|
|
</div>
|
2023-03-13 05:26:17 +08:00
|
|
|
) : (
|
|
|
|
|
<div className="flex min-h-[20px] flex-col flex-grow items-start gap-4 whitespace-pre-wrap">
|
|
|
|
|
{/* <div className={`${blinker ? 'result-streaming' : ''} markdown prose dark:prose-invert light w-full break-words`}> */}
|
|
|
|
|
<div className="markdown prose dark:prose-invert light w-full break-words">
|
|
|
|
|
{!isCreatedByUser ? wrapText(text) : text}
|
|
|
|
|
{blinker && <span className="result-streaming">█</span>}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2023-02-08 00:02:29 -05:00
|
|
|
</div>
|
2023-03-13 05:42:49 +08:00
|
|
|
<HoverButtons user={!error && isCreatedByUser && !edit} onClick={() => enterEdit()}/>
|
2023-02-06 18:25:11 -05:00
|
|
|
</div>
|
2023-02-05 19:41:24 -05:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|