YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
Improve chat user message edit ux (#3448)
Harsh Kumarac1f9a5d7e0f
diff --git a/apps/screenpipe-app-tauri/components/standalone-chat.tsx b/apps/screenpipe-app-tauri/components/standalone-chat.tsxindex 4b77c30aa..b09ff8b1e 100644--- a/apps/screenpipe-app-tauri/components/standalone-chat.tsx+++ b/apps/screenpipe-app-tauri/components/standalone-chat.tsx@@ -2785,6 +2785,23 @@ export function StandaloneChat({// select (let the browser select text — don't swallow it).const pendingEditDownXYRef = useRef<{ x: number; y: number } | null>(null);+ const enterEditMode = (message: Message, caretPos?: number) => {+ setEditDraft(message.content);+ pendingCaretRef.current = caretPos ?? message.content.length;+ setEditingMessageId(message.id);+ };++ const commitEditedMessage = (message: Message, draft: string) => {+ const trimmed = draft.trim();+ setEditingMessageId(null);+ pendingCaretRef.current = null;+ if (!trimmed || trimmed === message.content) return;+ const idx = messages.findIndex((m) => m.id === message.id);+ if (idx === -1) return;+ setMessages((prev) => prev.slice(0, idx));+ sendMessage(trimmed, message.displayContent);+ };+// Given a click on a rendered message bubble, compute the character offset// into `content` that corresponds to where the user clicked. Falls back to// end-of-text if the browser can't resolve a caret position (old Safari).@@ -7111,7 +7128,7 @@ export function StandaloneChat({className={cn("group/message flex flex-col min-w-0",message.role === "user"- ? "items-end max-w-[82%]"+ ? (editingMessageId === message.id ? "items-end w-full" : "items-end max-w-[82%]"): "items-start w-full")}>@@ -7150,53 +7167,82 @@ export function StandaloneChat({return;}// Real click — enter edit mode.- setEditDraft(message.content);- setEditingMessageId(message.id);+ enterEditMode(message, pendingCaretRef.current ?? undefined);}}className={cn("relative rounded-xl text-sm overflow-hidden max-w-full transition-all",message.role === "user"? "bg-muted/60 text-foreground px-4 py-3": "bg-background text-foreground py-1",- message.role === "user" && !isLoading && editingMessageId !== message.id && "cursor-text"+ message.role === "user" && !isLoading && editingMessageId !== message.id && "cursor-text",+ // In edit mode, keep the bubble at full available width so it+ // doesn't shrink or look like a separate small input.+ editingMessageId === message.id && message.role === "user" && "w-full")}>{editingMessageId === message.id ? (- <textarea- ref={(el) => {- editTextareaRef.current = el;- // Synchronous focus + caret placement BEFORE the browser- // paints. Using the ref callback (instead of useEffect)- // guarantees the cursor lands where the user clicked on- // the very first frame — no flash-of-start-of-text.- if (el && pendingCaretRef.current != null) {- const pos = pendingCaretRef.current;- pendingCaretRef.current = null;- el.focus({ preventScroll: true });- try { el.setSelectionRange(pos, pos); } catch { /* ignore */ }- }- }}- value={editDraft}- onChange={(e) => setEditDraft(e.target.value)}- onBlur={() => {- const trimmed = editDraft.trim();- setEditingMessageId(null);- if (!trimmed || trimmed === message.content) return;- const idx = messages.findIndex((m) => m.id === message.id);- if (idx === -1) return;- setMessages((prev) => prev.slice(0, idx));- sendMessage(trimmed, message.displayContent);- }}- onKeyDown={(e) => {- if (e.key === "Escape") { e.preventDefault(); setEditingMessageId(null); }- if (e.key === "Enter" && !e.shiftKey) {- e.preventDefault();- (e.currentTarget as HTMLTextAreaElement).blur();- }- }}- rows={Math.min(8, Math.max(1, editDraft.split("\n").length))}- className="w-full resize-none bg-transparent text-foreground placeholder:text-muted-foreground focus:outline-none"- />+ <div className="flex flex-col gap-2 w-full min-w-0">+ <textarea+ ref={(el) => {+ editTextareaRef.current = el;+ // Synchronous focus + caret placement BEFORE the browser+ // paints. Using the ref callback (instead of useEffect)+ // guarantees the cursor lands where the user clicked on+ // the very first frame — no flash-of-start-of-text.+ if (el && pendingCaretRef.current != null) {+ const pos = pendingCaretRef.current;+ pendingCaretRef.current = null;+ el.focus({ preventScroll: true });+ try { el.setSelectionRange(pos, pos); } catch { /* ignore */ }+ }+ }}+ value={editDraft}+ onChange={(e) => setEditDraft(e.target.value)}+ onBlur={() => commitEditedMessage(message, editDraft)}+ onKeyDown={(e) => {+ if (e.key === "Escape") {+ e.preventDefault();+ setEditingMessageId(null);+ pendingCaretRef.current = null;+ setEditDraft(message.content);+ }+ if (e.key === "Enter" && !e.shiftKey) {+ e.preventDefault();+ (e.currentTarget as HTMLTextAreaElement).blur();+ }+ }}+ rows={Math.min(10, Math.max(1, editDraft.split("\n").length))}+ className="block w-full min-w-0 resize-none bg-transparent text-foreground placeholder:text-muted-foreground focus:outline-none leading-relaxed"+ />+ <div className="flex items-center justify-end gap-2">+ <button+ type="button"+ className="text-xs px-2 py-1 rounded-md text-muted-foreground hover:text-foreground hover:bg-muted/50 transition-colors"+ onMouseDown={(e) => e.preventDefault()}+ onMouseUp={(e) => e.stopPropagation()}+ onClick={(e) => {+ e.stopPropagation();+ setEditingMessageId(null);+ pendingCaretRef.current = null;+ setEditDraft(message.content);+ }}+ >+ Cancel+ </button>+ <button+ type="button"+ className="text-xs px-2 py-1 rounded-md bg-foreground text-background hover:bg-foreground/90 transition-colors"+ onMouseDown={(e) => e.preventDefault()}+ onMouseUp={(e) => e.stopPropagation()}+ onClick={(e) => {+ e.stopPropagation();+ commitEditedMessage(message, editDraft);+ }}+ >+ Save+ </button>+ </div>+ </div>) : (<MessageContentmessage={message}@@ -7207,23 +7253,38 @@ export function StandaloneChat({)}</div>{/* Action buttons - appear on hover, outside the message box */}- <div className="flex items-center gap-0.5 self-end mt-1 opacity-0 group-hover/message:opacity-100 transition-all duration-200">- <button- onClick={async () => {- await navigator.clipboard.writeText(message.content);- setCopiedMessageId(message.id);- setTimeout(() => setCopiedMessageId(null), 2000);- }}- className="p-1 rounded-md hover:bg-muted text-muted-foreground hover:text-foreground"- title="Copy message"- >- {copiedMessageId === message.id ? (- <Check className="h-3 w-3" />- ) : (- <Copy className="h-3 w-3" />+ {editingMessageId !== message.id && (+ <div className="flex items-center gap-0.5 self-end mt-1 opacity-0 group-hover/message:opacity-100 group-focus-within/message:opacity-100 transition-all duration-200">+ <button+ onClick={async () => {+ await navigator.clipboard.writeText(message.content);+ setCopiedMessageId(message.id);+ setTimeout(() => setCopiedMessageId(null), 2000);+ }}+ className="p-1 rounded-md hover:bg-muted text-muted-foreground hover:text-foreground"+ title="Copy message"+ >+ {copiedMessageId === message.id ? (+ <Check className="h-3 w-3" />+ ) : (+ <Copy className="h-3 w-3" />+ )}+ </button>+ {message.role === "user" && !isLoading && (+ <button+ type="button"+ onMouseUp={(e) => e.stopPropagation()}+ onClick={(e) => {+ e.stopPropagation();+ enterEditMode(message);+ }}+ className="p-1 rounded-md hover:bg-muted text-muted-foreground hover:text-foreground"+ title="Edit"+ >+ <Pencil className="h-3 w-3" />+ </button>)}- </button>- {message.role === "assistant" && !isLoading && (+ {message.role === "assistant" && !isLoading && (<buttononClick={() => {const msgIndex = messages.findIndex((m) => m.id === message.id);@@ -7243,8 +7304,8 @@ export function StandaloneChat({><RefreshCw className="h-3 w-3" /></button>- )}- {message.role === "assistant" && (+ )}+ {message.role === "assistant" && (<Popoveropen={openMessageMenuId === message.id}onOpenChange={(open) => setOpenMessageMenuId(open ? message.id : null)}@@ -7297,8 +7358,9 @@ export function StandaloneChat({</button></PopoverContent></Popover>- )}- </div>+ )}+ </div>+ )}</div></motion.div>);