Skip to content

Commit cf8a1cc

Browse files
committed
feat: add chat pages
1 parent d0dcf27 commit cf8a1cc

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import type { Metadata } from "next";
2+
import { notFound, redirect } from "next/navigation";
3+
4+
import { Chat } from "~/components/ai/chat";
5+
import { AI, getChat } from "~/components/ai/chat/actions";
6+
import type { Session } from "~/utils/types";
7+
import { createClient } from "~/supabase/server";
8+
9+
export interface ChatPageProps {
10+
params: {
11+
id: string;
12+
};
13+
}
14+
15+
export async function generateMetadata({
16+
params,
17+
}: ChatPageProps): Promise<Metadata> {
18+
const supabase = createClient();
19+
const { data } = await supabase.auth.getUser();
20+
21+
const session: Session | undefined = data?.user?.email
22+
? {
23+
user: {
24+
id: data.user.id,
25+
email: data.user.email,
26+
},
27+
}
28+
: undefined;
29+
30+
if (!session?.user) {
31+
return {};
32+
}
33+
34+
const chat = await getChat(params.id, session.user.id);
35+
return {
36+
title: chat?.title.toString().slice(0, 50) ?? "Chat",
37+
};
38+
}
39+
40+
export default async function ChatPage({ params }: ChatPageProps) {
41+
const supabase = createClient();
42+
const { data } = await supabase.auth.getUser();
43+
const session: Session | undefined = data?.user?.email
44+
? {
45+
user: {
46+
id: data.user.id,
47+
email: data.user.email,
48+
},
49+
}
50+
: undefined;
51+
const missingKeys: string[] = [];
52+
53+
if (!session?.user) {
54+
redirect(`/login?next=/chat/${params.id}`);
55+
}
56+
57+
const userId = session.user.id as string;
58+
const chat = await getChat(params.id, userId);
59+
60+
if (!chat) {
61+
redirect("/");
62+
}
63+
64+
if (chat?.userId !== session?.user?.id) {
65+
notFound();
66+
}
67+
68+
return (
69+
<AI initialAIState={{ chatId: chat.id, messages: chat.messages }}>
70+
<Chat
71+
id={chat.id}
72+
session={session}
73+
initialMessages={chat.messages}
74+
missingKeys={missingKeys}
75+
/>
76+
</AI>
77+
);
78+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { nanoid } from "~/utils";
2+
import { Chat } from "~/components/ai/chat";
3+
import { AI } from "~/components/ai/chat/actions";
4+
import { createClient } from "~/supabase/server";
5+
import type { Session } from "~/utils/types";
6+
7+
export const metadata = {
8+
title: "The Liaison AI",
9+
};
10+
11+
export default async function AIPage() {
12+
const id = nanoid();
13+
const supabase = createClient();
14+
const { data } = await supabase.auth.getUser();
15+
16+
const session: Session | undefined = data?.user?.email
17+
? {
18+
user: {
19+
id: data.user.id,
20+
email: data.user.email,
21+
},
22+
}
23+
: undefined;
24+
25+
return (
26+
<AI initialAIState={{ chatId: id, messages: [] }}>
27+
<Chat id={id} session={session} missingKeys={[]} />
28+
</AI>
29+
);
30+
}

0 commit comments

Comments
 (0)