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>
This commit is contained in:
+231
-197
@@ -1,237 +1,271 @@
|
||||
import { Message } from '@/types/chat';
|
||||
import {
|
||||
IconCheck,
|
||||
IconCopy,
|
||||
IconEdit,
|
||||
IconUser,
|
||||
IconRobot,
|
||||
IconUser,
|
||||
} from '@tabler/icons-react';
|
||||
import { FC, memo, useContext, useEffect, useRef, useState } from 'react';
|
||||
|
||||
import { useTranslation } from 'next-i18next';
|
||||
import { FC, memo, useEffect, useRef, useState } from 'react';
|
||||
|
||||
import { updateConversation } from '@/utils/app/conversation';
|
||||
|
||||
import { Message } from '@/types/chat';
|
||||
|
||||
import HomeContext from '@/pages/api/home/home.context';
|
||||
|
||||
import { CodeBlock } from '../Markdown/CodeBlock';
|
||||
import { MemoizedReactMarkdown } from '../Markdown/MemoizedReactMarkdown';
|
||||
|
||||
import rehypeMathjax from 'rehype-mathjax';
|
||||
import remarkGfm from 'remark-gfm';
|
||||
import remarkMath from 'remark-math';
|
||||
import { CodeBlock } from '../Markdown/CodeBlock';
|
||||
import { MemoizedReactMarkdown } from '../Markdown/MemoizedReactMarkdown';
|
||||
|
||||
interface Props {
|
||||
message: Message;
|
||||
messageIndex: number;
|
||||
onEditMessage: (message: Message, messageIndex: number) => void;
|
||||
}
|
||||
|
||||
export const ChatMessage: FC<Props> = memo(
|
||||
({ message, messageIndex, onEditMessage }) => {
|
||||
const { t } = useTranslation('chat');
|
||||
const [isEditing, setIsEditing] = useState<boolean>(false);
|
||||
const [isTyping, setIsTyping] = useState<boolean>(false);
|
||||
const [messageContent, setMessageContent] = useState(message.content);
|
||||
const [messagedCopied, setMessageCopied] = useState(false);
|
||||
export const ChatMessage: FC<Props> = memo(({ message, messageIndex }) => {
|
||||
const { t } = useTranslation('chat');
|
||||
|
||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||
const {
|
||||
state: { selectedConversation, conversations },
|
||||
dispatch: homeDispatch,
|
||||
} = useContext(HomeContext);
|
||||
|
||||
const toggleEditing = () => {
|
||||
setIsEditing(!isEditing);
|
||||
};
|
||||
const [isEditing, setIsEditing] = useState<boolean>(false);
|
||||
const [isTyping, setIsTyping] = useState<boolean>(false);
|
||||
const [messageContent, setMessageContent] = useState(message.content);
|
||||
const [messagedCopied, setMessageCopied] = useState(false);
|
||||
|
||||
const handleInputChange = (
|
||||
event: React.ChangeEvent<HTMLTextAreaElement>,
|
||||
) => {
|
||||
setMessageContent(event.target.value);
|
||||
if (textareaRef.current) {
|
||||
textareaRef.current.style.height = 'inherit';
|
||||
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
|
||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||
|
||||
const toggleEditing = () => {
|
||||
setIsEditing(!isEditing);
|
||||
};
|
||||
|
||||
const handleInputChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||
setMessageContent(event.target.value);
|
||||
if (textareaRef.current) {
|
||||
textareaRef.current.style.height = 'inherit';
|
||||
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
|
||||
}
|
||||
};
|
||||
|
||||
const handleEditMessage = () => {
|
||||
if (message.content != messageContent) {
|
||||
if (selectedConversation) {
|
||||
const updatedMessages = selectedConversation.messages
|
||||
.map((m, i) => {
|
||||
if (i < messageIndex) {
|
||||
return m;
|
||||
}
|
||||
})
|
||||
.filter((m) => m) as Message[];
|
||||
|
||||
const updatedConversation = {
|
||||
...selectedConversation,
|
||||
messages: updatedMessages,
|
||||
};
|
||||
|
||||
const { single, all } = updateConversation(
|
||||
updatedConversation,
|
||||
conversations,
|
||||
);
|
||||
|
||||
homeDispatch({ field: 'selectedConversation', value: single });
|
||||
homeDispatch({ field: 'conversations', value: all });
|
||||
homeDispatch({
|
||||
field: 'currentMessage',
|
||||
value: { ...message, content: messageContent },
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
setIsEditing(false);
|
||||
};
|
||||
|
||||
const handleEditMessage = () => {
|
||||
if (message.content != messageContent) {
|
||||
onEditMessage({ ...message, content: messageContent }, messageIndex);
|
||||
}
|
||||
setIsEditing(false);
|
||||
};
|
||||
const handlePressEnter = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
||||
if (e.key === 'Enter' && !isTyping && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
handleEditMessage();
|
||||
}
|
||||
};
|
||||
|
||||
const handlePressEnter = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
||||
if (e.key === 'Enter' && !isTyping && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
handleEditMessage();
|
||||
}
|
||||
};
|
||||
const copyOnClick = () => {
|
||||
if (!navigator.clipboard) return;
|
||||
|
||||
const copyOnClick = () => {
|
||||
if (!navigator.clipboard) return;
|
||||
navigator.clipboard.writeText(message.content).then(() => {
|
||||
setMessageCopied(true);
|
||||
setTimeout(() => {
|
||||
setMessageCopied(false);
|
||||
}, 2000);
|
||||
});
|
||||
};
|
||||
|
||||
navigator.clipboard.writeText(message.content).then(() => {
|
||||
setMessageCopied(true);
|
||||
setTimeout(() => {
|
||||
setMessageCopied(false);
|
||||
}, 2000);
|
||||
});
|
||||
};
|
||||
useEffect(() => {
|
||||
if (textareaRef.current) {
|
||||
textareaRef.current.style.height = 'inherit';
|
||||
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
|
||||
}
|
||||
}, [isEditing]);
|
||||
|
||||
useEffect(() => {
|
||||
if (textareaRef.current) {
|
||||
textareaRef.current.style.height = 'inherit';
|
||||
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
|
||||
}
|
||||
}, [isEditing]);
|
||||
return (
|
||||
<div
|
||||
className={`group px-4 ${
|
||||
message.role === 'assistant'
|
||||
? 'border-b border-black/10 bg-gray-50 text-gray-800 dark:border-gray-900/50 dark:bg-[#444654] dark:text-gray-100'
|
||||
: 'border-b border-black/10 bg-white text-gray-800 dark:border-gray-900/50 dark:bg-[#343541] dark:text-gray-100'
|
||||
}`}
|
||||
style={{ overflowWrap: 'anywhere' }}
|
||||
>
|
||||
<div className="relative m-auto flex gap-4 p-4 text-base md:max-w-2xl md:gap-6 md:py-6 lg:max-w-2xl lg:px-0 xl:max-w-3xl">
|
||||
<div className="min-w-[40px] text-right font-bold">
|
||||
{message.role === 'assistant' ? (
|
||||
<IconRobot size={30} />
|
||||
) : (
|
||||
<IconUser size={30} />
|
||||
)}
|
||||
</div>
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`group px-4 ${
|
||||
message.role === 'assistant'
|
||||
? 'border-b border-black/10 bg-gray-50 text-gray-800 dark:border-gray-900/50 dark:bg-[#444654] dark:text-gray-100'
|
||||
: 'border-b border-black/10 bg-white text-gray-800 dark:border-gray-900/50 dark:bg-[#343541] dark:text-gray-100'
|
||||
}`}
|
||||
style={{ overflowWrap: 'anywhere' }}
|
||||
>
|
||||
<div className="relative m-auto flex gap-4 p-4 text-base md:max-w-2xl md:gap-6 md:py-6 lg:max-w-2xl lg:px-0 xl:max-w-3xl">
|
||||
<div className="min-w-[40px] text-right font-bold">
|
||||
{message.role === 'assistant' ? (
|
||||
<IconRobot size={30} />
|
||||
) : (
|
||||
<IconUser size={30} />
|
||||
)}
|
||||
</div>
|
||||
<div className="prose mt-[-2px] w-full dark:prose-invert">
|
||||
{message.role === 'user' ? (
|
||||
<div className="flex w-full">
|
||||
{isEditing ? (
|
||||
<div className="flex w-full flex-col">
|
||||
<textarea
|
||||
ref={textareaRef}
|
||||
className="w-full resize-none whitespace-pre-wrap border-none dark:bg-[#343541]"
|
||||
value={messageContent}
|
||||
onChange={handleInputChange}
|
||||
onKeyDown={handlePressEnter}
|
||||
onCompositionStart={() => setIsTyping(true)}
|
||||
onCompositionEnd={() => setIsTyping(false)}
|
||||
style={{
|
||||
fontFamily: 'inherit',
|
||||
fontSize: 'inherit',
|
||||
lineHeight: 'inherit',
|
||||
padding: '0',
|
||||
margin: '0',
|
||||
overflow: 'hidden',
|
||||
}}
|
||||
/>
|
||||
|
||||
<div className="prose mt-[-2px] w-full dark:prose-invert">
|
||||
{message.role === 'user' ? (
|
||||
<div className="flex w-full">
|
||||
{isEditing ? (
|
||||
<div className="flex w-full flex-col">
|
||||
<textarea
|
||||
ref={textareaRef}
|
||||
className="w-full resize-none whitespace-pre-wrap border-none dark:bg-[#343541]"
|
||||
value={messageContent}
|
||||
onChange={handleInputChange}
|
||||
onKeyDown={handlePressEnter}
|
||||
onCompositionStart={() => setIsTyping(true)}
|
||||
onCompositionEnd={() => setIsTyping(false)}
|
||||
style={{
|
||||
fontFamily: 'inherit',
|
||||
fontSize: 'inherit',
|
||||
lineHeight: 'inherit',
|
||||
padding: '0',
|
||||
margin: '0',
|
||||
overflow: 'hidden',
|
||||
<div className="mt-10 flex justify-center space-x-4">
|
||||
<button
|
||||
className="h-[40px] rounded-md bg-blue-500 px-4 py-1 text-sm font-medium text-white enabled:hover:bg-blue-600 disabled:opacity-50"
|
||||
onClick={handleEditMessage}
|
||||
disabled={messageContent.trim().length <= 0}
|
||||
>
|
||||
{t('Save & Submit')}
|
||||
</button>
|
||||
<button
|
||||
className="h-[40px] rounded-md border border-neutral-300 px-4 py-1 text-sm font-medium text-neutral-700 hover:bg-neutral-100 dark:border-neutral-700 dark:text-neutral-300 dark:hover:bg-neutral-800"
|
||||
onClick={() => {
|
||||
setMessageContent(message.content);
|
||||
setIsEditing(false);
|
||||
}}
|
||||
/>
|
||||
|
||||
<div className="mt-10 flex justify-center space-x-4">
|
||||
<button
|
||||
className="h-[40px] rounded-md bg-blue-500 px-4 py-1 text-sm font-medium text-white enabled:hover:bg-blue-600 disabled:opacity-50"
|
||||
onClick={handleEditMessage}
|
||||
disabled={messageContent.trim().length <= 0}
|
||||
>
|
||||
{t('Save & Submit')}
|
||||
</button>
|
||||
<button
|
||||
className="h-[40px] rounded-md border border-neutral-300 px-4 py-1 text-sm font-medium text-neutral-700 hover:bg-neutral-100 dark:border-neutral-700 dark:text-neutral-300 dark:hover:bg-neutral-800"
|
||||
onClick={() => {
|
||||
setMessageContent(message.content);
|
||||
setIsEditing(false);
|
||||
}}
|
||||
>
|
||||
{t('Cancel')}
|
||||
</button>
|
||||
</div>
|
||||
>
|
||||
{t('Cancel')}
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="prose whitespace-pre-wrap dark:prose-invert">
|
||||
{message.content}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<div className="prose whitespace-pre-wrap dark:prose-invert">
|
||||
{message.content}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{(window.innerWidth < 640 || !isEditing) && (
|
||||
<button
|
||||
className={`absolute translate-x-[1000px] text-gray-500 hover:text-gray-700 focus:translate-x-0 group-hover:translate-x-0 dark:text-gray-400 dark:hover:text-gray-300 ${
|
||||
window.innerWidth < 640
|
||||
? 'bottom-1 right-3'
|
||||
: 'right-0 top-[26px]'
|
||||
}
|
||||
{(window.innerWidth < 640 || !isEditing) && (
|
||||
<button
|
||||
className={`absolute translate-x-[1000px] text-gray-500 hover:text-gray-700 focus:translate-x-0 group-hover:translate-x-0 dark:text-gray-400 dark:hover:text-gray-300 ${
|
||||
window.innerWidth < 640
|
||||
? 'bottom-1 right-3'
|
||||
: 'right-0 top-[26px]'
|
||||
}
|
||||
`}
|
||||
onClick={toggleEditing}
|
||||
onClick={toggleEditing}
|
||||
>
|
||||
<IconEdit size={20} />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<div
|
||||
className={`absolute ${
|
||||
window.innerWidth < 640
|
||||
? 'bottom-1 right-3'
|
||||
: 'right-0 top-[26px] m-0'
|
||||
}`}
|
||||
>
|
||||
{messagedCopied ? (
|
||||
<IconCheck
|
||||
size={20}
|
||||
className="text-green-500 dark:text-green-400"
|
||||
/>
|
||||
) : (
|
||||
<button
|
||||
className="translate-x-[1000px] text-gray-500 hover:text-gray-700 focus:translate-x-0 group-hover:translate-x-0 dark:text-gray-400 dark:hover:text-gray-300"
|
||||
onClick={copyOnClick}
|
||||
>
|
||||
<IconEdit size={20} />
|
||||
<IconCopy size={20} />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<div
|
||||
className={`absolute ${
|
||||
window.innerWidth < 640
|
||||
? 'bottom-1 right-3'
|
||||
: 'right-0 top-[26px] m-0'
|
||||
}`}
|
||||
>
|
||||
{messagedCopied ? (
|
||||
<IconCheck
|
||||
size={20}
|
||||
className="text-green-500 dark:text-green-400"
|
||||
/>
|
||||
) : (
|
||||
<button
|
||||
className="translate-x-[1000px] text-gray-500 hover:text-gray-700 focus:translate-x-0 group-hover:translate-x-0 dark:text-gray-400 dark:hover:text-gray-300"
|
||||
onClick={copyOnClick}
|
||||
>
|
||||
<IconCopy size={20} />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<MemoizedReactMarkdown
|
||||
className="prose dark:prose-invert"
|
||||
remarkPlugins={[remarkGfm, remarkMath]}
|
||||
rehypePlugins={[rehypeMathjax]}
|
||||
components={{
|
||||
code({ node, inline, className, children, ...props }) {
|
||||
const match = /language-(\w+)/.exec(className || '');
|
||||
<MemoizedReactMarkdown
|
||||
className="prose dark:prose-invert"
|
||||
remarkPlugins={[remarkGfm, remarkMath]}
|
||||
rehypePlugins={[rehypeMathjax]}
|
||||
components={{
|
||||
code({ node, inline, className, children, ...props }) {
|
||||
const match = /language-(\w+)/.exec(className || '');
|
||||
|
||||
return !inline ? (
|
||||
<CodeBlock
|
||||
key={Math.random()}
|
||||
language={(match && match[1]) || ''}
|
||||
value={String(children).replace(/\n$/, '')}
|
||||
{...props}
|
||||
/>
|
||||
) : (
|
||||
<code className={className} {...props}>
|
||||
{children}
|
||||
</code>
|
||||
);
|
||||
},
|
||||
table({ children }) {
|
||||
return (
|
||||
<table className="border-collapse border border-black px-3 py-1 dark:border-white">
|
||||
{children}
|
||||
</table>
|
||||
);
|
||||
},
|
||||
th({ children }) {
|
||||
return (
|
||||
<th className="break-words border border-black bg-gray-500 px-3 py-1 text-white dark:border-white">
|
||||
{children}
|
||||
</th>
|
||||
);
|
||||
},
|
||||
td({ children }) {
|
||||
return (
|
||||
<td className="break-words border border-black px-3 py-1 dark:border-white">
|
||||
{children}
|
||||
</td>
|
||||
);
|
||||
},
|
||||
}}
|
||||
>
|
||||
{message.content}
|
||||
</MemoizedReactMarkdown>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
return !inline ? (
|
||||
<CodeBlock
|
||||
key={Math.random()}
|
||||
language={(match && match[1]) || ''}
|
||||
value={String(children).replace(/\n$/, '')}
|
||||
{...props}
|
||||
/>
|
||||
) : (
|
||||
<code className={className} {...props}>
|
||||
{children}
|
||||
</code>
|
||||
);
|
||||
},
|
||||
table({ children }) {
|
||||
return (
|
||||
<table className="border-collapse border border-black px-3 py-1 dark:border-white">
|
||||
{children}
|
||||
</table>
|
||||
);
|
||||
},
|
||||
th({ children }) {
|
||||
return (
|
||||
<th className="break-words border border-black bg-gray-500 px-3 py-1 text-white dark:border-white">
|
||||
{children}
|
||||
</th>
|
||||
);
|
||||
},
|
||||
td({ children }) {
|
||||
return (
|
||||
<td className="break-words border border-black px-3 py-1 dark:border-white">
|
||||
{children}
|
||||
</td>
|
||||
);
|
||||
},
|
||||
}}
|
||||
>
|
||||
{message.content}
|
||||
</MemoizedReactMarkdown>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
);
|
||||
</div>
|
||||
);
|
||||
});
|
||||
ChatMessage.displayName = 'ChatMessage';
|
||||
|
||||
Reference in New Issue
Block a user