2023-06-16 15:20:42 +04:00
|
|
|
'use client'
|
|
|
|
|
|
|
|
|
|
import { useRouter } from 'next/navigation'
|
2023-12-04 12:42:53 -05:00
|
|
|
import * as React from 'react'
|
2023-06-16 15:20:42 +04:00
|
|
|
import { toast } from 'react-hot-toast'
|
|
|
|
|
|
2023-12-04 12:42:53 -05:00
|
|
|
import { ServerActionResult, type Chat } from '@/lib/types'
|
2023-06-16 15:20:42 +04:00
|
|
|
import {
|
|
|
|
|
AlertDialog,
|
|
|
|
|
AlertDialogAction,
|
|
|
|
|
AlertDialogCancel,
|
|
|
|
|
AlertDialogContent,
|
|
|
|
|
AlertDialogDescription,
|
|
|
|
|
AlertDialogFooter,
|
|
|
|
|
AlertDialogHeader,
|
|
|
|
|
AlertDialogTitle
|
|
|
|
|
} from '@/components/ui/alert-dialog'
|
|
|
|
|
import { Button } from '@/components/ui/button'
|
2023-12-04 12:42:53 -05:00
|
|
|
import { IconShare, IconSpinner, IconTrash } from '@/components/ui/icons'
|
|
|
|
|
import { ChatShareDialog } from '@/components/chat-share-dialog'
|
2023-06-16 16:18:52 +04:00
|
|
|
import {
|
|
|
|
|
Tooltip,
|
|
|
|
|
TooltipContent,
|
|
|
|
|
TooltipTrigger
|
|
|
|
|
} from '@/components/ui/tooltip'
|
2023-06-16 15:20:42 +04:00
|
|
|
|
|
|
|
|
interface SidebarActionsProps {
|
|
|
|
|
chat: Chat
|
2023-06-16 21:52:01 +04:00
|
|
|
removeChat: (args: { id: string; path: string }) => ServerActionResult<void>
|
2023-12-04 12:42:53 -05:00
|
|
|
shareChat: (id: string) => ServerActionResult<Chat>
|
2023-06-16 15:20:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function SidebarActions({
|
|
|
|
|
chat,
|
|
|
|
|
removeChat,
|
|
|
|
|
shareChat
|
|
|
|
|
}: SidebarActionsProps) {
|
2023-12-04 12:42:53 -05:00
|
|
|
const router = useRouter()
|
2023-06-16 15:20:42 +04:00
|
|
|
const [deleteDialogOpen, setDeleteDialogOpen] = React.useState(false)
|
|
|
|
|
const [shareDialogOpen, setShareDialogOpen] = React.useState(false)
|
|
|
|
|
const [isRemovePending, startRemoveTransition] = React.useTransition()
|
2023-06-16 16:18:52 +04:00
|
|
|
|
2023-06-16 15:20:42 +04:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<div className="space-x-1">
|
2023-06-16 16:18:52 +04:00
|
|
|
<Tooltip>
|
|
|
|
|
<TooltipTrigger asChild>
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
2023-12-04 12:42:53 -05:00
|
|
|
className="w-6 h-6 p-0 hover:bg-background"
|
2023-06-16 16:18:52 +04:00
|
|
|
onClick={() => setShareDialogOpen(true)}
|
|
|
|
|
>
|
|
|
|
|
<IconShare />
|
|
|
|
|
<span className="sr-only">Share</span>
|
|
|
|
|
</Button>
|
|
|
|
|
</TooltipTrigger>
|
|
|
|
|
<TooltipContent>Share chat</TooltipContent>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
<Tooltip>
|
|
|
|
|
<TooltipTrigger asChild>
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
2023-12-04 12:42:53 -05:00
|
|
|
className="w-6 h-6 p-0 hover:bg-background"
|
2023-06-16 16:18:52 +04:00
|
|
|
disabled={isRemovePending}
|
|
|
|
|
onClick={() => setDeleteDialogOpen(true)}
|
|
|
|
|
>
|
|
|
|
|
<IconTrash />
|
|
|
|
|
<span className="sr-only">Delete</span>
|
|
|
|
|
</Button>
|
|
|
|
|
</TooltipTrigger>
|
|
|
|
|
<TooltipContent>Delete chat</TooltipContent>
|
|
|
|
|
</Tooltip>
|
2023-06-16 15:20:42 +04:00
|
|
|
</div>
|
2023-12-04 12:42:53 -05:00
|
|
|
<ChatShareDialog
|
|
|
|
|
chat={chat}
|
|
|
|
|
shareChat={shareChat}
|
|
|
|
|
open={shareDialogOpen}
|
|
|
|
|
onOpenChange={setShareDialogOpen}
|
|
|
|
|
onCopy={() => setShareDialogOpen(false)}
|
|
|
|
|
/>
|
2023-06-16 15:20:42 +04:00
|
|
|
<AlertDialog open={deleteDialogOpen} onOpenChange={setDeleteDialogOpen}>
|
|
|
|
|
<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={isRemovePending}>
|
|
|
|
|
Cancel
|
|
|
|
|
</AlertDialogCancel>
|
|
|
|
|
<AlertDialogAction
|
|
|
|
|
disabled={isRemovePending}
|
|
|
|
|
onClick={event => {
|
|
|
|
|
event.preventDefault()
|
2023-12-04 12:42:53 -05:00
|
|
|
// @ts-ignore
|
2023-06-16 15:20:42 +04:00
|
|
|
startRemoveTransition(async () => {
|
2023-06-16 21:52:01 +04:00
|
|
|
const result = await removeChat({
|
2023-06-16 15:20:42 +04:00
|
|
|
id: chat.id,
|
|
|
|
|
path: chat.path
|
|
|
|
|
})
|
2023-06-16 21:52:01 +04:00
|
|
|
|
|
|
|
|
if (result && 'error' in result) {
|
|
|
|
|
toast.error(result.error)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:20:42 +04:00
|
|
|
setDeleteDialogOpen(false)
|
2023-06-17 13:15:14 -05:00
|
|
|
router.refresh()
|
2023-06-16 15:20:42 +04:00
|
|
|
router.push('/')
|
2023-06-17 13:15:14 -05:00
|
|
|
toast.success('Chat deleted')
|
2023-06-16 15:20:42 +04:00
|
|
|
})
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{isRemovePending && <IconSpinner className="mr-2 animate-spin" />}
|
|
|
|
|
Delete
|
|
|
|
|
</AlertDialogAction>
|
|
|
|
|
</AlertDialogFooter>
|
|
|
|
|
</AlertDialogContent>
|
|
|
|
|
</AlertDialog>
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|