2024-04-09 10:51:05 +00:00
|
|
|
import { cn } from '@/lib/utils';
|
|
|
|
import { ArrowUp } from 'lucide-react';
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
import TextareaAutosize from 'react-textarea-autosize';
|
|
|
|
import { Attach, CopilotToggle } from './MessageInputActions';
|
|
|
|
|
|
|
|
const MessageInput = ({
|
|
|
|
sendMessage,
|
2024-04-24 04:36:56 +00:00
|
|
|
loading,
|
2024-04-09 10:51:05 +00:00
|
|
|
}: {
|
|
|
|
sendMessage: (message: string) => void;
|
2024-04-24 04:36:56 +00:00
|
|
|
loading: boolean;
|
2024-04-09 10:51:05 +00:00
|
|
|
}) => {
|
|
|
|
const [copilotEnabled, setCopilotEnabled] = useState(false);
|
|
|
|
const [message, setMessage] = useState('');
|
|
|
|
const [textareaRows, setTextareaRows] = useState(1);
|
|
|
|
const [mode, setMode] = useState<'multi' | 'single'>('single');
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (textareaRows >= 2 && message && mode === 'single') {
|
|
|
|
setMode('multi');
|
|
|
|
} else if (!message && mode === 'multi') {
|
|
|
|
setMode('single');
|
|
|
|
}
|
|
|
|
}, [textareaRows, mode, message]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form
|
|
|
|
onSubmit={(e) => {
|
2024-04-24 04:36:56 +00:00
|
|
|
if (loading) return;
|
2024-04-09 10:51:05 +00:00
|
|
|
e.preventDefault();
|
|
|
|
sendMessage(message);
|
|
|
|
setMessage('');
|
|
|
|
}}
|
|
|
|
onKeyDown={(e) => {
|
2024-04-24 04:36:56 +00:00
|
|
|
if (e.key === 'Enter' && !e.shiftKey && !loading) {
|
2024-04-09 10:51:05 +00:00
|
|
|
e.preventDefault();
|
|
|
|
sendMessage(message);
|
|
|
|
setMessage('');
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
className={cn(
|
2024-05-27 03:49:09 +00:00
|
|
|
'bg-light-primary dark:bg-dark-primary p-4 flex items-center overflow-hidden border border-light-300 dark:border-dark-200',
|
2024-04-09 10:51:05 +00:00
|
|
|
mode === 'multi' ? 'flex-col rounded-lg' : 'flex-row rounded-full',
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{mode === 'single' && <Attach />}
|
|
|
|
<TextareaAutosize
|
|
|
|
value={message}
|
|
|
|
onChange={(e) => setMessage(e.target.value)}
|
|
|
|
onHeightChange={(height, props) => {
|
|
|
|
setTextareaRows(Math.ceil(height / props.rowHeight));
|
|
|
|
}}
|
2024-05-24 12:29:49 +00:00
|
|
|
className="transition bg-transparent dark:placeholder:text-white/50 placeholder:text-sm text-sm dark:text-white resize-none focus:outline-none w-full px-2 max-h-24 lg:max-h-36 xl:max-h-48 flex-grow flex-shrink"
|
2024-04-09 10:51:05 +00:00
|
|
|
placeholder="Ask a follow-up"
|
|
|
|
/>
|
|
|
|
{mode === 'single' && (
|
|
|
|
<div className="flex flex-row items-center space-x-4">
|
|
|
|
<CopilotToggle
|
|
|
|
copilotEnabled={copilotEnabled}
|
|
|
|
setCopilotEnabled={setCopilotEnabled}
|
|
|
|
/>
|
|
|
|
<button
|
2024-04-24 04:36:56 +00:00
|
|
|
disabled={message.trim().length === 0 || loading}
|
2024-05-24 12:29:49 +00:00
|
|
|
className="bg-[#24A0ED] dark:text-white disabled:text-black/50 dark:disabled:text-white/50 hover:bg-opacity-85 transition duration-100 disabled:bg-[#e0e0dc] dark:disabled:bg-[#ececec21] rounded-full p-2"
|
2024-04-09 10:51:05 +00:00
|
|
|
>
|
|
|
|
<ArrowUp className="bg-background" size={17} />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{mode === 'multi' && (
|
|
|
|
<div className="flex flex-row items-center justify-between w-full pt-2">
|
|
|
|
<Attach />
|
|
|
|
<div className="flex flex-row items-center space-x-4">
|
|
|
|
<CopilotToggle
|
|
|
|
copilotEnabled={copilotEnabled}
|
|
|
|
setCopilotEnabled={setCopilotEnabled}
|
|
|
|
/>
|
|
|
|
<button
|
2024-04-24 04:36:56 +00:00
|
|
|
disabled={message.trim().length === 0 || loading}
|
2024-05-24 12:29:49 +00:00
|
|
|
className="bg-[#24A0ED] dark:text-white text-black/50 dark:disabled:text-white/50 hover:bg-opacity-85 transition duration-100 disabled:bg-[#e0e0dc] dark:disabled:bg-[#ececec21] rounded-full p-2"
|
2024-04-09 10:51:05 +00:00
|
|
|
>
|
|
|
|
<ArrowUp className="bg-background" size={17} />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default MessageInput;
|