|
| 1 | +import { getLogtoContext } from "@logto/next/server-actions"; |
| 2 | +import { logtoConfig } from "@/lib/auth"; |
| 3 | +import { redirect, notFound } from "next/navigation"; |
| 4 | +import { prisma } from "@/lib/prisma"; |
| 5 | +import Models from "@/consts/models.json"; |
| 6 | +import * as React from "react"; |
| 7 | +import { ChatContainer } from "@/components/chatContainer"; |
| 8 | +import { ChatMeta } from "@/components/chatMeta"; |
| 9 | +import Image from "next/image"; |
| 10 | +import Link from "next/link"; |
| 11 | + |
| 12 | +import UserIcon from "@/assets/img/user.png"; |
| 13 | +import Logo from "@/assets/img/mikan-vtube.svg"; |
| 14 | + |
| 15 | +const ModelInfoFromID: Record<string, { name: string; description: string }> = |
| 16 | + Object.entries(Models) |
| 17 | + .flatMap(([providerKey, provider]) => |
| 18 | + provider.models.map( |
| 19 | + (model) => |
| 20 | + [ |
| 21 | + model.id, |
| 22 | + { name: model.name, description: provider.description }, |
| 23 | + ] as [string, { name: string; description: string }], |
| 24 | + ), |
| 25 | + ) |
| 26 | + .reduce( |
| 27 | + ( |
| 28 | + acc, |
| 29 | + [id, info]: [string, { name: string; description: string }], |
| 30 | + ) => ({ |
| 31 | + ...acc, |
| 32 | + [id]: info, |
| 33 | + }), |
| 34 | + {}, |
| 35 | + ); |
| 36 | + |
| 37 | +export async function generateMetadata({ |
| 38 | + params, |
| 39 | +}: { params: Promise<{ id: string }> }) { |
| 40 | + const id = (await params).id; |
| 41 | + |
| 42 | + const chat = await prisma.chat.findUnique({ |
| 43 | + where: { id }, |
| 44 | + include: { |
| 45 | + messages: { |
| 46 | + orderBy: { createdAt: "asc" }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + if (!chat || !chat.public) { |
| 52 | + return { |
| 53 | + title: "Chat not found", |
| 54 | + description: "The requested chat does not exist or is not public.", |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + const modelInfo = ModelInfoFromID[chat.model] || { name: "Unknown Model" }; |
| 59 | + |
| 60 | + return { |
| 61 | + title: `MD Chat - ${chat.name || "Untitled Chat"}`, |
| 62 | + description: `Chat using ${modelInfo.name}. Read it on MD Chat!`, |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +export default async function SharePage({ |
| 67 | + params, |
| 68 | +}: { params: Promise<{ id: string }> }) { |
| 69 | + const { id } = await params; |
| 70 | + const { claims } = await getLogtoContext(logtoConfig); |
| 71 | + |
| 72 | + const chat = await prisma.chat.findUnique({ |
| 73 | + where: { id }, |
| 74 | + include: { |
| 75 | + messages: { |
| 76 | + orderBy: { createdAt: "asc" }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + if (!chat) { |
| 82 | + return notFound(); |
| 83 | + } |
| 84 | + |
| 85 | + if (!chat.public) { |
| 86 | + return notFound(); |
| 87 | + } |
| 88 | + |
| 89 | + if (chat.userId == claims?.sub) { |
| 90 | + await redirect(`/chat/${id}`); |
| 91 | + } |
| 92 | + |
| 93 | + const messages = await prisma.message.findMany({ |
| 94 | + where: { chatId: chat.id }, |
| 95 | + orderBy: { createdAt: "asc" }, |
| 96 | + include: { attachments: true }, |
| 97 | + }); |
| 98 | + |
| 99 | + const modelsArray = Object.entries(Models).flatMap( |
| 100 | + ([providerKey, provider]) => |
| 101 | + provider.models.map((model) => ({ |
| 102 | + ...model, |
| 103 | + provider: providerKey, |
| 104 | + providerName: provider.name, |
| 105 | + icon: provider.icon, |
| 106 | + })), |
| 107 | + ); |
| 108 | + |
| 109 | + const modelInfo = ModelInfoFromID[chat.model] || chat.model; |
| 110 | + |
| 111 | + const formattedMessages = messages.map((message) => ({ |
| 112 | + id: message.id, |
| 113 | + content: message.content, |
| 114 | + role: message.role as "user" | "assistant" | "system", |
| 115 | + createdAt: message.createdAt, |
| 116 | + experimental_attachments: message.attachments.map((attachment) => ({ |
| 117 | + id: attachment.id, |
| 118 | + url: attachment.url, |
| 119 | + contentType: attachment.filetype || "unknown", |
| 120 | + })), |
| 121 | + })); |
| 122 | + |
| 123 | + return ( |
| 124 | + <main className="container mx-auto p-4"> |
| 125 | + <ChatMeta |
| 126 | + createdAt={chat.createdAt.toISOString()} |
| 127 | + model={modelInfo?.name || chat.model || "Unknown Model"} |
| 128 | + title={chat.name || "Untitled Chat"} |
| 129 | + id={id} |
| 130 | + shared={chat.public} |
| 131 | + isPublic={true} |
| 132 | + /> |
| 133 | + <div className="space-y-4"> |
| 134 | + <ChatContainer |
| 135 | + id={id} |
| 136 | + avatar={UserIcon.src} |
| 137 | + //@ts-ignore |
| 138 | + initialMessages={formattedMessages} |
| 139 | + model={chat.model} |
| 140 | + //@ts-ignore |
| 141 | + models={modelsArray} |
| 142 | + isPublic={true} |
| 143 | + /> |
| 144 | + </div> |
| 145 | + <div className="flex items-center w-full card bg-base-200 shadow-xl p-4 mt-8"> |
| 146 | + <div className="flex flex-row justify-between items-center w-full"> |
| 147 | + <div className="flex flex-row items-center"> |
| 148 | + <p |
| 149 | + className={ |
| 150 | + "text-base-content/70 text-sm mr-2 font-bold text-xl" |
| 151 | + } |
| 152 | + > |
| 153 | + Generated on |
| 154 | + </p> |
| 155 | + <Image |
| 156 | + src={Logo} |
| 157 | + alt="MikanDev Logo" |
| 158 | + width={200} |
| 159 | + height={200} |
| 160 | + className="rounded-lg w-1/6 h-auto" |
| 161 | + /> |
| 162 | + <p |
| 163 | + className={ |
| 164 | + "text-base-content/70 text-sm ml-2 font-bold text-xl" |
| 165 | + } |
| 166 | + > |
| 167 | + Chat |
| 168 | + </p> |
| 169 | + </div> |
| 170 | + <Link href={`/chat`} className=""> |
| 171 | + <button className={"btn btn-secondary"}> |
| 172 | + Try it now! |
| 173 | + </button> |
| 174 | + </Link> |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + </main> |
| 178 | + ); |
| 179 | +} |
0 commit comments