2024-04-09 10:51:05 +00:00
|
|
|
import { ArrowRight } from 'lucide-react';
|
2024-06-23 05:16:22 +00:00
|
|
|
import { useEffect, useRef, useState } from 'react';
|
2024-04-09 10:51:05 +00:00
|
|
|
import TextareaAutosize from 'react-textarea-autosize';
|
2024-05-31 05:32:37 +00:00
|
|
|
import CopilotToggle from './MessageInputActions/Copilot';
|
|
|
|
import Focus from './MessageInputActions/Focus';
|
2024-10-11 05:05:59 +00:00
|
|
|
import Optimization from './MessageInputActions/Optimization';
|
2024-11-23 09:34:19 +00:00
|
|
|
import Attach from './MessageInputActions/Attach';
|
|
|
|
import { File } from './ChatWindow';
|
2024-04-09 10:51:05 +00:00
|
|
|
|
|
|
|
const EmptyChatMessageInput = ({
|
|
|
|
sendMessage,
|
2024-04-13 06:41:47 +00:00
|
|
|
focusMode,
|
|
|
|
setFocusMode,
|
2024-10-11 05:05:59 +00:00
|
|
|
optimizationMode,
|
|
|
|
setOptimizationMode,
|
2024-11-23 09:34:19 +00:00
|
|
|
fileIds,
|
|
|
|
setFileIds,
|
|
|
|
files,
|
|
|
|
setFiles,
|
2024-04-09 10:51:05 +00:00
|
|
|
}: {
|
|
|
|
sendMessage: (message: string) => void;
|
2024-04-13 06:41:47 +00:00
|
|
|
focusMode: string;
|
|
|
|
setFocusMode: (mode: string) => void;
|
2024-10-11 05:05:59 +00:00
|
|
|
optimizationMode: string;
|
|
|
|
setOptimizationMode: (mode: string) => void;
|
2024-11-23 09:34:19 +00:00
|
|
|
fileIds: string[];
|
|
|
|
setFileIds: (fileIds: string[]) => void;
|
|
|
|
files: File[];
|
|
|
|
setFiles: (files: File[]) => void;
|
2024-04-09 10:51:05 +00:00
|
|
|
}) => {
|
|
|
|
const [copilotEnabled, setCopilotEnabled] = useState(false);
|
|
|
|
const [message, setMessage] = useState('');
|
|
|
|
|
2024-06-23 05:16:22 +00:00
|
|
|
const inputRef = useRef<HTMLTextAreaElement | null>(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-09-02 06:14:40 +00:00
|
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
|
|
const activeElement = document.activeElement;
|
|
|
|
|
|
|
|
const isInputFocused =
|
|
|
|
activeElement?.tagName === 'INPUT' ||
|
|
|
|
activeElement?.tagName === 'TEXTAREA' ||
|
|
|
|
activeElement?.hasAttribute('contenteditable');
|
|
|
|
|
|
|
|
if (e.key === '/' && !isInputFocused) {
|
|
|
|
e.preventDefault();
|
|
|
|
inputRef.current?.focus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-06-23 05:16:22 +00:00
|
|
|
document.addEventListener('keydown', handleKeyDown);
|
|
|
|
|
2024-11-05 04:00:08 +00:00
|
|
|
inputRef.current?.focus();
|
|
|
|
|
2024-06-23 05:16:22 +00:00
|
|
|
return () => {
|
|
|
|
document.removeEventListener('keydown', handleKeyDown);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2024-04-09 10:51:05 +00:00
|
|
|
return (
|
|
|
|
<form
|
|
|
|
onSubmit={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
sendMessage(message);
|
|
|
|
setMessage('');
|
|
|
|
}}
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
if (e.key === 'Enter' && !e.shiftKey) {
|
|
|
|
e.preventDefault();
|
|
|
|
sendMessage(message);
|
|
|
|
setMessage('');
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
className="w-full"
|
|
|
|
>
|
2024-05-29 04:22:29 +00:00
|
|
|
<div className="flex flex-col bg-light-secondary dark:bg-dark-secondary px-5 pt-5 pb-2 rounded-lg w-full border border-light-200 dark:border-dark-200">
|
2024-04-09 10:51:05 +00:00
|
|
|
<TextareaAutosize
|
2024-06-23 05:16:22 +00:00
|
|
|
ref={inputRef}
|
2024-04-09 10:51:05 +00:00
|
|
|
value={message}
|
|
|
|
onChange={(e) => setMessage(e.target.value)}
|
|
|
|
minRows={2}
|
2024-05-24 12:29:49 +00:00
|
|
|
className="bg-transparent placeholder:text-black/50 dark:placeholder:text-white/50 text-sm text-black dark:text-white resize-none focus:outline-none w-full max-h-24 lg:max-h-36 xl:max-h-48"
|
2024-04-09 10:51:05 +00:00
|
|
|
placeholder="Ask anything..."
|
|
|
|
/>
|
|
|
|
<div className="flex flex-row items-center justify-between mt-4">
|
2024-11-23 09:34:19 +00:00
|
|
|
<div className="flex flex-row items-center space-x-2 lg:space-x-4">
|
2024-04-13 06:41:47 +00:00
|
|
|
<Focus focusMode={focusMode} setFocusMode={setFocusMode} />
|
2024-11-23 09:34:19 +00:00
|
|
|
<Attach
|
|
|
|
fileIds={fileIds}
|
|
|
|
setFileIds={setFileIds}
|
|
|
|
files={files}
|
|
|
|
setFiles={setFiles}
|
|
|
|
showText
|
|
|
|
/>
|
2024-04-09 10:51:05 +00:00
|
|
|
</div>
|
2024-10-11 05:05:59 +00:00
|
|
|
<div className="flex flex-row items-center space-x-1 sm:space-x-4">
|
|
|
|
<Optimization
|
|
|
|
optimizationMode={optimizationMode}
|
|
|
|
setOptimizationMode={setOptimizationMode}
|
2024-04-09 10:51:05 +00:00
|
|
|
/>
|
|
|
|
<button
|
|
|
|
disabled={message.trim().length === 0}
|
2024-05-27 23:30:28 +00:00
|
|
|
className="bg-[#24A0ED] text-white disabled:text-black/50 dark:disabled:text-white/50 disabled:bg-[#e0e0dc] dark:disabled:bg-[#ececec21] hover:bg-opacity-85 transition duration-100 rounded-full p-2"
|
2024-04-09 10:51:05 +00:00
|
|
|
>
|
|
|
|
<ArrowRight className="bg-background" size={17} />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EmptyChatMessageInput;
|