-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: make text selection highlight visible in chat bubbles #697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { memo, useEffect, useState } from "react"; | ||
| import { CircleDashed, ChevronRight, ChevronDown, X } from "lucide-react"; | ||
| import { memo, useEffect, useState, useRef, useCallback } from "react"; | ||
| import { CircleDashed, ChevronRight, ChevronDown, X, Pencil } from "lucide-react"; | ||
| import { useI18n } from "../../components/useI18n"; | ||
| import type { Attachment } from "../../../../shared/attachments"; | ||
|
|
||
|
|
@@ -11,23 +11,55 @@ | |
| interface QueuedMessagesProps { | ||
| messages: QueuedMessage[]; | ||
| onRemove: (index: number) => void; | ||
| onEdit: (index: number, newText: string) => void; | ||
| } | ||
|
|
||
| /** | ||
| * Pending-send queue indicator shown above the input while the agent is busy. | ||
| * Each queued message can be individually cancelled via an X button. | ||
| * Each queued message can be edited via a pencil button or cancelled via an X button. | ||
| */ | ||
| export const QueuedMessages = memo(function QueuedMessages({ | ||
| messages, | ||
| onRemove, | ||
| onEdit, | ||
| }: QueuedMessagesProps): React.JSX.Element | null { | ||
| const { t } = useI18n(); | ||
| const [expanded, setExpanded] = useState(false); | ||
| const [editingIndex, setEditingIndex] = useState<number | null>(null); | ||
| const [editText, setEditText] = useState(""); | ||
| const editInputRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| useEffect(() => { | ||
| if (messages.length === 0) setExpanded(false); | ||
| }, [messages.length]); | ||
|
|
||
| // Focus the edit input when entering edit mode | ||
| useEffect(() => { | ||
| if (editingIndex !== null && editInputRef.current) { | ||
| editInputRef.current.focus(); | ||
| editInputRef.current.select(); | ||
| } | ||
| }, [editingIndex]); | ||
|
|
||
| const startEdit = useCallback((index: number, text: string) => { | ||
| setEditingIndex(index); | ||
| setEditText(text); | ||
| }, []); | ||
|
|
||
| const commitEdit = useCallback((index: number) => { | ||
| const trimmed = editText.trim(); | ||
| if (trimmed && trimmed !== messages[index].text) { | ||
| onEdit(index, trimmed); | ||
| } | ||
| setEditingIndex(null); | ||
| setEditText(""); | ||
| }, [editText, messages, onEdit]); | ||
|
|
||
| const cancelEdit = useCallback(() => { | ||
| setEditingIndex(null); | ||
| setEditText(""); | ||
| }, []); | ||
|
|
||
| if (messages.length === 0) return null; | ||
|
|
||
| const preview = (m: QueuedMessage): string => { | ||
|
|
@@ -36,13 +68,42 @@ | |
| return t("chat.queuedAttachment", { count: m.attachments.length }); | ||
| }; | ||
|
|
||
| const renderEditInput = (index: number) => ( | ||
| <input | ||
| ref={editInputRef} | ||
| type="text" | ||
| className="chat-queue-edit-input" | ||
| value={editText} | ||
| onChange={(e) => setEditText(e.target.value)} | ||
| onKeyDown={(e) => { | ||
| if (e.key === "Enter") commitEdit(index); | ||
| if (e.key === "Escape") cancelEdit(); | ||
| }} | ||
| onBlur={() => commitEdit(index)} | ||
| /> | ||
| ); | ||
|
|
||
| if (messages.length === 1) { | ||
| const isEditing = editingIndex === 0; | ||
| return ( | ||
| <div className="chat-queue-indicator"> | ||
| <CircleDashed size={14} className="chat-queue-icon" /> | ||
| <span className="chat-queue-single" title={preview(messages[0])}> | ||
| {preview(messages[0])} | ||
| </span> | ||
| {isEditing ? ( | ||
| renderEditInput(0) | ||
| ) : ( | ||
| <span className="chat-queue-single" title={preview(messages[0])}> | ||
| {preview(messages[0])} | ||
| </span> | ||
| )} | ||
| <button | ||
| type="button" | ||
| className="chat-queue-edit" | ||
| onClick={() => startEdit(0, messages[0].text)} | ||
| aria-label={t("chat.queuedEdit")} | ||
| title={t("chat.queuedEdit")} | ||
| > | ||
| <Pencil size={12} /> | ||
| </button> | ||
|
Comment on lines
+98
to
+106
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When |
||
| <button | ||
| type="button" | ||
| className="chat-queue-remove" | ||
|
|
@@ -70,23 +131,39 @@ | |
| </button> | ||
| {expanded && ( | ||
| <ul className="chat-queue-list"> | ||
| {messages.map((m, i) => ( | ||
| <li | ||
| key={`${i}-${m.text.length}-${m.attachments.length}`} | ||
| className="chat-queue-item" | ||
| title={preview(m)} | ||
| > | ||
| <span className="chat-queue-item-text">{preview(m)}</span> | ||
| <button | ||
| type="button" | ||
| className="chat-queue-remove" | ||
| onClick={() => onRemove(i)} | ||
| aria-label={t("chat.queuedCancel")} | ||
| {messages.map((m, i) => { | ||
| const isEditing = editingIndex === i; | ||
| return ( | ||
| <li | ||
| key={`${i}-${m.text.length}-${m.attachments.length}`} | ||
| className="chat-queue-item" | ||
| title={isEditing ? undefined : preview(m)} | ||
| > | ||
| <X size={12} /> | ||
| </button> | ||
| </li> | ||
| ))} | ||
| {isEditing ? ( | ||
| renderEditInput(i) | ||
| ) : ( | ||
| <span className="chat-queue-item-text">{preview(m)}</span> | ||
| )} | ||
| <button | ||
| type="button" | ||
| className="chat-queue-edit" | ||
| onClick={() => startEdit(i, m.text)} | ||
| aria-label={t("chat.queuedEdit")} | ||
| title={t("chat.queuedEdit")} | ||
| > | ||
| <Pencil size={12} /> | ||
| </button> | ||
| <button | ||
| type="button" | ||
| className="chat-queue-remove" | ||
| onClick={() => onRemove(i)} | ||
| aria-label={t("chat.queuedCancel")} | ||
| > | ||
| <X size={12} /> | ||
| </button> | ||
| </li> | ||
| ); | ||
| })} | ||
| </ul> | ||
| )} | ||
| </div> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
onBlurimmediately re-commitsWhen a user presses Escape,
cancelEdit()queues its state updates (setEditingIndex(null),setEditText("")) but React batches those updates — they haven't taken effect yet. The input then loses focus (unmounts on re-render), firingonBlur→commitEdit(index). At that pointeditTextstill holds the modified value, so theif (trimmed && trimmed !== messages[index].text)check passes andonEditis called, silently saving changes the user explicitly rejected.A common fix is a ref flag set synchronously before any state update so
commitEditcan bail out early: