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

50 lines
1.2 KiB
TypeScript
Raw Normal View History

import { CoreMessage } from 'ai';
import { cookies } from 'next/headers';
import { notFound } from 'next/navigation';
2023-05-19 12:33:56 -04:00
import { auth } from '@/app/(auth)/auth';
import { Chat as PreviewChat } from '@/components/custom/chat';
import { getChatById } from '@/db/queries';
import { Chat } from '@/db/schema';
import { DEFAULT_MODEL_NAME, models } from '@/lib/model';
import { convertToUIMessages } from '@/lib/utils';
2024-10-11 18:00:22 +05:30
2024-10-23 12:21:30 -04:00
export default async function Page(props: { params: Promise<any> }) {
const params = await props.params;
2024-10-11 18:00:22 +05:30
const { id } = params;
const chatFromDb = await getChatById({ id });
if (!chatFromDb) {
notFound();
2023-06-16 21:52:01 +04:00
}
2024-10-11 18:00:22 +05:30
// type casting
const chat: Chat = {
...chatFromDb,
messages: convertToUIMessages(chatFromDb.messages as Array<CoreMessage>),
};
2023-06-16 21:52:01 +04:00
2024-10-11 18:00:22 +05:30
const session = await auth();
if (!session || !session.user) {
return notFound();
}
2023-05-19 12:33:56 -04:00
2024-10-11 18:00:22 +05:30
if (session.user.id !== chat.userId) {
return notFound();
2023-06-16 15:28:43 -04:00
}
2024-10-11 18:00:22 +05:30
const cookieStore = await cookies();
const value = cookieStore.get('model')?.value;
const selectedModelName =
models.find((m) => m.name === value)?.name || DEFAULT_MODEL_NAME;
return (
<PreviewChat
id={chat.id}
initialMessages={chat.messages}
selectedModelName={selectedModelName}
/>
);
2023-06-02 15:15:35 -04:00
}