chatbot-template/app/(chat)/chat/[id]/page.tsx

66 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-06-02 15:33:48 -04:00
import { type Metadata } from 'next'
2023-06-16 21:52:01 +04:00
import { notFound, redirect } from 'next/navigation'
2023-06-16 21:52:01 +04:00
import { auth } from '@/auth'
2024-03-14 20:00:52 +03:00
import { getChat, getMissingKeys } from '@/app/actions'
2023-06-16 21:52:01 +04:00
import { Chat } from '@/components/chat'
2024-03-14 20:00:52 +03:00
import { AI } from '@/lib/chat/actions'
import { Session } from '@/lib/types'
2023-06-02 13:29:54 -04:00
2023-05-19 12:33:56 -04:00
export interface ChatPageProps {
params: {
2023-06-02 15:33:48 -04:00
id: string
}
2023-05-19 12:33:56 -04:00
}
export async function generateMetadata({
2023-06-02 15:33:48 -04:00
params
2023-05-19 12:33:56 -04:00
}: ChatPageProps): Promise<Metadata> {
2023-06-16 21:52:01 +04:00
const session = await auth()
if (!session?.user) {
return {}
}
const chat = await getChat(params.id, session.user.id)
if (!chat || 'error' in chat) {
redirect('/')
} else {
return {
title: chat?.title.toString().slice(0, 50) ?? 'Chat'
}
2023-06-02 15:33:48 -04:00
}
2023-05-19 12:33:56 -04:00
}
export default async function ChatPage({ params }: ChatPageProps) {
2024-03-14 20:00:52 +03:00
const session = (await auth()) as Session
const missingKeys = await getMissingKeys()
2023-06-16 21:52:01 +04:00
if (!session?.user) {
2024-03-14 20:00:52 +03:00
redirect(`/login?next=/chat/${params.id}`)
2023-06-16 21:52:01 +04:00
}
2024-03-14 20:00:52 +03:00
const userId = session.user.id as string
const chat = await getChat(params.id, userId)
2023-06-16 21:52:01 +04:00
if (!chat || 'error' in chat) {
2024-03-14 20:00:52 +03:00
redirect('/')
} else {
if (chat?.userId !== session?.user?.id) {
notFound()
}
2023-05-19 12:33:56 -04:00
return (
<AI initialAIState={{ chatId: chat.id, messages: chat.messages }}>
<Chat
id={chat.id}
session={session}
initialMessages={chat.messages}
missingKeys={missingKeys}
/>
</AI>
)
2023-06-16 15:28:43 -04:00
}
2023-06-02 15:15:35 -04:00
}