2023-09-29 22:51:54 +00:00
|
|
|
'use client'
|
|
|
|
|
|
|
|
|
|
import { type Message } from 'ai'
|
|
|
|
|
|
2023-12-08 04:40:45 +00:00
|
|
|
import { MessageActionType } from '@/lib/types'
|
|
|
|
|
import { cn } from '@/lib/utils'
|
2023-09-29 22:51:54 +00:00
|
|
|
import { Button } from '@/components/ui/button'
|
2023-11-23 00:58:21 +00:00
|
|
|
import { IconEdit, IconRefresh, IconTrash } from '@/components/ui/icons'
|
|
|
|
|
import { CopyButton } from '@/components/copy-button'
|
2023-09-29 22:51:54 +00:00
|
|
|
|
|
|
|
|
interface ChatMessageActionsProps extends React.ComponentProps<'div'> {
|
|
|
|
|
message: Message
|
2023-11-14 20:50:58 +00:00
|
|
|
handleMessageAction: (messageId: string, action: MessageActionType) => void
|
2023-09-29 22:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ChatMessageActions({
|
|
|
|
|
message,
|
|
|
|
|
className,
|
2023-11-14 20:50:58 +00:00
|
|
|
handleMessageAction,
|
2023-09-29 22:51:54 +00:00
|
|
|
...props
|
|
|
|
|
}: ChatMessageActionsProps) {
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
2023-11-14 20:50:58 +00:00
|
|
|
'flex items-center justify-end transition-opacity group-hover:opacity-100 md:absolute md:-right-[5rem] md:-top-2 md:opacity-0',
|
2023-09-29 22:51:54 +00:00
|
|
|
className
|
|
|
|
|
)}
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
2023-11-14 20:50:58 +00:00
|
|
|
{message.role === 'user' ? (
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={e => handleMessageAction(message.id, 'edit')}
|
|
|
|
|
>
|
|
|
|
|
<IconEdit />
|
|
|
|
|
<span className="sr-only">Edit message</span>
|
|
|
|
|
</Button>
|
|
|
|
|
) : (
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={e => handleMessageAction(message.id, 'regenerate')}
|
|
|
|
|
>
|
|
|
|
|
<IconRefresh />
|
|
|
|
|
<span className="sr-only">Regenerate message</span>
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={e => handleMessageAction(message.id, 'delete')}
|
|
|
|
|
>
|
|
|
|
|
<IconTrash />
|
|
|
|
|
<span className="sr-only">Delete message</span>
|
|
|
|
|
</Button>
|
2023-11-23 00:58:21 +00:00
|
|
|
<CopyButton value={message.content} />
|
2023-09-29 22:51:54 +00:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|