ai modal
This commit is contained in:
32
src/app/@modal/(.)chat/_components/ChatModal.tsx
Normal file
32
src/app/@modal/(.)chat/_components/ChatModal.tsx
Normal file
@@ -0,0 +1,32 @@
|
||||
'use client'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '~/components/ui/dialog'
|
||||
import ChatInterface from '~/app/chat/_components/ChatInterface'
|
||||
|
||||
type DBMessage = {
|
||||
id: string
|
||||
role: 'user' | 'assistant'
|
||||
content: string
|
||||
}
|
||||
|
||||
interface ChatModalProps {
|
||||
sessionId: string
|
||||
initialMessages: DBMessage[]
|
||||
}
|
||||
|
||||
export default function ChatModal({ sessionId, initialMessages }: ChatModalProps) {
|
||||
const router = useRouter()
|
||||
|
||||
return (
|
||||
<Dialog open onOpenChange={() => router.back()}>
|
||||
<DialogContent className="max-w-2xl h-[80vh] flex flex-col p-0 gap-0">
|
||||
<DialogHeader className="p-4 border-b shrink-0">
|
||||
<DialogTitle>AI Recruiter</DialogTitle>
|
||||
</DialogHeader>
|
||||
<div className="flex-1 overflow-hidden min-h-0">
|
||||
<ChatInterface sessionId={sessionId} initialMessages={initialMessages} />
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
34
src/app/@modal/(.)chat/page.tsx
Normal file
34
src/app/@modal/(.)chat/page.tsx
Normal file
@@ -0,0 +1,34 @@
|
||||
import { auth } from '@clerk/nextjs/server'
|
||||
import { redirect } from 'next/navigation'
|
||||
import { asc, desc, eq } from 'drizzle-orm'
|
||||
import { db } from '~/server/db'
|
||||
import { chatMessage, chatSession } from '~/server/dbschema/schema'
|
||||
import ChatModal from './_components/ChatModal'
|
||||
|
||||
export default async function ChatModalPage() {
|
||||
const { userId } = await auth()
|
||||
if (!userId) redirect('/')
|
||||
|
||||
let session = await db
|
||||
.select()
|
||||
.from(chatSession)
|
||||
.where(eq(chatSession.userId, userId))
|
||||
.orderBy(desc(chatSession.createdAt))
|
||||
.limit(1)
|
||||
.then((r) => r[0])
|
||||
|
||||
if (!session) {
|
||||
const [created] = await db.insert(chatSession).values({ userId }).returning()
|
||||
session = created
|
||||
}
|
||||
|
||||
if (!session) redirect('/')
|
||||
|
||||
const messages = await db
|
||||
.select()
|
||||
.from(chatMessage)
|
||||
.where(eq(chatMessage.sessionId, session.id))
|
||||
.orderBy(asc(chatMessage.createdAt))
|
||||
|
||||
return <ChatModal sessionId={session.id} initialMessages={messages} />
|
||||
}
|
||||
Reference in New Issue
Block a user