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

131 lines
3.8 KiB
React
Raw Normal View History

import { useState, useRef, useEffect } from 'react';
import { useRecoilState, useSetRecoilState } from 'recoil';
import { useUpdateConversationMutation } from '~/data-provider';
2023-02-06 21:17:46 -05:00
import RenameButton from './RenameButton';
import DeleteButton from './DeleteButton';
import ConvoIcon from '../svg/ConvoIcon';
import store from '~/store';
export default function Conversation({ conversation, retainView }) {
const [currentConversation, setCurrentConversation] = useRecoilState(store.conversation);
const setSubmission = useSetRecoilState(store.submission);
const { refreshConversations } = store.useConversations();
const { switchToConversation } = store.useConversation();
const updateConvoMutation = useUpdateConversationMutation(currentConversation?.conversationId);
const [renaming, setRenaming] = useState(false);
const inputRef = useRef(null);
const { conversationId, title } = conversation;
const [titleInput, setTitleInput] = useState(title);
const clickHandler = async () => {
if (currentConversation?.conversationId === conversationId) {
return;
}
// stop existing submission
setSubmission(null);
// set document title
document.title = title;
// set conversation to the new conversation
switchToConversation(conversation);
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;
}
updateConvoMutation.mutate({ conversationId, title: titleInput });
};
useEffect(() => {
if (updateConvoMutation.isSuccess) {
refreshConversations();
if (conversationId == currentConversation?.conversationId) {
setCurrentConversation((prevState) => ({
...prevState,
title: titleInput
}));
}
}
}, [updateConvoMutation.isSuccess]);
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 (currentConversation?.conversationId !== conversationId) {
2023-02-08 11:05:54 -05:00
aProps.className =
feat: Add clear button to search bar (#328) * feat: Add clear button to search bar This commit introduces a clear button to the SearchBar component using the X icon from Lucide-React. When the user enters a query in the input field, the clear button appears allowing them to easily remove the search term. The clear button is hidden when there is no search term entered. * Refactor SearchBar component to improve user experience Changed SearchBar's input field to add padding on the left side and an absolute positioned search icon. Also, added absolute positioned X icon on the right side when there is an input value, ensuring a better user experience. * Refactor SearchBar component to show Clear Search icon dynamically This commit makes changes to the SearchBar React component to render the Clear Search X icon only when the input field has a value. A showClearIcon state using useState hook is added and updated every time the input value changes. The useEffect hook is used to handle the case when the user clears the input value. This allows better UX by providing clear intent to the user that the icon is clickable and will clear the search query. * Improve UX: Add styling to clear button & export button This commit modifies the NavLinks component to improve user experience by removing a rounded styling to the "Clear conversations" and "Export conversations" buttons. Prior to this change, the buttons had a rounded styling. * Refactor submit button styling for improved accessibility and readability. Changed submit button styling for better accessibility and readability, including adjustments to padding and hover effects. The new styles ensure that the button is easily clickable for all users, while also improving its visual appearance. * hotfix * Improve UI styling in Conversation component Changed the background color and hover effect of the conversation link in Conversation component to make it more visually appealing. The previous background color was '#2A2B32' and now it's 'gray-800'. The 'px-4' class has also been changed to 'hover:pr-4' for better readability. --------- Co-authored-by: Danny Avila <110412045+danny-avila@users.noreply.github.com>
2023-06-02 09:41:34 +05:30
'group relative flex cursor-pointer items-center gap-3 break-all rounded-md py-3 px-3 hover:bg-gray-800 hover:pr-4';
2023-02-08 11:05:54 -05:00
}
2023-02-06 13:27:28 -05:00
return (
<a onClick={() => clickHandler()} {...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>
{currentConversation?.conversationId === conversationId ? (
2023-02-08 11:05:54 -05:00
<div className="visible absolute right-1 z-10 flex text-gray-300">
<RenameButton
conversationId={conversationId}
renaming={renaming}
renameHandler={renameHandler}
onRename={onRename}
/>
<DeleteButton
conversationId={conversationId}
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 rounded-r-md 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>
);
}