'use client' import { useState, useTransition } from 'react' import { IconShare as Share } from '@tabler/icons-react' import { toast } from 'sonner' import { shareChat } from '@/lib/actions/chat' import { useCopyToClipboard } from '@/lib/hooks/use-copy-to-clipboard' import { cn } from '@/lib/utils' import { Button } from './ui/button' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from './ui/dialog' import { Spinner } from './ui/spinner' interface ChatShareProps { chatId: string className?: string } export function ChatShare({ chatId, className }: ChatShareProps) { const [open, setOpen] = useState(false) const [pending, startTransition] = useTransition() const { copyToClipboard } = useCopyToClipboard({ timeout: 1000 }) const [shareUrl, setShareUrl] = useState('') const handleShare = async () => { startTransition(() => { setOpen(true) }) const sharedChatObject = await shareChat(chatId) if (!sharedChatObject) { toast.error( 'Failed to make chat public. You may need to be logged in or own the chat.' ) return } const url = new URL( `/search/${sharedChatObject.id}`, window.location.origin ) setShareUrl(url.toString()) } const handleCopy = () => { if (shareUrl) { copyToClipboard(shareUrl) toast.success('Link copied to clipboard') setOpen(false) } else { toast.error('No link to copy') } } return (