import { memo, useState, useRef, useEffect } from 'react' import { Send, X, Pencil, Loader2 } from 'lucide-react' import { useRefreshMessage } from '@/hooks/useRemoveMessage' import { useUIState } from '@/stores/uiStateStore' import { useMobile } from '@/hooks/useMobile' import { useSessionAgent } from '@/hooks/useSessionAgent' interface EditableUserMessageProps { sessionId: string directory?: string content: string assistantMessageId: string onCancel: () => void model?: string } export const EditableUserMessage = memo(function EditableUserMessage({ sessionId, directory, content, assistantMessageId, onCancel, model }: EditableUserMessageProps) { const [editedContent, setEditedContent] = useState(content) const textareaRef = useRef(null) const isMobile = useMobile() const refreshMessage = useRefreshMessage({ sessionId, directory }) const setIsEditingMessage = useUIState((state) => state.setIsEditingMessage) const sessionAgent = useSessionAgent(sessionId, directory) const currentMode = sessionAgent.agent useEffect(() => { if (textareaRef.current) { textareaRef.current.focus() textareaRef.current.setSelectionRange( textareaRef.current.value.length, textareaRef.current.value.length ) } }, []) useEffect(() => { if (textareaRef.current) { textareaRef.current.style.height = 'auto' textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px` } }, [editedContent]) useEffect(() => { return () => { setIsEditingMessage(false) } }, [setIsEditingMessage]) const handleSubmit = () => { if (!editedContent.trim() || refreshMessage.isPending) return refreshMessage.mutate({ assistantMessageID: assistantMessageId, userMessageContent: editedContent.trim(), model, agent: currentMode }, { onSuccess: () => { onCancel() } }) } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) { e.preventDefault() handleSubmit() } else if (e.key === 'Escape') { e.preventDefault() onCancel() } } return (