import { zodResolver } from '@hookform/resolvers/zod'
import { Pencil, ThumbsDown, ThumbsUp, Trash2 } from 'lucide-react'
import { useEffect, useState, type PropsWithChildren } from 'react'
import { useForm } from 'react-hook-form'
import {
Button,
cn,
Form,
FormControl,
FormField,
Popover,
PopoverContent,
PopoverTrigger,
TextArea,
} from 'ui'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import * as z from 'zod'
import { ButtonTooltip } from '../ButtonTooltip'
export function MessageActions({
children,
alwaysShow = false,
}: PropsWithChildren<{ alwaysShow?: boolean }>) {
return (
)
}
function MessageActionsEdit({ onClick, tooltip }: { onClick: () => void; tooltip: string }) {
return (
}
onClick={onClick}
className="text-foreground-light hover:text-foreground p-1 rounded-sm"
aria-label={tooltip}
tooltip={{
content: {
side: 'bottom',
text: tooltip,
},
}}
/>
)
}
MessageActions.Edit = MessageActionsEdit
function MessageActionsDelete({ onClick }: { onClick: () => void }) {
return (
}
tooltip={{ content: { side: 'bottom', text: 'Delete message' } }}
onClick={onClick}
className="text-foreground-light hover:text-foreground p-1 rounded-sm"
title="Delete message"
aria-label="Delete message"
/>
)
}
MessageActions.Delete = MessageActionsDelete
function MessageActionsThumbsUp({
onClick,
isActive,
disabled,
}: {
onClick: () => void
isActive?: boolean
disabled?: boolean
}) {
return (
}
onClick={onClick}
className={cn(
'p-1 rounded-sm transition-colors',
disabled && 'opacity-50 pointer-events-none'
)}
title="Good response"
aria-label="Good response"
/>
)
}
MessageActions.ThumbsUp = MessageActionsThumbsUp
const feedbackSchema = z.object({
reason: z.string().optional(),
})
type FeedbackFormValues = z.infer
function MessageActionsThumbsDown({
onClick,
isActive,
disabled,
}: {
onClick: (reason?: string) => void
isActive?: boolean
disabled?: boolean
}) {
const [open, setOpen] = useState(false)
const form = useForm({
resolver: zodResolver(feedbackSchema as any),
defaultValues: { reason: '' },
mode: 'onSubmit',
})
const handleOpenChange = (newOpen: boolean) => {
if (disabled) return
// When popover closes, submit the rating if not already submitted
if (!newOpen && open && !form.formState.isSubmitSuccessful) {
onClick()
}
setOpen(newOpen)
if (!newOpen) {
form.reset()
}
}
const onSubmit = (values: FeedbackFormValues) => {
onClick(values.reason || undefined)
}
// Auto-close popover after showing thank you message
useEffect(() => {
if (form.formState.isSubmitSuccessful) {
const timer = setTimeout(() => {
setOpen(false)
}, 2000)
return () => clearTimeout(timer)
}
}, [form.formState.isSubmitSuccessful])
return (
{form.formState.isSubmitSuccessful ? (
We appreciate your feedback!
) : (
)}
)
}
MessageActions.ThumbsDown = MessageActionsThumbsDown