代码优化
This commit is contained in:
@@ -266,12 +266,29 @@ export function MessageInput({
|
|||||||
const lastDomDropAtRef = useRef(0)
|
const lastDomDropAtRef = useRef(0)
|
||||||
const composingRef = useRef(false)
|
const composingRef = useRef(false)
|
||||||
const textRef = useRef(text)
|
const textRef = useRef(text)
|
||||||
|
const disabledRef = useRef(disabled)
|
||||||
|
const isPromptingRef = useRef(isPrompting)
|
||||||
|
const dragActiveRef = useRef(false)
|
||||||
const canAttachImages = promptCapabilities.image
|
const canAttachImages = promptCapabilities.image
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
textRef.current = text
|
textRef.current = text
|
||||||
}, [text])
|
}, [text])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
disabledRef.current = disabled
|
||||||
|
}, [disabled])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
isPromptingRef.current = isPrompting
|
||||||
|
}, [isPrompting])
|
||||||
|
|
||||||
|
const setDragActiveIfChanged = useCallback((next: boolean) => {
|
||||||
|
if (dragActiveRef.current === next) return
|
||||||
|
dragActiveRef.current = next
|
||||||
|
setIsDragActive(next)
|
||||||
|
}, [])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!effectiveDraftStorageKey) return
|
if (!effectiveDraftStorageKey) return
|
||||||
saveMessageInputDraft(effectiveDraftStorageKey, text)
|
saveMessageInputDraft(effectiveDraftStorageKey, text)
|
||||||
@@ -584,6 +601,11 @@ export function MessageInput({
|
|||||||
[appendImagePathAttachments, appendResourceAttachments, canAttachImages]
|
[appendImagePathAttachments, appendResourceAttachments, canAttachImages]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const appendPathsFromDropRef = useRef(appendPathsFromDrop)
|
||||||
|
useEffect(() => {
|
||||||
|
appendPathsFromDropRef.current = appendPathsFromDrop
|
||||||
|
}, [appendPathsFromDrop])
|
||||||
|
|
||||||
const appendFilesFromInput = useCallback(
|
const appendFilesFromInput = useCallback(
|
||||||
async (files: File[]) => {
|
async (files: File[]) => {
|
||||||
if (files.length === 0) return
|
if (files.length === 0) return
|
||||||
@@ -713,20 +735,22 @@ export function MessageInput({
|
|||||||
const host = containerRef.current
|
const host = containerRef.current
|
||||||
if (!host) return
|
if (!host) return
|
||||||
if (payload.type === "leave") {
|
if (payload.type === "leave") {
|
||||||
setIsDragActive(false)
|
setDragActiveIfChanged(false)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const inside = pointWithinElement(payload.position, host)
|
const inside = pointWithinElement(payload.position, host)
|
||||||
if (payload.type === "drop") {
|
if (payload.type === "drop") {
|
||||||
setIsDragActive(false)
|
setDragActiveIfChanged(false)
|
||||||
if (Date.now() - lastDomDropAtRef.current < 250) return
|
if (Date.now() - lastDomDropAtRef.current < 250) return
|
||||||
if (!inside || disabled || isPrompting) return
|
if (!inside || disabledRef.current || isPromptingRef.current) return
|
||||||
void appendPathsFromDrop(payload.paths).catch((error) => {
|
void appendPathsFromDropRef.current(payload.paths).catch((error) => {
|
||||||
console.error("[MessageInput] drag drop paths failed:", error)
|
console.error("[MessageInput] drag drop paths failed:", error)
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
setIsDragActive(inside && !disabled && !isPrompting)
|
setDragActiveIfChanged(
|
||||||
|
inside && !disabledRef.current && !isPromptingRef.current
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const setup = async () => {
|
const setup = async () => {
|
||||||
@@ -792,7 +816,7 @@ export function MessageInput({
|
|||||||
cancelled = true
|
cancelled = true
|
||||||
cleanupListeners()
|
cleanupListeners()
|
||||||
}
|
}
|
||||||
}, [appendPathsFromDrop, disabled, isPrompting])
|
}, [setDragActiveIfChanged])
|
||||||
|
|
||||||
const removeAttachment = useCallback((id: string) => {
|
const removeAttachment = useCallback((id: string) => {
|
||||||
setAttachments((prev) => prev.filter((item) => item.id !== id))
|
setAttachments((prev) => prev.filter((item) => item.id !== id))
|
||||||
@@ -910,10 +934,10 @@ export function MessageInput({
|
|||||||
if (!hasDragFiles(event.dataTransfer)) return
|
if (!hasDragFiles(event.dataTransfer)) return
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
if (!disabled && !isPrompting) {
|
if (!disabled && !isPrompting) {
|
||||||
setIsDragActive(true)
|
setDragActiveIfChanged(true)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[disabled, isPrompting]
|
[disabled, isPrompting, setDragActiveIfChanged]
|
||||||
)
|
)
|
||||||
|
|
||||||
const handleContainerDragLeave = useCallback(
|
const handleContainerDragLeave = useCallback(
|
||||||
@@ -926,9 +950,9 @@ export function MessageInput({
|
|||||||
) {
|
) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
setIsDragActive(false)
|
setDragActiveIfChanged(false)
|
||||||
},
|
},
|
||||||
[]
|
[setDragActiveIfChanged]
|
||||||
)
|
)
|
||||||
|
|
||||||
const handleContainerDrop = useCallback(
|
const handleContainerDrop = useCallback(
|
||||||
@@ -936,7 +960,7 @@ export function MessageInput({
|
|||||||
if (!hasDragFiles(event.dataTransfer)) return
|
if (!hasDragFiles(event.dataTransfer)) return
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
lastDomDropAtRef.current = Date.now()
|
lastDomDropAtRef.current = Date.now()
|
||||||
setIsDragActive(false)
|
setDragActiveIfChanged(false)
|
||||||
if (disabled || isPrompting) return
|
if (disabled || isPrompting) return
|
||||||
const files = Array.from(event.dataTransfer.files ?? [])
|
const files = Array.from(event.dataTransfer.files ?? [])
|
||||||
if (files.length > 0) {
|
if (files.length > 0) {
|
||||||
@@ -945,7 +969,7 @@ export function MessageInput({
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[appendFilesFromInput, disabled, isPrompting]
|
[appendFilesFromInput, disabled, isPrompting, setDragActiveIfChanged]
|
||||||
)
|
)
|
||||||
|
|
||||||
const hasImageAttachments = imageAttachments.length > 0
|
const hasImageAttachments = imageAttachments.length > 0
|
||||||
|
|||||||
Reference in New Issue
Block a user