misc UI fixes (#20)

This commit is contained in:
Jared Palmer 2023-06-16 14:09:05 -04:00 committed by GitHub
commit 572ce91708
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 523 additions and 313 deletions

View file

@ -1,9 +1,10 @@
'use server'
import { revalidatePath } from 'next/cache'
import { redirect } from 'next/navigation'
import { kv } from '@vercel/kv'
import { auth } from '@/auth'
import { auth } from '@/auth'
import { type Chat } from '@/lib/types'
export async function getChats(userId?: string | null) {
@ -32,12 +33,8 @@ export async function getChats(userId?: string | null) {
export async function getChat(id: string, userId: string) {
const chat = await kv.hgetall<Chat>(`chat:${id}`)
if (!chat) {
throw new Error('Not found')
}
if (userId && chat.userId !== userId) {
throw new Error('Unauthorized')
if (!chat || (userId && chat.userId !== userId)) {
return null
}
return chat
@ -47,41 +44,36 @@ export async function removeChat({ id, path }: { id: string; path: string }) {
const session = await auth()
if (!session) {
throw new Error('Unauthorized')
return {
error: 'Unauthorized'
}
}
const uid = await kv.hget<string>(`chat:${id}`, 'userId')
if (uid !== session?.user?.id) {
throw new Error('Unauthorized')
return {
error: 'Unauthorized'
}
}
await kv.del(`chat:${id}`)
await kv.zrem(`user:chat:${session.user.id}`, `chat:${id}`)
revalidatePath('/')
revalidatePath(path)
return revalidatePath(path)
}
export async function clearChats() {
const session = await auth()
if (!session) {
throw new Error('Unauthorized')
}
if (!session.user?.id) {
throw new Error('Unauthorized')
}
const chats: string[] = await kv.zrange(
`user:chat:${session.user.id}`,
0,
-1,
{
rev: true
if (!session?.user?.id) {
return {
error: 'Unauthorized'
}
)
}
const chats: string[] = await kv.zrange(`user:chat:${session.user.id}`, 0, -1)
const pipeline = kv.pipeline()
@ -93,6 +85,7 @@ export async function clearChats() {
await pipeline.exec()
revalidatePath('/')
return redirect('/')
}
export async function getSharedChat(id: string) {
@ -108,16 +101,10 @@ export async function getSharedChat(id: string) {
export async function shareChat(chat: Chat) {
const session = await auth()
if (!session) {
throw new Error('Unauthorized')
}
if (!session.user?.id) {
throw new Error('Unauthorized')
}
if (chat.userId !== session.user?.id) {
throw new Error('Unauthorized')
if (!session?.user?.id || session.user.id !== chat.userId) {
return {
error: 'Unauthorized'
}
}
const payload = {

View file

@ -1,8 +1,9 @@
import { kv } from '@vercel/kv'
import { OpenAIStream, StreamingTextResponse } from 'ai-connector'
import { Configuration, OpenAIApi } from 'openai-edge'
import { nanoid } from '@/lib/utils'
import { auth } from '@/auth'
import { nanoid } from '@/lib/utils'
export const runtime = 'edge'

View file

@ -1,8 +1,9 @@
import { type Metadata } from 'next'
import { auth } from '@/auth'
import { notFound, redirect } from 'next/navigation'
import { Chat } from '@/components/chat'
import { auth } from '@/auth'
import { getChat } from '@/app/actions'
import { Chat } from '@/components/chat'
// export const runtime = 'edge'
export const preferredRegion = 'home'
@ -16,16 +17,30 @@ export interface ChatPageProps {
export async function generateMetadata({
params
}: ChatPageProps): Promise<Metadata> {
const { user } = await auth()
const chat = await getChat(params.id, user?.id ?? '')
const session = await auth()
if (!session?.user) {
return {}
}
const chat = await getChat(params.id, session.user.id)
return {
title: chat?.title.slice(0, 50) ?? 'Chat'
}
}
export default async function ChatPage({ params }: ChatPageProps) {
const { user } = await auth()
const chat = await getChat(params.id, user?.id ?? '')
const session = await auth()
if (!session?.user) {
redirect('/sign-in')
}
const chat = await getChat(params.id, session.user.id)
if (!chat) {
notFound()
}
return <Chat id={chat.id} initialMessages={chat.messages} />
}

View file

@ -43,10 +43,10 @@ export default function RootLayout({ children }: RootLayoutProps) {
>
<Toaster />
<Providers attribute="class" defaultTheme="system" enableSystem>
<div className="flex flex-col min-h-screen">
<div className="flex min-h-screen flex-col">
{/* @ts-ignore */}
<Header />
<main className="flex flex-col flex-1 bg-muted/50">{children}</main>
<main className="flex flex-1 flex-col bg-muted/50">{children}</main>
</div>
<TailwindIndicator />
</Providers>

View file

@ -1,13 +1,9 @@
import { FooterText } from '@/components/footer'
import { LoginButton } from '@/components/login-button'
export default function SignInPage() {
return (
<div className="flex flex-col items-center justify-center h-full min-h-screen">
<div className="space-y-4">
<LoginButton />
<FooterText />
</div>
<div className="flex h-[calc(100vh-theme(spacing.16))] items-center justify-center py-10">
<LoginButton />
</div>
)
}