2023-06-11 00:23:23 +04:00
|
|
|
'use client'
|
|
|
|
|
|
2023-06-11 15:08:59 +04:00
|
|
|
import * as React from 'react'
|
2023-06-11 00:23:23 +04:00
|
|
|
import Link from 'next/link'
|
|
|
|
|
import { usePathname, useRouter } from 'next/navigation'
|
|
|
|
|
|
|
|
|
|
import { cn } from '@/lib/utils'
|
|
|
|
|
import {
|
|
|
|
|
AlertDialog,
|
|
|
|
|
AlertDialogAction,
|
|
|
|
|
AlertDialogCancel,
|
|
|
|
|
AlertDialogContent,
|
|
|
|
|
AlertDialogDescription,
|
|
|
|
|
AlertDialogFooter,
|
|
|
|
|
AlertDialogHeader,
|
|
|
|
|
AlertDialogTitle
|
|
|
|
|
} from '@/components/ui/alert-dialog'
|
|
|
|
|
import { Button, buttonVariants } from '@/components/ui/button'
|
2023-06-13 16:02:07 +04:00
|
|
|
import { IconMessage, IconSpinner, IconTrash } from '@/components/ui/icons'
|
2023-06-11 00:23:23 +04:00
|
|
|
|
2023-06-14 16:39:55 +04:00
|
|
|
interface SidebarItemProps {
|
2023-06-11 00:23:23 +04:00
|
|
|
title: string
|
|
|
|
|
href: string
|
|
|
|
|
id: string
|
2023-06-14 16:39:55 +04:00
|
|
|
removeChat: (args: { id: string; path: string }) => Promise<void>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function SidebarItem({ title, href, id, removeChat }: SidebarItemProps) {
|
2023-06-11 00:23:23 +04:00
|
|
|
const [open, setIsOpen] = React.useState(false)
|
|
|
|
|
const pathname = usePathname()
|
|
|
|
|
const router = useRouter()
|
2023-06-14 16:39:55 +04:00
|
|
|
const [isPending, startTransition] = React.useTransition()
|
2023-06-11 00:23:23 +04:00
|
|
|
const isActive = pathname === href
|
|
|
|
|
|
|
|
|
|
if (!id) return null
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Link
|
|
|
|
|
href={href}
|
|
|
|
|
className={cn(
|
|
|
|
|
buttonVariants({ variant: 'ghost' }),
|
|
|
|
|
'group w-full px-2',
|
|
|
|
|
isActive && 'bg-accent'
|
|
|
|
|
)}
|
|
|
|
|
>
|
2023-06-13 16:02:07 +04:00
|
|
|
<IconMessage className="mr-2" />
|
2023-06-11 00:23:23 +04:00
|
|
|
<div
|
2023-06-11 15:39:04 +04:00
|
|
|
className="relative max-h-5 flex-1 select-none overflow-hidden text-ellipsis break-all"
|
2023-06-11 00:23:23 +04:00
|
|
|
title={title}
|
|
|
|
|
>
|
|
|
|
|
<span className="whitespace-nowrap">{title}</span>
|
|
|
|
|
</div>
|
|
|
|
|
{isActive && (
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
2023-06-11 15:39:04 +04:00
|
|
|
className="h-7 w-7"
|
2023-06-11 00:23:23 +04:00
|
|
|
disabled={isPending}
|
|
|
|
|
onClick={() => setIsOpen(true)}
|
|
|
|
|
>
|
2023-06-13 16:02:07 +04:00
|
|
|
<IconTrash />
|
2023-06-11 00:23:23 +04:00
|
|
|
<span className="sr-only">Delete</span>
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
</Link>
|
|
|
|
|
<AlertDialog open={open} onOpenChange={setIsOpen}>
|
|
|
|
|
<AlertDialogContent>
|
|
|
|
|
<AlertDialogHeader>
|
|
|
|
|
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
|
|
|
|
|
<AlertDialogDescription>
|
|
|
|
|
This will permanently delete your chat message and remove your
|
|
|
|
|
data from our servers.
|
|
|
|
|
</AlertDialogDescription>
|
|
|
|
|
</AlertDialogHeader>
|
|
|
|
|
<AlertDialogFooter>
|
|
|
|
|
<AlertDialogCancel disabled={isPending}>Cancel</AlertDialogCancel>
|
|
|
|
|
<AlertDialogAction
|
|
|
|
|
disabled={isPending}
|
|
|
|
|
onClick={event => {
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
startTransition(async () => {
|
2023-06-14 16:39:55 +04:00
|
|
|
await removeChat({ id, path: href })
|
2023-06-11 00:23:23 +04:00
|
|
|
setIsOpen(false)
|
|
|
|
|
router.push('/')
|
|
|
|
|
})
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-06-13 16:02:07 +04:00
|
|
|
{isPending && <IconSpinner className="mr-2 animate-spin" />}
|
2023-06-11 00:23:23 +04:00
|
|
|
Delete
|
|
|
|
|
</AlertDialogAction>
|
|
|
|
|
</AlertDialogFooter>
|
|
|
|
|
</AlertDialogContent>
|
|
|
|
|
</AlertDialog>
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|