2023-06-02 15:33:48 -04:00
|
|
|
import { Sidebar } from '@/app/sidebar'
|
2023-05-19 12:33:56 -04:00
|
|
|
|
2023-06-02 15:33:48 -04:00
|
|
|
import { auth } from '@/auth'
|
|
|
|
|
import { type Metadata } from 'next'
|
2023-06-02 15:15:35 -04:00
|
|
|
|
2023-06-02 15:33:48 -04:00
|
|
|
import { Chat } from '@/app/chat'
|
|
|
|
|
import { type Chat as ChatType } from '@/lib/types'
|
|
|
|
|
import { kv } from '@vercel/kv'
|
|
|
|
|
import { Message } from 'ai-connector'
|
2023-06-02 13:29:54 -04:00
|
|
|
|
2023-06-02 15:33:48 -04:00
|
|
|
export const runtime = 'edge'
|
|
|
|
|
export const preferredRegion = 'home'
|
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-02 15:33:48 -04:00
|
|
|
const session = await auth()
|
|
|
|
|
const chat = await getChat(params.id, session?.user?.email ?? '')
|
2023-05-19 12:33:56 -04:00
|
|
|
return {
|
2023-06-02 15:33:48 -04:00
|
|
|
title: chat?.title.slice(0, 50) ?? 'Chat'
|
|
|
|
|
}
|
2023-05-19 12:33:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default async function ChatPage({ params }: ChatPageProps) {
|
2023-06-02 15:33:48 -04:00
|
|
|
const session = await auth()
|
|
|
|
|
const chat = await getChat(params.id, session?.user?.email ?? '')
|
2023-05-19 12:33:56 -04:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="relative flex h-full w-full overflow-hidden">
|
|
|
|
|
<Sidebar session={session} />
|
|
|
|
|
<div className="flex h-full min-w-0 flex-1 flex-col">
|
2023-06-02 11:57:44 -04:00
|
|
|
<Chat id={chat.id} initialMessages={chat.messages as Message[]} />
|
2023-05-19 12:33:56 -04:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2023-06-02 15:33:48 -04:00
|
|
|
)
|
2023-05-19 12:33:56 -04:00
|
|
|
}
|
|
|
|
|
|
2023-06-02 15:33:48 -04:00
|
|
|
ChatPage.displayName = 'ChatPage'
|
2023-06-02 15:15:35 -04:00
|
|
|
|
|
|
|
|
async function getChat(id: string, userId: string) {
|
2023-06-02 15:33:48 -04:00
|
|
|
const chat = await kv.hgetall<ChatType>(`chat:${id}`)
|
2023-06-02 15:15:35 -04:00
|
|
|
if (!chat) {
|
2023-06-02 15:33:48 -04:00
|
|
|
throw new Error('Not found')
|
2023-06-02 15:15:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (userId && chat.userId !== userId) {
|
2023-06-02 15:33:48 -04:00
|
|
|
throw new Error('Unauthorized')
|
2023-06-02 15:15:35 -04:00
|
|
|
}
|
|
|
|
|
|
2023-06-02 15:33:48 -04:00
|
|
|
return chat
|
2023-06-02 15:15:35 -04:00
|
|
|
}
|