fix: workaround for using currentUser in server actions

This commit is contained in:
shadcn 2023-06-14 16:39:55 +04:00
parent c0011ed15f
commit 8156e62256
5 changed files with 11 additions and 34 deletions

View file

@ -56,5 +56,5 @@ export async function removeChat({ id, path }: { id: string; path: string }) {
await kv.zrem(`user:chat:${user.id}`, `chat:${id}`) await kv.zrem(`user:chat:${user.id}`, `chat:${id}`)
revalidatePath('/') revalidatePath('/')
revalidatePath('/chat/[id]') revalidatePath(path)
} }

View file

@ -1,5 +1,3 @@
'use client'
import { type Message } from 'ai-connector' import { type Message } from 'ai-connector'
import { Separator } from '@/components/ui/separator' import { Separator } from '@/components/ui/separator'

View file

@ -1,7 +1,6 @@
'use client' 'use client'
import * as React from 'react' import * as React from 'react'
import { useTransition } from 'react'
import Link from 'next/link' import Link from 'next/link'
import { usePathname, useRouter } from 'next/navigation' import { usePathname, useRouter } from 'next/navigation'
@ -17,25 +16,21 @@ import {
AlertDialogTitle AlertDialogTitle
} from '@/components/ui/alert-dialog' } from '@/components/ui/alert-dialog'
import { Button, buttonVariants } from '@/components/ui/button' import { Button, buttonVariants } from '@/components/ui/button'
import { removeChat } from '@/app/actions'
import { IconMessage, IconSpinner, IconTrash } from '@/components/ui/icons' import { IconMessage, IconSpinner, IconTrash } from '@/components/ui/icons'
export function SidebarItem({ interface SidebarItemProps {
title,
href,
id,
userId
}: {
title: string title: string
href: string href: string
id: string id: string
userId: string removeChat: (args: { id: string; path: string }) => Promise<void>
}) { }
export function SidebarItem({ title, href, id, removeChat }: SidebarItemProps) {
const [open, setIsOpen] = React.useState(false) const [open, setIsOpen] = React.useState(false)
const pathname = usePathname() const pathname = usePathname()
const router = useRouter() const router = useRouter()
const [isPending, startTransition] = React.useTransition()
const isActive = pathname === href const isActive = pathname === href
const [isPending, startTransition] = useTransition()
if (!id) return null if (!id) return null
@ -85,7 +80,7 @@ export function SidebarItem({
onClick={event => { onClick={event => {
event.preventDefault() event.preventDefault()
startTransition(async () => { startTransition(async () => {
await removeChat({ id, userId, path: href }) await removeChat({ id, path: href })
setIsOpen(false) setIsOpen(false)
router.push('/') router.push('/')
}) })

View file

@ -1,27 +1,12 @@
import { getChats } from '@/app/actions' import { getChats, removeChat } from '@/app/actions'
import { SidebarItem } from '@/components/sidebar-item' import { SidebarItem } from '@/components/sidebar-item'
import { LoginButton } from '@/components/login-button'
export interface SidebarListProps { export interface SidebarListProps {
userId?: string userId?: string
} }
export async function SidebarList({ userId }: SidebarListProps) { export async function SidebarList({ userId }: SidebarListProps) {
if (!userId) {
return (
<div className="flex flex-1 flex-col items-center space-y-4 p-6 text-sm">
<div className="space-y-1 text-center">
<p className="font-medium">You are not logged in!</p>
<p className="text-muted-foreground">
Please login for chat history.
</p>
</div>
<LoginButton />
</div>
)
}
const chats = await getChats(userId) const chats = await getChats(userId)
return ( return (
@ -32,9 +17,9 @@ export async function SidebarList({ userId }: SidebarListProps) {
<SidebarItem <SidebarItem
key={chat.id} key={chat.id}
title={chat.title} title={chat.title}
userId={userId ?? ''}
href={`/chat/${chat.id}`} href={`/chat/${chat.id}`}
id={chat.id} id={chat.id}
removeChat={removeChat}
/> />
))} ))}
</div> </div>
@ -46,5 +31,3 @@ export async function SidebarList({ userId }: SidebarListProps) {
</div> </div>
) )
} }
SidebarList.displayName = 'SidebarList'

View file

@ -21,6 +21,7 @@ export interface SidebarProps {
export function Sidebar({ userId, children }: SidebarProps) { export function Sidebar({ userId, children }: SidebarProps) {
const { signOut } = useClerk() const { signOut } = useClerk()
return ( return (
<Sheet> <Sheet>
<SheetTrigger asChild> <SheetTrigger asChild>