|
1 | | -// src/app/(public)/share/[id]/page.tsx |
2 | 1 | import { prisma } from "@/lib/prisma"; |
3 | | -import { ChatPage } from "@/components/chat"; |
4 | | -import { notFound } from "next/navigation"; |
5 | | -import AIIcon from "@/assets/img/ai.png"; |
| 2 | +import { getLogtoContext } from "@logto/next/server-actions"; |
| 3 | +import { logtoConfig } from "@/lib/auth"; |
6 | 4 |
|
7 | | -export default async function SharedChatPage({ |
8 | | - params, |
9 | | -}: { params: { id: string } }) { |
10 | | - const { id } = params; |
| 5 | +import { redirect } from "next/navigation"; |
11 | 6 |
|
12 | | - // Fetch the chat and its messages |
13 | | - const chat = await prisma.chat.findUnique({ |
14 | | - where: { id, public: true }, |
15 | | - include: { messages: { orderBy: { createdAt: "asc" } } }, |
16 | | - }); |
| 7 | +export default async function Home() { |
| 8 | + const { claims } = await getLogtoContext(logtoConfig); |
17 | 9 |
|
18 | | - if (!chat) { |
19 | | - return notFound(); |
20 | | - } |
| 10 | + if (!claims) { |
| 11 | + await redirect("/login"); |
| 12 | + } |
21 | 13 |
|
22 | | - // Format messages to match the expected structure by ChatPage |
23 | | - const formattedMessages = chat.messages.map((msg) => ({ |
24 | | - id: msg.id, |
25 | | - role: msg.role as "user" | "assistant" | "system", |
26 | | - content: msg.content, |
27 | | - parts: [{ type: "text", text: msg.content }], |
28 | | - attachment: msg.attachmentId |
29 | | - ? { |
30 | | - url: msg.attachmentId, |
31 | | - name: msg.attachmentName || undefined, |
32 | | - contentType: msg.attachmentType || undefined, |
33 | | - } |
34 | | - : undefined, |
35 | | - })); |
| 14 | + const userSharedChats = await prisma.chat.findMany({ |
| 15 | + where: { userId: claims?.sub, public: true }, |
| 16 | + orderBy: { createdAt: "desc" }, |
| 17 | + }); |
36 | 18 |
|
37 | | - return ( |
38 | | - <div className="container mx-auto flex flex-col h-screen"> |
39 | | - <div className="bg-base-200 p-4 flex items-center"> |
40 | | - <h1 className="text-2xl font-bold"> |
41 | | - {chat.name || "Shared Chat"} |
42 | | - </h1> |
43 | | - <span className="ml-2 badge badge-primary">Read Only</span> |
44 | | - </div> |
45 | | - |
46 | | - <div className="flex-grow overflow-hidden"> |
47 | | - <ChatPage |
48 | | - id={id} |
49 | | - msg={formattedMessages} |
50 | | - avatar="/avatar-placeholder.png" // Default avatar for shared chats |
51 | | - status="ready" |
52 | | - /> |
53 | | - </div> |
54 | | - </div> |
55 | | - ); |
| 19 | + return ( |
| 20 | + <div className="container mx-auto p-4"> |
| 21 | + <h1 className={"text-base-content text-4xl mb-4"}>Shared Chats</h1> |
| 22 | + <div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4"> |
| 23 | + {userSharedChats.map((chat) => ( |
| 24 | + <div |
| 25 | + key={chat.id} |
| 26 | + className="card bg-base-200 shadow-xl hover:shadow-2xl transition-shadow duration-300" |
| 27 | + > |
| 28 | + <div className="card-body"> |
| 29 | + <h2 className="card-title"> |
| 30 | + {chat.name || "Untitled Chat"} |
| 31 | + </h2> |
| 32 | + <p className="text-base-content/70"> |
| 33 | + Created at:{" "} |
| 34 | + {new Date(chat.createdAt).toLocaleDateString()} |
| 35 | + </p> |
| 36 | + <a |
| 37 | + href={`/chat/${chat.id}`} |
| 38 | + className="btn btn-primary mt-4" |
| 39 | + > |
| 40 | + Open Chat |
| 41 | + </a> |
| 42 | + </div> |
| 43 | + </div> |
| 44 | + ))} |
| 45 | + </div> |
| 46 | + </div> |
| 47 | + ); |
56 | 48 | } |
0 commit comments