Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/components/EmptyChatMessageInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const EmptyChatMessageInput = () => {
const [message, setMessage] = useState('');

const inputRef = useRef<HTMLTextAreaElement | null>(null);
const isComposing = useRef(false);
const lastCompositionEndTime = useRef(0);

useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
Expand Down Expand Up @@ -46,13 +48,6 @@ const EmptyChatMessageInput = () => {
sendMessage(message);
setMessage('');
}}
onKeyDown={(e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
sendMessage(message);
setMessage('');
}
}}
className="w-full"
>
<div className="flex flex-col bg-light-secondary dark:bg-dark-secondary px-3 pt-5 pb-3 rounded-2xl w-full border border-light-200 dark:border-dark-200 shadow-sm shadow-light-200/10 dark:shadow-black/20 transition-all duration-200 focus-within:border-light-300 dark:focus-within:border-dark-300">
Expand All @@ -61,6 +56,27 @@ const EmptyChatMessageInput = () => {
value={message}
onChange={(e) => setMessage(e.target.value)}
minRows={2}
onKeyDown={(e) => {
if (
e.key === 'Enter' &&
!e.shiftKey &&
!e.nativeEvent.isComposing &&
!isComposing.current &&
e.keyCode !== 229 &&
Date.now() - lastCompositionEndTime.current > 150
) {
e.preventDefault();
sendMessage(message);
setMessage('');
}
}}
onCompositionStart={() => {
isComposing.current = true;
}}
onCompositionEnd={() => {
isComposing.current = false;
lastCompositionEndTime.current = Date.now();
}}
className="px-2 bg-transparent placeholder:text-[15px] 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"
placeholder="Ask anything..."
/>
Expand Down
32 changes: 25 additions & 7 deletions src/components/MessageInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const MessageInput = () => {
const [textareaRows, setTextareaRows] = useState(1);
const [mode, setMode] = useState<'multi' | 'single'>('single');

const isComposing = useRef(false);
const lastCompositionEndTime = useRef(0);

useEffect(() => {
if (textareaRows >= 2 && message && mode === 'single') {
setMode('multi');
Expand Down Expand Up @@ -53,13 +56,6 @@ const MessageInput = () => {
sendMessage(message);
setMessage('');
}}
onKeyDown={(e) => {
if (e.key === 'Enter' && !e.shiftKey && !loading) {
e.preventDefault();
sendMessage(message);
setMessage('');
}
}}
className={cn(
'relative bg-light-secondary dark:bg-dark-secondary p-4 flex items-center overflow-visible border border-light-200 dark:border-dark-200 shadow-sm shadow-light-200/10 dark:shadow-black/20 transition-all duration-200 focus-within:border-light-300 dark:focus-within:border-dark-300',
mode === 'multi' ? 'flex-col rounded-2xl' : 'flex-row rounded-full',
Expand All @@ -73,6 +69,28 @@ const MessageInput = () => {
onHeightChange={(height, props) => {
setTextareaRows(Math.ceil(height / props.rowHeight));
}}
onKeyDown={(e) => {
if (
e.key === 'Enter' &&
!e.shiftKey &&
!loading &&
!e.nativeEvent.isComposing &&
!isComposing.current &&
e.keyCode !== 229 &&
Date.now() - lastCompositionEndTime.current > 150
) {
e.preventDefault();
sendMessage(message);
setMessage('');
}
}}
onCompositionStart={() => {
isComposing.current = true;
}}
onCompositionEnd={() => {
isComposing.current = false;
lastCompositionEndTime.current = Date.now();
}}
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"
placeholder="Ask a follow-up"
/>
Expand Down