diff --git a/app/actions.ts b/app/actions.ts index cb28bf5..3d40e88 100644 --- a/app/actions.ts +++ b/app/actions.ts @@ -5,7 +5,11 @@ import { kv } from '@vercel/kv' import { type Chat } from '@/lib/types' -export async function getChats(userId: string) { +export async function getChats(userId?: string | null) { + if (!userId) { + return [] + } + try { const pipeline = kv.pipeline() const chats: string[] = await kv.zrange(`user:chat:${userId}`, 0, -1) diff --git a/components/chat-panel.tsx b/components/chat-panel.tsx index c184c70..6189500 100644 --- a/components/chat-panel.tsx +++ b/components/chat-panel.tsx @@ -32,7 +32,7 @@ export function ChatPanel({ onClick={() => stop()} className="bg-background" > - + Stop generating ) : ( @@ -42,13 +42,13 @@ export function ChatPanel({ onClick={() => reload()} className="bg-background" > - + Regenerate response ) )} -
+
{ append({ @@ -58,7 +58,7 @@ export function ChatPanel({ }} isLoading={isLoading} /> -

+

Open source AI chatbot app built with{' '} Next.js and{' '} diff --git a/components/header.tsx b/components/header.tsx index e662cb4..d1dae70 100644 --- a/components/header.tsx +++ b/components/header.tsx @@ -5,19 +5,24 @@ import { buttonVariants } from '@/components/ui/button' import { GitHub, Separator, Vercel } from '@/components/icons' import { Sidebar } from '@/components/sidebar' import { UserMenu } from '@/components/user-menu' -import { getChats } from '@/app/actions' +import { SidebarList } from './sidebar-list' +import { Suspense } from 'react' export async function Header() { const session = await auth() - const chats = session?.user?.email ? await getChats(session.user.email) : [] return ( -

+
{/* @ts-ignore */} - -
- + + }> + {/* @ts-ignore */} + + + +
+
@@ -28,7 +33,7 @@ export async function Header() { rel="noopener noreferrer" className={cn(buttonVariants({ variant: 'outline' }))} > - + GitHub - + Deploy to Vercel - Deploy + Vercel
diff --git a/components/sidebar-list.tsx b/components/sidebar-list.tsx new file mode 100644 index 0000000..22c405e --- /dev/null +++ b/components/sidebar-list.tsx @@ -0,0 +1,40 @@ +import { getChats } from '@/app/actions' +import { Session } from '@auth/core/types' +import { SidebarItem } from './sidebar-item' + +export interface SidebarListProps { + session?: Session +} + +export async function SidebarList(props: SidebarListProps) { + const chats = await getChats(props.session?.user?.email) + return ( +
+ {chats?.length ? ( +
+ {chats.map(chat => ( + + ))} +
+ ) : ( +
+

+ {props?.session?.user ? ( + <>No chat history + ) : ( + <>Login for history + )} +

+
+ )} +
+ ) +} + +SidebarList.displayName = 'SidebarList' diff --git a/components/sidebar.tsx b/components/sidebar.tsx index 83e0fa9..c3f011d 100644 --- a/components/sidebar.tsx +++ b/components/sidebar.tsx @@ -5,7 +5,6 @@ import { signOut } from '@auth/nextjs/client' import { type Session } from '@auth/nextjs/types' import { Sidebar as SidebarIcon } from 'lucide-react' -import { type Chat } from '@/lib/types' import { Button } from '@/components/ui/button' import { Sheet, @@ -14,20 +13,19 @@ import { SheetTitle, SheetTrigger } from '@/components/ui/sheet' -import { SidebarItem } from '@/components/sidebar-item' import { ThemeToggle } from '@/components/theme-toggle' export interface SidebarProps { session?: Session - chats: Chat[] + children?: React.ReactNode } -export async function Sidebar({ session, chats }: SidebarProps) { +export function Sidebar({ session, children }: SidebarProps) { return ( - @@ -38,27 +36,7 @@ export async function Sidebar({ session, chats }: SidebarProps) { Chat History -
- {chats?.length ? ( -
- {chats.map(chat => ( - - ))} -
- ) : ( -
-

- {session?.user ? <>No chat history : <>Login for history} -

-
- )} -
+ {children}
{session?.user && (