-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
Copy pathAsk.tsx
70 lines (63 loc) · 2.09 KB
/
Ask.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { useState, useEffect, useRef } from 'react';
import { invoke } from '@tauri-apps/api/core';
import { useHotkeys } from 'react-hotkeys-hook';
import useInfo from '~hooks/useInfo';
import SendIcon from '~icons/Send';
import debounce from 'lodash/debounce';
export default function ChatInput() {
const inputRef = useRef<HTMLTextAreaElement>(null);
const [message, setMessage] = useState('');
const { isMac } = useInfo();
useEffect(() => {
const syncMessage = debounce(async () => {
try {
await invoke('ask_sync', { message: JSON.stringify(message) });
} catch (error) {
console.error('Error syncing message:', error);
}
}, 300); // Debounce by 300ms
syncMessage();
return () => syncMessage.cancel(); // Cleanup debounce on unmount
}, [message]);
useHotkeys(isMac ? 'meta+enter' : 'ctrl+enter', async (event: KeyboardEvent) => {
event.preventDefault();
await handleSend();
}, {
enableOnFormTags: true,
}, [message]);
const handleInput = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setMessage(e.target.value);
};
const handleSend = async () => {
if (!message) return;
try {
await invoke('ask_send', { message: JSON.stringify(message) });
} catch (error) {
console.error('Error sending message:', error);
}
setMessage('');
if (inputRef.current) {
inputRef.current.value = '';
inputRef.current.focus();
}
};
return (
<div className="relative flex h-full dark:bg-app-gray-2/[0.98] bg-gray-100 dark:text-slate-200 items-center gap-1">
<textarea
ref={inputRef}
onChange={handleInput}
spellCheck="false"
autoFocus
className="w-full h-full pl-3 pr-[40px] py-2 outline-none resize-none bg-transparent"
placeholder="Type your message here..."
/>
<SendIcon
size={30}
className="absolute right-2 text-gray-400/80 dark:text-gray-600 cursor-pointer"
onClick={handleSend}
title={`Send message (${isMac ? '⌘⏎' : '⌃⏎'})`}
aria-label="Send message"
/>
</div>
);
}