mirror of
https://github.com/simstudioai/sim.git
synced 2026-04-28 03:00:29 -04:00
fix(home): simplify enter-to-send queued message to single press (#4008)
* fix(home): simplify enter-to-send queued message to single press * fix(home): prevent empty submit fallthrough when sending with empty input
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
'use client'
|
||||
|
||||
import { useCallback, useEffect, useLayoutEffect, useRef } from 'react'
|
||||
import { useCallback, useLayoutEffect, useRef } from 'react'
|
||||
import { cn } from '@/lib/core/utils/cn'
|
||||
import { MessageActions } from '@/app/workspace/[workspaceId]/components'
|
||||
import { ChatMessageAttachments } from '@/app/workspace/[workspaceId]/home/components/chat-message-attachments'
|
||||
@@ -99,41 +99,16 @@ export function MothershipChat({
|
||||
const hasMessages = messages.length > 0
|
||||
const initialScrollDoneRef = useRef(false)
|
||||
|
||||
const primedQueueIdRef = useRef<string | null>(null)
|
||||
const primeTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
const messageQueueRef = useRef(messageQueue)
|
||||
messageQueueRef.current = messageQueue
|
||||
const onSendQueuedMessageRef = useRef(onSendQueuedMessage)
|
||||
onSendQueuedMessageRef.current = onSendQueuedMessage
|
||||
|
||||
const clearPrimed = useCallback(() => {
|
||||
primedQueueIdRef.current = null
|
||||
if (primeTimerRef.current) {
|
||||
clearTimeout(primeTimerRef.current)
|
||||
primeTimerRef.current = null
|
||||
}
|
||||
}, [])
|
||||
|
||||
const handleEnterWhileEmpty = useCallback(() => {
|
||||
const topMessage = messageQueueRef.current[0]
|
||||
if (!topMessage) return false
|
||||
|
||||
if (primedQueueIdRef.current === topMessage.id) {
|
||||
clearPrimed()
|
||||
void onSendQueuedMessageRef.current(topMessage.id)
|
||||
return true
|
||||
}
|
||||
|
||||
primedQueueIdRef.current = topMessage.id
|
||||
if (primeTimerRef.current) clearTimeout(primeTimerRef.current)
|
||||
primeTimerRef.current = setTimeout(clearPrimed, 3000)
|
||||
void onSendQueuedMessageRef.current(topMessage.id)
|
||||
return true
|
||||
}, [clearPrimed])
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (primeTimerRef.current) clearTimeout(primeTimerRef.current)
|
||||
}
|
||||
}, [])
|
||||
|
||||
useLayoutEffect(() => {
|
||||
@@ -235,7 +210,6 @@ export function MothershipChat({
|
||||
editValue={editValue}
|
||||
onEditValueConsumed={onEditValueConsumed}
|
||||
onEnterWhileEmpty={handleEnterWhileEmpty}
|
||||
onPrimedDismiss={clearPrimed}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -109,7 +109,6 @@ interface UserInputProps {
|
||||
userId?: string
|
||||
onContextAdd?: (context: ChatContext) => void
|
||||
onEnterWhileEmpty?: () => boolean
|
||||
onPrimedDismiss?: () => void
|
||||
}
|
||||
|
||||
export function UserInput({
|
||||
@@ -123,7 +122,6 @@ export function UserInput({
|
||||
userId,
|
||||
onContextAdd,
|
||||
onEnterWhileEmpty,
|
||||
onPrimedDismiss,
|
||||
}: UserInputProps) {
|
||||
const { workspaceId } = useParams<{ workspaceId: string }>()
|
||||
const { data: workflowsById = {} } = useWorkflowMap(workspaceId)
|
||||
@@ -456,7 +454,8 @@ export function UserInput({
|
||||
(e: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
||||
if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing) {
|
||||
e.preventDefault()
|
||||
if (isSendingRef.current && !valueRef.current.trim() && onEnterWhileEmptyRef.current?.()) {
|
||||
if (isSendingRef.current && !valueRef.current.trim()) {
|
||||
onEnterWhileEmptyRef.current?.()
|
||||
return
|
||||
}
|
||||
handleSubmit()
|
||||
@@ -551,9 +550,8 @@ export function UserInput({
|
||||
|
||||
setValue(newValue)
|
||||
restartRecognition(newValue)
|
||||
if (newValue.trim()) onPrimedDismiss?.()
|
||||
},
|
||||
[restartRecognition, onPrimedDismiss]
|
||||
[restartRecognition]
|
||||
)
|
||||
|
||||
const handleSelectAdjust = useCallback(() => {
|
||||
|
||||
Reference in New Issue
Block a user