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

37 lines
936 B
TypeScript
Raw Normal View History

import { CoreMessage } from "ai";
2024-10-11 18:00:22 +05:30
import { notFound } from "next/navigation";
2023-05-19 12:33:56 -04:00
2024-10-11 18:00:22 +05:30
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 { convertToUIMessages, generateUUID } 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
return <PreviewChat id={chat.id} initialMessages={chat.messages} />;
2023-06-02 15:15:35 -04:00
}