|
|
@ -1,8 +1,12 @@ |
|
|
|
|
|
import { debounce } from "@app/core/utils/debounce"; |
|
|
import { Button } from "@components/UI/Button.tsx"; |
|
|
import { Button } from "@components/UI/Button.tsx"; |
|
|
import { Input } from "@components/UI/Input.tsx"; |
|
|
import { Input } from "@components/UI/Input.tsx"; |
|
|
import { useDevice } from "@core/stores/deviceStore.ts"; |
|
|
import { useDevice } from "@core/stores/deviceStore.ts"; |
|
|
import type { Types } from "@meshtastic/js"; |
|
|
import type { Types } from "@meshtastic/js"; |
|
|
import { SendIcon } from "lucide-react"; |
|
|
import { SendIcon } from "lucide-react"; |
|
|
|
|
|
import { useCallback, useState, useMemo } from "react"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export interface MessageInputProps { |
|
|
export interface MessageInputProps { |
|
|
to: Types.Destination; |
|
|
to: Types.Destination; |
|
|
@ -20,10 +24,15 @@ export const MessageInput = ({ |
|
|
setMessageDraft, |
|
|
setMessageDraft, |
|
|
hardware, |
|
|
hardware, |
|
|
} = useDevice(); |
|
|
} = useDevice(); |
|
|
|
|
|
|
|
|
const myNodeNum = hardware.myNodeNum; |
|
|
const myNodeNum = hardware.myNodeNum; |
|
|
|
|
|
const [localDraft, setLocalDraft] = useState(messageDraft); |
|
|
|
|
|
|
|
|
const sendText = async (message: string) => { |
|
|
const debouncedSetMessageDraft = useMemo( |
|
|
|
|
|
() => debounce(setMessageDraft, 300), |
|
|
|
|
|
[setMessageDraft] |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const sendText = useCallback(async (message: string) => { |
|
|
await connection |
|
|
await connection |
|
|
?.sendText(message, to, true, channel) |
|
|
?.sendText(message, to, true, channel) |
|
|
.then((id) => |
|
|
.then((id) => |
|
|
@ -46,6 +55,12 @@ export const MessageInput = ({ |
|
|
e.error, |
|
|
e.error, |
|
|
), |
|
|
), |
|
|
); |
|
|
); |
|
|
|
|
|
}, [channel, connection, myNodeNum, setMessageState, to]); |
|
|
|
|
|
|
|
|
|
|
|
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
|
|
|
|
|
const newValue = e.target.value; |
|
|
|
|
|
setLocalDraft(newValue); |
|
|
|
|
|
debouncedSetMessageDraft(newValue); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
return ( |
|
|
return ( |
|
|
@ -54,7 +69,8 @@ export const MessageInput = ({ |
|
|
className="w-full" |
|
|
className="w-full" |
|
|
onSubmit={(e) => { |
|
|
onSubmit={(e) => { |
|
|
e.preventDefault(); |
|
|
e.preventDefault(); |
|
|
sendText(messageDraft); |
|
|
sendText(localDraft); |
|
|
|
|
|
setLocalDraft(""); |
|
|
setMessageDraft(""); |
|
|
setMessageDraft(""); |
|
|
}} |
|
|
}} |
|
|
> |
|
|
> |
|
|
@ -64,8 +80,8 @@ export const MessageInput = ({ |
|
|
autoFocus={true} |
|
|
autoFocus={true} |
|
|
minLength={1} |
|
|
minLength={1} |
|
|
placeholder="Enter Message" |
|
|
placeholder="Enter Message" |
|
|
value={messageDraft} |
|
|
value={localDraft} |
|
|
onChange={(e) => setMessageDraft(e.target.value)} |
|
|
onChange={handleInputChange} |
|
|
/> |
|
|
/> |
|
|
</span> |
|
|
</span> |
|
|
<Button type="submit"> |
|
|
<Button type="submit"> |
|
|
@ -75,4 +91,4 @@ export const MessageInput = ({ |
|
|
</form> |
|
|
</form> |
|
|
</div> |
|
|
</div> |
|
|
); |
|
|
); |
|
|
}; |
|
|
}; |