|
1 | 1 | 'use client' |
2 | 2 |
|
3 | | -import { useLayoutEffect, useRef } from 'react' |
| 3 | +import { useCallback, useEffect, useLayoutEffect, useRef } from 'react' |
4 | 4 | import { cn } from '@/lib/core/utils/cn' |
5 | 5 | import { MessageActions } from '@/app/workspace/[workspaceId]/components' |
6 | 6 | import { ChatMessageAttachments } from '@/app/workspace/[workspaceId]/home/components/chat-message-attachments' |
@@ -99,6 +99,43 @@ export function MothershipChat({ |
99 | 99 | const hasMessages = messages.length > 0 |
100 | 100 | const initialScrollDoneRef = useRef(false) |
101 | 101 |
|
| 102 | + const primedQueueIdRef = useRef<string | null>(null) |
| 103 | + const primeTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null) |
| 104 | + const messageQueueRef = useRef(messageQueue) |
| 105 | + messageQueueRef.current = messageQueue |
| 106 | + const onSendQueuedMessageRef = useRef(onSendQueuedMessage) |
| 107 | + onSendQueuedMessageRef.current = onSendQueuedMessage |
| 108 | + |
| 109 | + const clearPrimed = useCallback(() => { |
| 110 | + primedQueueIdRef.current = null |
| 111 | + if (primeTimerRef.current) { |
| 112 | + clearTimeout(primeTimerRef.current) |
| 113 | + primeTimerRef.current = null |
| 114 | + } |
| 115 | + }, []) |
| 116 | + |
| 117 | + const handleEnterWhileEmpty = useCallback(() => { |
| 118 | + const topMessage = messageQueueRef.current[0] |
| 119 | + if (!topMessage) return false |
| 120 | + |
| 121 | + if (primedQueueIdRef.current === topMessage.id) { |
| 122 | + clearPrimed() |
| 123 | + void onSendQueuedMessageRef.current(topMessage.id) |
| 124 | + return true |
| 125 | + } |
| 126 | + |
| 127 | + primedQueueIdRef.current = topMessage.id |
| 128 | + if (primeTimerRef.current) clearTimeout(primeTimerRef.current) |
| 129 | + primeTimerRef.current = setTimeout(clearPrimed, 3000) |
| 130 | + return true |
| 131 | + }, [clearPrimed]) |
| 132 | + |
| 133 | + useEffect(() => { |
| 134 | + return () => { |
| 135 | + if (primeTimerRef.current) clearTimeout(primeTimerRef.current) |
| 136 | + } |
| 137 | + }, []) |
| 138 | + |
102 | 139 | useLayoutEffect(() => { |
103 | 140 | if (!hasMessages) { |
104 | 141 | initialScrollDoneRef.current = false |
@@ -197,6 +234,8 @@ export function MothershipChat({ |
197 | 234 | onContextAdd={onContextAdd} |
198 | 235 | editValue={editValue} |
199 | 236 | onEditValueConsumed={onEditValueConsumed} |
| 237 | + onEnterWhileEmpty={handleEnterWhileEmpty} |
| 238 | + onPrimedDismiss={clearPrimed} |
200 | 239 | /> |
201 | 240 | </div> |
202 | 241 | </div> |
|
0 commit comments