An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Refactor(share): Remove dedicated share page and update share functionality
Yoshiki Miura53a9820c637c
diff --git a/app/share/[id]/page.tsx b/app/share/[id]/page.tsxdeleted file mode 100644index d9fbc624..00000000--- a/app/share/[id]/page.tsx+++ /dev/null@@ -1,40 +0,0 @@-import { Chat } from '@/components/chat'-import { getSharedChat } from '@/lib/actions/chat'-import { getModels } from '@/lib/config/models'-import { convertToUIMessages } from '@/lib/utils'-import { notFound } from 'next/navigation'--export async function generateMetadata(props: {- params: Promise<{ id: string }>-}) {- const { id } = await props.params- const chat = await getSharedChat(id)-- if (!chat || !chat.sharePath) {- return notFound()- }-- return {- title: chat?.title.toString().slice(0, 50) || 'Search'- }-}--export default async function SharePage(props: {- params: Promise<{ id: string }>-}) {- const { id } = await props.params- const chat = await getSharedChat(id)-- if (!chat || !chat.sharePath) {- return notFound()- }-- const models = await getModels()- return (- <Chat- id={chat.id}- savedMessages={convertToUIMessages(chat.messages)}- models={models}- />- )-}diff --git a/app/share/loading.tsx b/app/share/loading.tsxdeleted file mode 100644index 768aa888..00000000--- a/app/share/loading.tsx+++ /dev/null@@ -1,13 +0,0 @@-'use client'--import { DefaultSkeleton } from '../../components/default-skeleton'--export default function Loading() {- return (- <div className="flex flex-col flex-1 items-center justify-center">- <div className="w-full max-w-3xl px-4">- <DefaultSkeleton />- </div>- </div>- )-}diff --git a/components/answer-section.tsx b/components/answer-section.tsxindex 9b90ae97..01fef859 100644--- a/components/answer-section.tsx+++ b/components/answer-section.tsx@@ -28,7 +28,7 @@ export function AnswerSection({messageId,reload}: AnswerSectionProps) {- const enableShare = process.env.NEXT_PUBLIC_ENABLE_SHARE === 'true'+ const enableShare = process.env.NEXT_PUBLIC_SUPABASE_URL !== undefinedconst handleReload = () => {if (reload) {diff --git a/components/chat-share.tsx b/components/chat-share.tsxindex 104f1520..abd2241b 100644--- a/components/chat-share.tsx+++ b/components/chat-share.tsx@@ -1,6 +1,6 @@'use client'-import { shareChat } from '@/lib/actions/chat'+import { shareChat } from '@/lib/actions/chat-db'import { useCopyToClipboard } from '@/lib/hooks/use-copy-to-clipboard'import { cn } from '@/lib/utils'import { Share } from 'lucide-react'@@ -33,18 +33,19 @@ export function ChatShare({ chatId, className }: ChatShareProps) {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')+ 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(result.sharePath, window.location.href)+ const url = new URL(+ `/search/${sharedChatObject.id}`,+ window.location.origin+ )setShareUrl(url.toString())}@@ -78,9 +79,10 @@ export function ChatShare({ chatId, className }: ChatShareProps) {</DialogTrigger><DialogContent><DialogHeader>- <DialogTitle>Share link to search result</DialogTitle>+ <DialogTitle>Share Chat</DialogTitle><DialogDescription>- Anyone with the link will be able to view this search result.+ Anyone with the link will be able to view this chat if it's+ public.</DialogDescription></DialogHeader><DialogFooter className="items-center">diff --git a/lib/actions/chat-db.ts b/lib/actions/chat-db.tsindex b782acb2..3cf1215f 100644--- a/lib/actions/chat-db.ts+++ b/lib/actions/chat-db.ts@@ -267,26 +267,27 @@ export async function saveChat(}}-// Get a shared chat-export async function getSharedChat(id: string) {- const chat = await chatDb.getSharedChat(id)+// Share a chat (makes it public if authorized)+export async function shareChat(id: string): Promise<DBChat | null> {+ const userId = await getCurrentUserId() // Get user ID on the server- if (!chat) {+ if (!userId) {+ // Or throw new Error('Authentication required to share a chat.');+ // Depending on how you want to handle unauthorized attempts at the action level.+ console.error('shareChat: User not authenticated')return null}- // Get messages for the chat- const messages = await chatDb.getChatMessages(id)+ // We assume chatDb.shareChat updates the chat in the DB (e.g., sets visibility to public)+ // and handles ownership check (i.e., if this userId can share this chat id).+ // It should return the updated chat object (DBChat), or null on failure/permission denied.+ const updatedChat = await chatDb.shareChat(id, userId)- // Return chat with messages- return {- ...chat,- messages,- sharePath: `/share/${id}`+ if (!updatedChat) {+ // The operation to share the chat in the database failed or was not permitted+ return null}-}-// Share a chat-export async function shareChat(id: string, userId: string) {- return chatDb.shareChat(id, userId)+ // Return the updated chat object. The concept of a specific sharePath is obsolete.+ return updatedChat}