Skip to content

An AI-powered search engine with a generative UI

feat: add message copy button component

Yoshiki Miurac6cbdf721c98
diff --git a/components/answer-section.tsx b/components/answer-section.tsx
index 69998f7c..67588375 100644
--- a/components/answer-section.tsx
+++ b/components/answer-section.tsx
@@ -1,10 +1,10 @@
'use client'
import { Text } from 'lucide-react'
-import { ChatShare } from './chat-share'
import { CollapsibleMessage } from './collapsible-message'
import { DefaultSkeleton } from './default-skeleton'
import { BotMessage } from './message'
+import { MessageActions } from './message-actions'
export type AnswerSectionProps = {
content: string
@@ -30,9 +30,12 @@ export function AnswerSection({
const message = content ? (
<div className="flex flex-col gap-1">
<BotMessage message={content} />
- {enableShare && chatId && (
- <ChatShare chatId={chatId} className="self-end" />
- )}
+ <MessageActions
+ message={content}
+ chatId={chatId}
+ enableShare={enableShare}
+ className="self-end"
+ />
</div>
) : (
<DefaultSkeleton />
diff --git a/components/chat-share.tsx b/components/chat-share.tsx
index cfcc8abc..104f1520 100644
--- a/components/chat-share.tsx
+++ b/components/chat-share.tsx
@@ -1,10 +1,8 @@
'use client'
import { shareChat } from '@/lib/actions/chat'
-import { CHAT_ID } from '@/lib/constants'
import { useCopyToClipboard } from '@/lib/hooks/use-copy-to-clipboard'
import { cn } from '@/lib/utils'
-import { useChat } from 'ai/react'
import { Share } from 'lucide-react'
import { useState, useTransition } from 'react'
import { toast } from 'sonner'
@@ -30,9 +28,6 @@ export function ChatShare({ chatId, className }: ChatShareProps) {
const [pending, startTransition] = useTransition()
const { copyToClipboard } = useCopyToClipboard({ timeout: 1000 })
const [shareUrl, setShareUrl] = useState('')
- const { isLoading } = useChat({
- id: CHAT_ID
- })
const handleShare = async () => {
startTransition(() => {
@@ -73,11 +68,10 @@ export function ChatShare({ chatId, className }: ChatShareProps) {
>
<DialogTrigger asChild>
<Button
- className={cn('rounded-full', isLoading && 'invisible')}
+ className={cn('rounded-full')}
size="icon"
variant={'ghost'}
onClick={() => setOpen(true)}
- disabled={isLoading}
>
<Share size={14} />
</Button>
diff --git a/components/message-actions.tsx b/components/message-actions.tsx
new file mode 100644
index 00000000..d912f9ad
--- /dev/null
+++ b/components/message-actions.tsx
@@ -0,0 +1,49 @@
+'use client'
+
+import { CHAT_ID } from '@/lib/constants'
+import { cn } from '@/lib/utils'
+import { useChat } from 'ai/react'
+import { Copy } from 'lucide-react'
+import { toast } from 'sonner'
+import { ChatShare } from './chat-share'
+import { Button } from './ui/button'
+
+interface MessageActionsProps {
+ message: string
+ chatId?: string
+ enableShare?: boolean
+ className?: string
+}
+
+export function MessageActions({
+ message,
+ chatId,
+ enableShare,
+ className
+}: MessageActionsProps) {
+ const { isLoading } = useChat({
+ id: CHAT_ID
+ })
+ async function handleCopy() {
+ await navigator.clipboard.writeText(message)
+ toast.success('Message copied to clipboard')
+ }
+
+ if (isLoading) {
+ return <div className="size-10" />
+ }
+
+ return (
+ <div className={cn('flex items-center gap-0.5', className)}>
+ <Button
+ variant="ghost"
+ size="icon"
+ onClick={handleCopy}
+ className="rounded-full"
+ >
+ <Copy size={14} />
+ </Button>
+ {enableShare && chatId && <ChatShare chatId={chatId} />}
+ </div>
+ )
+}