2023-06-02 15:33:48 -04:00
|
|
|
'use client'
|
2023-05-19 12:33:56 -04:00
|
|
|
|
2023-06-02 15:33:48 -04:00
|
|
|
import { cn } from '@/lib/utils'
|
|
|
|
|
import Link from 'next/link'
|
|
|
|
|
import { usePathname, useRouter } from 'next/navigation'
|
|
|
|
|
import { MessageCircleIcon, Trash2Icon, Loader2Icon } from 'lucide-react'
|
|
|
|
|
import { removeChat } from './actions'
|
|
|
|
|
import { useTransition } from 'react'
|
2023-05-19 12:33:56 -04:00
|
|
|
|
|
|
|
|
export function SidebarItem({
|
|
|
|
|
title,
|
|
|
|
|
href,
|
|
|
|
|
id,
|
2023-06-02 15:33:48 -04:00
|
|
|
userId
|
2023-05-19 12:33:56 -04:00
|
|
|
}: {
|
2023-06-02 15:33:48 -04:00
|
|
|
title: string
|
|
|
|
|
href: string
|
|
|
|
|
id: string
|
|
|
|
|
userId: string
|
2023-05-19 12:33:56 -04:00
|
|
|
}) {
|
2023-06-02 15:33:48 -04:00
|
|
|
const pathname = usePathname()
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
const active = pathname === href
|
|
|
|
|
const [isPending, startTransition] = useTransition()
|
2023-05-19 12:33:56 -04:00
|
|
|
|
2023-06-02 15:33:48 -04:00
|
|
|
if (!id) return null
|
2023-05-19 12:33:56 -04:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Link
|
|
|
|
|
href={href}
|
|
|
|
|
className={cn(
|
2023-06-02 15:33:48 -04:00
|
|
|
'group flex shrink-0 cursor-pointer items-center gap-2 rounded p-2 text-sm font-medium transition-colors duration-100 hover:bg-zinc-500/10 hover:dark:bg-zinc-300/20',
|
2023-05-19 12:33:56 -04:00
|
|
|
isPending
|
2023-06-02 15:33:48 -04:00
|
|
|
? 'text-zinc-400 dark:text-zinc-500'
|
|
|
|
|
: 'text-zinc-800 dark:text-zinc-400',
|
2023-05-19 12:33:56 -04:00
|
|
|
active
|
2023-06-02 15:33:48 -04:00
|
|
|
? 'bg-zinc-500/20 text-zinc-900 font-semibold hover:bg-zinc-500/30 dark:text-white'
|
|
|
|
|
: 'bg-transparent'
|
2023-05-19 12:33:56 -04:00
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<MessageCircleIcon className="h-4 w-4" />
|
|
|
|
|
<div
|
|
|
|
|
className="relative max-h-5 flex-1 overflow-hidden text-ellipsis break-all select-none"
|
|
|
|
|
title={title}
|
|
|
|
|
>
|
|
|
|
|
<span className="whitespace-nowrap">{title}</span>
|
|
|
|
|
</div>
|
|
|
|
|
<button
|
|
|
|
|
className="opacity-0 -mr-6 transition-opacity duration-0 hover:bg-zinc-400/50 group-hover:mr-0 group-hover:opacity-80 p-1 -my-1 rounded group-hover:duration-600"
|
|
|
|
|
disabled={isPending}
|
2023-06-02 15:33:48 -04:00
|
|
|
onClick={e => {
|
|
|
|
|
e.preventDefault()
|
2023-05-19 12:33:56 -04:00
|
|
|
startTransition(() => {
|
2023-06-02 14:28:05 -04:00
|
|
|
removeChat({ id, userId, path: href }).then(() => {
|
2023-06-02 15:33:48 -04:00
|
|
|
router.push('/')
|
|
|
|
|
})
|
|
|
|
|
})
|
2023-05-19 12:33:56 -04:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{isPending ? (
|
|
|
|
|
<Loader2Icon className="animate-spin h-4 w-4" />
|
|
|
|
|
) : (
|
|
|
|
|
<Trash2Icon className="h-4 w-4" />
|
|
|
|
|
)}
|
|
|
|
|
</button>
|
|
|
|
|
</Link>
|
2023-06-02 15:33:48 -04:00
|
|
|
)
|
2023-05-19 12:33:56 -04:00
|
|
|
}
|