2023-02-11 10:22:15 -05:00
|
|
|
import React, { useState, useRef } from 'react';
|
2023-03-28 20:36:21 +08:00
|
|
|
import { useRecoilState, useResetRecoilState, useSetRecoilState } from 'recoil';
|
|
|
|
|
|
2023-02-06 21:17:46 -05:00
|
|
|
import RenameButton from './RenameButton';
|
|
|
|
|
import DeleteButton from './DeleteButton';
|
2023-02-11 10:22:15 -05:00
|
|
|
import ConvoIcon from '../svg/ConvoIcon';
|
2023-03-28 20:36:21 +08:00
|
|
|
import manualSWR from '~/utils/fetchers';
|
|
|
|
|
|
|
|
|
|
import store from '~/store';
|
|
|
|
|
|
|
|
|
|
export default function Conversation({ conversation, retainView }) {
|
|
|
|
|
const [currentConversation, setCurrentConversation] = useRecoilState(store.conversation);
|
|
|
|
|
const setMessages = useSetRecoilState(store.messages);
|
|
|
|
|
const setSubmission = useSetRecoilState(store.submission);
|
|
|
|
|
const resetLatestMessage = useResetRecoilState(store.latestMessage);
|
|
|
|
|
|
|
|
|
|
const { refreshConversations } = store.useConversations();
|
2023-03-28 22:39:27 +08:00
|
|
|
const { switchToConversation } = store.useConversation();
|
2023-03-28 20:36:21 +08:00
|
|
|
|
2023-02-11 10:22:15 -05:00
|
|
|
const [renaming, setRenaming] = useState(false);
|
|
|
|
|
const [titleInput, setTitleInput] = useState(title);
|
|
|
|
|
const inputRef = useRef(null);
|
2023-03-28 20:36:21 +08:00
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
model,
|
|
|
|
|
parentMessageId,
|
|
|
|
|
conversationId,
|
|
|
|
|
title,
|
|
|
|
|
chatGptLabel = null,
|
|
|
|
|
promptPrefix = null,
|
|
|
|
|
jailbreakConversationId,
|
|
|
|
|
conversationSignature,
|
|
|
|
|
clientId,
|
|
|
|
|
invocationId,
|
|
|
|
|
toneStyle
|
|
|
|
|
} = conversation;
|
|
|
|
|
|
2023-03-10 21:06:13 +08:00
|
|
|
const rename = manualSWR(`/api/convos/update`, 'post');
|
2023-02-07 00:05:00 -05:00
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
const bingData = conversationSignature
|
|
|
|
|
? {
|
|
|
|
|
jailbreakConversationId: jailbreakConversationId,
|
|
|
|
|
conversationSignature: conversationSignature,
|
|
|
|
|
parentMessageId: parentMessageId || null,
|
|
|
|
|
clientId: clientId,
|
|
|
|
|
invocationId: invocationId,
|
|
|
|
|
toneStyle: toneStyle
|
|
|
|
|
}
|
|
|
|
|
: null;
|
|
|
|
|
|
2023-02-07 19:07:48 -05:00
|
|
|
const clickHandler = async () => {
|
2023-03-28 20:36:21 +08:00
|
|
|
if (currentConversation?.conversationId === conversationId) {
|
2023-02-08 08:27:23 -05:00
|
|
|
return;
|
|
|
|
|
}
|
2023-02-07 16:22:35 -05:00
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
// stop existing submission
|
|
|
|
|
setSubmission(null);
|
|
|
|
|
|
|
|
|
|
// set conversation to the new conversation
|
2023-03-28 22:39:27 +08:00
|
|
|
switchToConversation(conversation);
|
2023-03-28 20:36:21 +08:00
|
|
|
|
|
|
|
|
// if (!stopStream) {
|
|
|
|
|
// dispatch(setStopStream(true));
|
|
|
|
|
// dispatch(setSubmission({}));
|
|
|
|
|
// }
|
|
|
|
|
// dispatch(setEmptyMessage());
|
|
|
|
|
|
|
|
|
|
// const convo = { title, error: false, conversationId: id, chatGptLabel, promptPrefix };
|
|
|
|
|
|
|
|
|
|
// if (bingData) {
|
|
|
|
|
// const {
|
|
|
|
|
// parentMessageId,
|
|
|
|
|
// conversationSignature,
|
|
|
|
|
// jailbreakConversationId,
|
|
|
|
|
// clientId,
|
|
|
|
|
// invocationId,
|
|
|
|
|
// toneStyle
|
|
|
|
|
// } = bingData;
|
|
|
|
|
// dispatch(
|
|
|
|
|
// setConversation({
|
|
|
|
|
// ...convo,
|
|
|
|
|
// parentMessageId,
|
|
|
|
|
// jailbreakConversationId,
|
|
|
|
|
// conversationSignature,
|
|
|
|
|
// clientId,
|
|
|
|
|
// invocationId,
|
|
|
|
|
// toneStyle,
|
|
|
|
|
// latestMessage: null
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
// } else {
|
|
|
|
|
// dispatch(
|
|
|
|
|
// setConversation({
|
|
|
|
|
// ...convo,
|
|
|
|
|
// parentMessageId,
|
|
|
|
|
// jailbreakConversationId: null,
|
|
|
|
|
// conversationSignature: null,
|
|
|
|
|
// clientId: null,
|
|
|
|
|
// invocationId: null,
|
|
|
|
|
// toneStyle: null,
|
|
|
|
|
// latestMessage: null
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
// const data = await trigger();
|
|
|
|
|
|
|
|
|
|
// if (chatGptLabel) {
|
|
|
|
|
// dispatch(setModel('chatgptCustom'));
|
|
|
|
|
// dispatch(setCustomModel(chatGptLabel.toLowerCase()));
|
|
|
|
|
// } else {
|
|
|
|
|
// dispatch(setModel(model));
|
|
|
|
|
// dispatch(setCustomModel(null));
|
|
|
|
|
// }
|
2023-03-04 21:10:45 -05:00
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
// dispatch(setMessages(data));
|
|
|
|
|
// dispatch(setCustomGpt(convo));
|
|
|
|
|
// dispatch(setText(''));
|
|
|
|
|
// dispatch(setStopStream(false));
|
2023-02-07 00:05:00 -05:00
|
|
|
};
|
2023-02-06 13:27:28 -05:00
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
const renameHandler = e => {
|
2023-02-11 10:22:15 -05:00
|
|
|
e.preventDefault();
|
2023-03-13 14:04:47 +08:00
|
|
|
setTitleInput(title);
|
2023-02-11 10:22:15 -05:00
|
|
|
setRenaming(true);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
inputRef.current.focus();
|
|
|
|
|
}, 25);
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
const cancelHandler = e => {
|
2023-02-11 10:22:15 -05:00
|
|
|
e.preventDefault();
|
|
|
|
|
setRenaming(false);
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
const onRename = e => {
|
2023-02-11 10:22:15 -05:00
|
|
|
e.preventDefault();
|
|
|
|
|
setRenaming(false);
|
|
|
|
|
if (titleInput === title) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-03-28 20:36:21 +08:00
|
|
|
rename.trigger({ conversationId, title: titleInput }).then(() => {
|
|
|
|
|
refreshConversations();
|
|
|
|
|
if (conversationId == currentConversation?.conversationId)
|
|
|
|
|
setCurrentConversation(prevState => ({
|
|
|
|
|
...prevState,
|
|
|
|
|
title: titleInput
|
|
|
|
|
}));
|
|
|
|
|
});
|
2023-02-11 10:22:15 -05:00
|
|
|
};
|
|
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
const handleKeyDown = e => {
|
2023-02-11 10:22:15 -05:00
|
|
|
if (e.key === 'Enter') {
|
|
|
|
|
onRename(e);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-08 11:05:54 -05:00
|
|
|
const aProps = {
|
|
|
|
|
className:
|
|
|
|
|
'animate-flash group relative flex cursor-pointer items-center gap-3 break-all rounded-md bg-gray-800 py-3 px-3 pr-14 hover:bg-gray-800'
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
if (currentConversation?.conversationId !== conversationId) {
|
2023-02-08 11:05:54 -05:00
|
|
|
aProps.className =
|
|
|
|
|
'group relative flex cursor-pointer items-center gap-3 break-all rounded-md py-3 px-3 hover:bg-[#2A2B32] hover:pr-4';
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 13:27:28 -05:00
|
|
|
return (
|
2023-02-06 18:25:11 -05:00
|
|
|
<a
|
2023-02-07 16:22:35 -05:00
|
|
|
onClick={() => clickHandler()}
|
2023-02-08 11:05:54 -05:00
|
|
|
{...aProps}
|
2023-02-06 18:25:11 -05:00
|
|
|
>
|
2023-02-11 10:22:15 -05:00
|
|
|
<ConvoIcon />
|
2023-02-20 21:16:40 -05:00
|
|
|
<div className="relative max-h-5 flex-1 overflow-hidden text-ellipsis break-all">
|
2023-02-11 10:22:15 -05:00
|
|
|
{renaming === true ? (
|
|
|
|
|
<input
|
|
|
|
|
ref={inputRef}
|
|
|
|
|
type="text"
|
2023-02-20 21:16:40 -05:00
|
|
|
className="m-0 mr-0 w-full border border-blue-500 bg-transparent p-0 text-sm leading-tight outline-none"
|
2023-02-11 10:22:15 -05:00
|
|
|
value={titleInput}
|
2023-03-28 20:36:21 +08:00
|
|
|
onChange={e => setTitleInput(e.target.value)}
|
2023-02-11 10:22:15 -05:00
|
|
|
onBlur={onRename}
|
2023-03-07 13:53:23 -05:00
|
|
|
onKeyDown={handleKeyDown}
|
2023-02-11 10:22:15 -05:00
|
|
|
/>
|
|
|
|
|
) : (
|
2023-03-13 14:04:47 +08:00
|
|
|
title
|
2023-02-11 10:22:15 -05:00
|
|
|
)}
|
2023-02-06 13:27:28 -05:00
|
|
|
</div>
|
2023-03-28 20:36:21 +08:00
|
|
|
{currentConversation?.conversationId === conversationId ? (
|
2023-02-08 11:05:54 -05:00
|
|
|
<div className="visible absolute right-1 z-10 flex text-gray-300">
|
2023-02-11 10:22:15 -05:00
|
|
|
<RenameButton
|
2023-03-28 20:36:21 +08:00
|
|
|
conversationId={conversationId}
|
2023-02-11 10:22:15 -05:00
|
|
|
renaming={renaming}
|
|
|
|
|
renameHandler={renameHandler}
|
|
|
|
|
onRename={onRename}
|
|
|
|
|
/>
|
|
|
|
|
<DeleteButton
|
2023-03-28 20:36:21 +08:00
|
|
|
conversationId={conversationId}
|
2023-02-11 10:22:15 -05:00
|
|
|
renaming={renaming}
|
|
|
|
|
cancelHandler={cancelHandler}
|
2023-03-11 22:59:32 -05:00
|
|
|
retainView={retainView}
|
2023-02-11 10:22:15 -05:00
|
|
|
/>
|
2023-02-08 11:05:54 -05:00
|
|
|
</div>
|
2023-02-08 15:26:42 -05:00
|
|
|
) : (
|
|
|
|
|
<div className="absolute inset-y-0 right-0 z-10 w-8 bg-gradient-to-l from-gray-900 group-hover:from-[#2A2B32]" />
|
2023-02-08 11:05:54 -05:00
|
|
|
)}
|
2023-02-06 13:27:28 -05:00
|
|
|
</a>
|
|
|
|
|
);
|
|
|
|
|
}
|