Skip to content

An AI-powered search engine with a generative UI

Fix handle sharing and copying chat links

Yoshiki Miura62378de2951f
diff --git a/components/chat-share.tsx b/components/chat-share.tsx
index 31214104..32d192cb 100644
--- a/components/chat-share.tsx
+++ b/components/chat-share.tsx
@@ -26,24 +26,35 @@ 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 handleClick = async () => {
- startTransition(async () => {
- const result = await shareChat(chatId)
- if (!result) {
- toast.error('Failed to share chat')
- }
+ const handleShare = async () => {
+ startTransition(() => {
+ setOpen(true)
+ })
+ const result = await shareChat(chatId)
+ if (!result) {
+ toast.error('Failed to share chat')
+ return
+ }
+
+ if (!result.sharePath) {
+ toast.error('Could not copy link to clipboard')
+ return
+ }
- if (!result?.sharePath) {
- toast.error('Could not copy link to clipboard')
- return
- }
+ const url = new URL(result.sharePath, window.location.href)
+ setShareUrl(url.toString())
+ }
- const url = new URL(result.sharePath, window.location.origin)
- copyToClipboard(url.toString())
+ const handleCopy = () => {
+ if (shareUrl) {
+ copyToClipboard(shareUrl)
toast.success('Link copied to clipboard')
setOpen(false)
- })
+ } else {
+ toast.error('No link to copy')
+ }
}
return (
@@ -72,9 +83,16 @@ export function ChatShare({ chatId, className }: ChatShareProps) {
</DialogDescription>
</DialogHeader>
<DialogFooter className="items-center">
- <Button onClick={handleClick} disabled={pending} size="sm">
- {pending ? <Spinner /> : 'Copy link'}
- </Button>
+ {!shareUrl && (
+ <Button onClick={handleShare} disabled={pending} size="sm">
+ {pending ? <Spinner /> : 'Get link'}
+ </Button>
+ )}
+ {shareUrl && (
+ <Button onClick={handleCopy} disabled={pending} size="sm">
+ {'Copy link'}
+ </Button>
+ )}
</DialogFooter>
</DialogContent>
</Dialog>