LibreChat/src/components/Conversations/Conversation.jsx

109 lines
3.2 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 { useDispatch } from 'react-redux';
import { setConversation } from '~/store/convoSlice';
import { setMessages } from '~/store/messageSlice';
import { setText } from '~/store/textSlice';
import manualSWR from '~/utils/fetchers';
import ConvoIcon from '../svg/ConvoIcon';
2023-02-07 00:05:00 -05:00
export default function Conversation({ id, parentMessageId, conversationId, title = 'New conversation' }) {
const [renaming, setRenaming] = useState(false);
const [titleInput, setTitleInput] = useState(title);
const inputRef = useRef(null);
2023-02-07 00:05:00 -05:00
const dispatch = useDispatch();
2023-02-08 11:05:54 -05:00
const { trigger, isMutating } = manualSWR(`http://localhost:3050/messages/${id}`, 'get');
const rename = manualSWR(`http://localhost:3050/update_convo`, 'post');
2023-02-07 00:05:00 -05:00
const clickHandler = async () => {
if (conversationId === id) {
return;
}
dispatch(setConversation({ title, error: false, conversationId: id, parentMessageId }));
const data = await trigger();
dispatch(setMessages(data));
dispatch(setText(''));
2023-02-07 00:05:00 -05:00
};
2023-02-06 13:27:28 -05:00
const renameHandler = (e) => {
e.preventDefault();
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 });
};
const handleKeyPress = (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 />
<div className="relative max-h-5 flex-1 overflow-hidden text-ellipsis break-all" >
{renaming === true ? (
<input
ref={inputRef}
type="text"
className="m-0 mr-0 w-full leading-tight border border-blue-500 bg-transparent p-0 text-sm outline-none"
value={titleInput}
onChange={(e) => setTitleInput(e.target.value)}
onBlur={onRename}
onKeyPress={handleKeyPress}
/>
) : (
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}
/>
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>
);
}