An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: add copy button to user message hover actions (#815)
Yoshiki Miuraece7b643d7cd
diff --git a/components/user-text-section.tsx b/components/user-text-section.tsxindex c1458c2d..a8b1d8c1 100644--- a/components/user-text-section.tsx+++ b/components/user-text-section.tsx@@ -3,7 +3,7 @@import React, { useEffect, useRef, useState } from 'react'import TextareaAutosize from 'react-textarea-autosize'-import { Pencil } from 'lucide-react'+import { Check, Copy, Pencil } from 'lucide-react'import { cn } from '@/lib/utils'@@ -25,6 +25,7 @@ export const UserTextSection: React.FC<UserTextSectionProps> = ({const [editedContent, setEditedContent] = useState(content)const [isComposing, setIsComposing] = useState(false)const [enterDisabled, setEnterDisabled] = useState(false)+ const [copied, setCopied] = useState(false)const enterResetTimeoutRef = useRef<NodeJS.Timeout | null>(null)useEffect(() => {@@ -35,6 +36,17 @@ export const UserTextSection: React.FC<UserTextSectionProps> = ({}}, [])+ const handleCopyClick = async (e: React.MouseEvent<HTMLButtonElement>) => {+ e.stopPropagation()+ try {+ await navigator.clipboard.writeText(content)+ setCopied(true)+ setTimeout(() => setCopied(false), 2000)+ } catch {+ // Clipboard access denied — silently ignore+ }+ }+const handleEditClick = (e: React.MouseEvent<HTMLButtonElement>) => {e.stopPropagation()setEditedContent(content)@@ -118,13 +130,12 @@ export const UserTextSection: React.FC<UserTextSectionProps> = ({</div>) : (<div className="relative">- <div className="pr-10 whitespace-pre-wrap">{content}</div>+ <div className="whitespace-pre-wrap">{content}</div><divclassName={cn(- 'absolute top-0 right-0 transition-opacity',+ 'absolute -top-1 -right-1 flex items-center gap-0.5 p-0.5 transition-opacity bg-background rounded-full shadow-sm border','opacity-0',- 'group-focus-within:opacity-100',- 'md:opacity-0',+ 'max-md:group-focus-within:opacity-100','md:group-hover:opacity-100')}>@@ -132,6 +143,20 @@ export const UserTextSection: React.FC<UserTextSectionProps> = ({variant="ghost"size="icon"className="rounded-full size-7"+ onMouseDown={e => e.preventDefault()}+ onClick={handleCopyClick}+ >+ {copied ? (+ <Check className="size-3.5" />+ ) : (+ <Copy className="size-3.5" />+ )}+ </Button>+ <Button+ variant="ghost"+ size="icon"+ className="rounded-full size-7"+ onMouseDown={e => e.preventDefault()}onClick={handleEditClick}><Pencil className="size-3.5" />