LibreChat/client/src/components/Conversations/Conversation.jsx

182 lines
4.8 KiB
React
Raw Normal View History

import React, { useState, useRef } from 'react';
2023-02-06 21:17:46 -05:00
import RenameButton from './RenameButton';
import DeleteButton from './DeleteButton';
import { useSelector, useDispatch } from 'react-redux';
import { setConversation } from '~/store/convoSlice';
import { setSubmission, setStopStream, setCustomGpt, setModel, setCustomModel } from '~/store/submitSlice';
2023-03-11 18:39:46 -05:00
import { setMessages, setEmptyMessage } from '~/store/messageSlice';
import { setText } from '~/store/textSlice';
import manualSWR from '~/utils/fetchers';
import ConvoIcon from '../svg/ConvoIcon';
import { refreshConversation } from '../../store/convoSlice';
2023-02-07 00:05:00 -05:00
2023-02-20 21:16:40 -05:00
export default function Conversation({
id,
model,
2023-02-20 21:16:40 -05:00
parentMessageId,
conversationId,
title,
chatGptLabel = null,
promptPrefix = null,
bingData,
retainView,
2023-02-20 21:16:40 -05:00
}) {
const [renaming, setRenaming] = useState(false);
const [titleInput, setTitleInput] = useState(title);
const { modelMap } = useSelector((state) => state.models);
2023-03-11 21:42:08 -05:00
const { stopStream } = useSelector((state) => state.submit);
const inputRef = useRef(null);
2023-02-07 00:05:00 -05:00
const dispatch = useDispatch();
const { trigger } = manualSWR(`/api/messages/${id}`, 'get');
const rename = manualSWR(`/api/convos/update`, 'post');
2023-02-07 00:05:00 -05:00
const clickHandler = async () => {
if (conversationId === id) {
return;
}
2023-03-11 21:42:08 -05:00
if (!stopStream) {
dispatch(setStopStream(true));
dispatch(setSubmission({}));
}
2023-03-11 18:39:46 -05:00
dispatch(setEmptyMessage());
const convo = { title, error: false, conversationId: id, chatGptLabel, promptPrefix };
2023-02-20 21:16:40 -05:00
if (bingData) {
const {
parentMessageId,
conversationSignature,
jailbreakConversationId,
clientId,
invocationId
} = bingData;
2023-02-20 21:16:40 -05:00
dispatch(
setConversation({
...convo,
parentMessageId,
2023-03-08 19:47:23 -05:00
jailbreakConversationId,
conversationSignature,
clientId,
invocationId
2023-02-20 21:16:40 -05:00
})
);
} else {
dispatch(
setConversation({
...convo,
2023-02-20 21:16:40 -05:00
parentMessageId,
2023-03-08 19:47:23 -05:00
jailbreakConversationId: null,
2023-02-20 21:16:40 -05:00
conversationSignature: null,
clientId: null,
invocationId: null
})
);
}
const data = await trigger();
2023-03-13 22:00:13 -04:00
console.log('data', data);
2023-03-04 20:48:59 -05:00
if (chatGptLabel) {
dispatch(setModel('chatgptCustom'));
} else {
dispatch(setModel(model));
2023-03-04 20:48:59 -05:00
}
if (modelMap[model.toLowerCase()]) {
console.log('sender', model);
dispatch(setCustomModel(model.toLowerCase()));
} else {
dispatch(setCustomModel(null));
}
dispatch(setMessages(data));
2023-03-04 20:48:59 -05:00
dispatch(setCustomGpt(convo));
dispatch(setText(''));
2023-03-11 18:39:46 -05:00
dispatch(setStopStream(false));
2023-02-07 00:05:00 -05:00
};
2023-02-06 13:27:28 -05:00
const renameHandler = (e) => {
e.preventDefault();
setTitleInput(title);
setRenaming(true);
setTimeout(() => {
inputRef.current.focus();
}, 25);
};
const cancelHandler = (e) => {
e.preventDefault();
setRenaming(false);
};
const onRename = (e) => {
e.preventDefault();
setRenaming(false);
if (titleInput === title) {
return;
}
rename.trigger({ conversationId, title: titleInput })
.then(() => {
dispatch(refreshConversation())
});
};
2023-03-07 13:53:23 -05:00
const handleKeyDown = (e) => {
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'
};
if (conversationId !== id) {
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 (
<a
onClick={() => clickHandler()}
2023-02-08 11:05:54 -05:00
{...aProps}
>
<ConvoIcon />
2023-02-20 21:16:40 -05:00
<div className="relative max-h-5 flex-1 overflow-hidden text-ellipsis break-all">
{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"
value={titleInput}
onChange={(e) => setTitleInput(e.target.value)}
onBlur={onRename}
2023-03-07 13:53:23 -05:00
onKeyDown={handleKeyDown}
/>
) : (
title
)}
2023-02-06 13:27:28 -05:00
</div>
{conversationId === id ? (
2023-02-08 11:05:54 -05:00
<div className="visible absolute right-1 z-10 flex text-gray-300">
<RenameButton
conversationId={id}
renaming={renaming}
renameHandler={renameHandler}
onRename={onRename}
/>
<DeleteButton
conversationId={id}
renaming={renaming}
cancelHandler={cancelHandler}
retainView={retainView}
/>
2023-02-08 11:05:54 -05:00
</div>
) : (
<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>
);
}