Files
chatbot-ui/components/Chatbar/components/Conversation.tsx
T
Mckay Wrigley 6500db9c1c MAJOR REFACTOR (#494)
* move index to home folder, create state and context files and barrell folder

* Sanity Check Commit:  reducer added to home.tsx manual QA all working

* WIP: promptBar

* fix missing json parse on folders and prompts

* split context and add promptbar context

* add context to nested prompt componets and componetize Folder componet

* remove log

* Create buttons folder and componetize sidebar action button

* tidy up prompt handlers

* componetized sidebar

* added back chatbar componet to left side sidebar

* monster commit: Componetized the common code between chatbar and promptbar into new componet Sidebar and added context to both bars

* add useFetch service

* added prettier import sort to keep imports ordered and easier to indentify

* added react query and useFetch to work with RQ

* added apiService, errorService and reactQuery

* add callback and tidy up error service

* refactor chat and child componets to useContext

* fix extra calls and bad calls to mel endpoint

* minor import cleanup

---------

Co-authored-by: jc.durbin <jc.durbin@ardanis.com>
2023-04-10 21:10:18 -06:00

169 lines
5.1 KiB
TypeScript

import {
IconCheck,
IconMessage,
IconPencil,
IconTrash,
IconX,
} from '@tabler/icons-react';
import {
DragEvent,
KeyboardEvent,
MouseEventHandler,
useContext,
useEffect,
useState,
} from 'react';
import { Conversation } from '@/types/chat';
import HomeContext from '@/pages/api/home/home.context';
import SidebarActionButton from '@/components/Buttons/SidebarActionButton';
import ChatbarContext from '@/components/Chatbar/Chatbar.context';
interface Props {
conversation: Conversation;
}
export const ConversationComponent = ({ conversation }: Props) => {
const {
state: { selectedConversation, messageIsStreaming },
handleSelectConversation,
handleUpdateConversation,
} = useContext(HomeContext);
const { handleDeleteConversation } = useContext(ChatbarContext);
const [isDeleting, setIsDeleting] = useState(false);
const [isRenaming, setIsRenaming] = useState(false);
const [renameValue, setRenameValue] = useState('');
const handleEnterDown = (e: KeyboardEvent<HTMLDivElement>) => {
if (e.key === 'Enter') {
e.preventDefault();
selectedConversation && handleRename(selectedConversation);
}
};
const handleDragStart = (
e: DragEvent<HTMLButtonElement>,
conversation: Conversation,
) => {
if (e.dataTransfer) {
e.dataTransfer.setData('conversation', JSON.stringify(conversation));
}
};
const handleRename = (conversation: Conversation) => {
if (renameValue.trim().length > 0) {
handleUpdateConversation(conversation, {
key: 'name',
value: renameValue,
});
setRenameValue('');
setIsRenaming(false);
}
};
const handleConfirm: MouseEventHandler<HTMLButtonElement> = (e) => {
e.stopPropagation();
if (isDeleting) {
handleDeleteConversation(conversation);
} else if (isRenaming) {
handleRename(conversation);
}
setIsDeleting(false);
setIsRenaming(false);
};
const handleCancel: MouseEventHandler<HTMLButtonElement> = (e) => {
e.stopPropagation();
setIsDeleting(false);
setIsRenaming(false);
};
const handleOpenRenameModal: MouseEventHandler<HTMLButtonElement> = (e) => {
e.stopPropagation();
setIsRenaming(true);
selectedConversation && setRenameValue(selectedConversation.name);
};
const handleOpenDeleteModal: MouseEventHandler<HTMLButtonElement> = (e) => {
e.stopPropagation();
setIsDeleting(true);
};
useEffect(() => {
if (isRenaming) {
setIsDeleting(false);
} else if (isDeleting) {
setIsRenaming(false);
}
}, [isRenaming, isDeleting]);
return (
<div className="relative flex items-center">
{isRenaming && selectedConversation?.id === conversation.id ? (
<div className="flex w-full items-center gap-3 rounded-lg bg-[#343541]/90 p-3">
<IconMessage size={18} />
<input
className="mr-12 flex-1 overflow-hidden overflow-ellipsis border-neutral-400 bg-transparent text-left text-[12.5px] leading-3 text-white outline-none focus:border-neutral-100"
type="text"
value={renameValue}
onChange={(e) => setRenameValue(e.target.value)}
onKeyDown={handleEnterDown}
autoFocus
/>
</div>
) : (
<button
className={`flex w-full cursor-pointer items-center gap-3 rounded-lg p-3 text-sm transition-colors duration-200 hover:bg-[#343541]/90 ${
messageIsStreaming ? 'disabled:cursor-not-allowed' : ''
} ${
selectedConversation?.id === conversation.id
? 'bg-[#343541]/90'
: ''
}`}
onClick={() => handleSelectConversation(conversation)}
disabled={messageIsStreaming}
draggable="true"
onDragStart={(e) => handleDragStart(e, conversation)}
>
<IconMessage size={18} />
<div
className={`relative max-h-5 flex-1 overflow-hidden text-ellipsis whitespace-nowrap break-all text-left text-[12.5px] leading-3 ${
selectedConversation?.id === conversation.id ? 'pr-12' : 'pr-1'
}`}
>
{conversation.name}
</div>
</button>
)}
{(isDeleting || isRenaming) &&
selectedConversation?.id === conversation.id && (
<div className="absolute right-1 z-10 flex text-gray-300">
<SidebarActionButton handleClick={handleConfirm}>
<IconCheck size={18} />
</SidebarActionButton>
<SidebarActionButton handleClick={handleCancel}>
<IconX size={18} />
</SidebarActionButton>
</div>
)}
{selectedConversation?.id === conversation.id &&
!isDeleting &&
!isRenaming && (
<div className="absolute right-1 z-10 flex text-gray-300">
<SidebarActionButton handleClick={handleOpenRenameModal}>
<IconPencil size={18} />
</SidebarActionButton>
<SidebarActionButton handleClick={handleOpenDeleteModal}>
<IconTrash size={18} />
</SidebarActionButton>
</div>
)}
</div>
);
};