-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathEditableUserMessage.tsx
More file actions
161 lines (145 loc) · 5.11 KB
/
EditableUserMessage.tsx
File metadata and controls
161 lines (145 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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 {
opcodeUrl: string
sessionId: string
directory?: string
content: string
assistantMessageId: string
onCancel: () => void
model?: string
}
export const EditableUserMessage = memo(function EditableUserMessage({
opcodeUrl,
sessionId,
directory,
content,
assistantMessageId,
onCancel,
model
}: EditableUserMessageProps) {
const [editedContent, setEditedContent] = useState(content)
const textareaRef = useRef<HTMLTextAreaElement>(null)
const isMobile = useMobile()
const refreshMessage = useRefreshMessage({ opcodeUrl, sessionId, directory })
const setIsEditingMessage = useUIState((state) => state.setIsEditingMessage)
const sessionAgent = useSessionAgent(opcodeUrl, 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 (
<div className="flex flex-col gap-2 w-full">
<textarea
ref={textareaRef}
value={editedContent}
onChange={(e) => setEditedContent(e.target.value)}
onKeyDown={handleKeyDown}
onFocus={() => setIsEditingMessage(true)}
onBlur={() => setIsEditingMessage(false)}
className="w-full p-3 rounded-lg bg-background border border-primary/50 focus:border-primary focus:ring-1 focus:ring-primary outline-none resize-none min-h-[60px] text-[16px] md:text-sm"
placeholder="Edit your message..."
disabled={refreshMessage.isPending}
/>
<div className="flex items-center justify-between">
<span className="text-xs text-muted-foreground">
Press <kbd className="px-1 py-0.5 rounded bg-muted text-xs">Cmd+Enter</kbd> to send, <kbd className="px-1 py-0.5 rounded bg-muted text-xs">Esc</kbd> to cancel
</span>
<div className={`flex items-center ${isMobile ? 'gap-3' : 'gap-2'}`}>
<button
onClick={onCancel}
disabled={refreshMessage.isPending}
className="flex items-center gap-1 px-3 py-1.5 rounded text-sm border border-muted-foreground/40 bg-secondary text-secondary-foreground hover:bg-secondary/80 hover:border-muted-foreground/60 transition-colors disabled:opacity-50"
>
<X className="w-4 h-4" />
Cancel
</button>
<button
onClick={handleSubmit}
disabled={!editedContent.trim() || refreshMessage.isPending}
className="flex items-center gap-1 px-3 py-1.5 rounded text-sm bg-primary text-primary-foreground hover:bg-primary/90 transition-colors disabled:opacity-50"
>
{refreshMessage.isPending ? (
<Loader2 className="w-4 h-4 animate-spin" />
) : (
<Send className="w-4 h-4" />
)}
Resend
</button>
</div>
</div>
</div>
)
})
interface ClickableUserMessageProps {
content: string
onClick: () => void
isEditable: boolean
}
export const ClickableUserMessage = memo(function ClickableUserMessage({
content,
onClick,
isEditable
}: ClickableUserMessageProps) {
const isMobile = useMobile()
if (!isEditable) {
return (
<div className="text-sm whitespace-pre-wrap break-words">
{content}
</div>
)
}
return (
<button
onClick={onClick}
className="group/edit -m-1 flex w-full items-start gap-2 rounded p-1 text-left text-sm whitespace-pre-wrap break-words transition-colors hover:bg-primary/10"
title="Click to edit and resend"
>
<span className="flex-1">{content}</span>
<Pencil className={`w-4 h-4 flex-shrink-0 mt-0.5 text-muted-foreground transition-all ${isMobile ? 'opacity-100' : 'opacity-50 group-hover/edit:opacity-100 group-hover/edit:text-primary'}`} />
</button>
)
})