Skip to content

Commit

Permalink
feat: category remove dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed May 16, 2024
1 parent c32e95d commit 2321667
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 24 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@fontsource/sn-pro": "5.0.1",
"@hono/auth-js": "1.0.7",
"@hookform/resolvers": "3.3.4",
"@radix-ui/react-alert-dialog": "1.0.5",
"@radix-ui/react-avatar": "1.0.4",
"@radix-ui/react-checkbox": "1.0.4",
"@radix-ui/react-collapsible": "1.0.3",
Expand Down
31 changes: 31 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 32 additions & 24 deletions src/renderer/src/components/feed-column/category-context-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
ContextMenuTrigger,
ContextMenuSeparator,
} from "@renderer/components/ui/context-menu"
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { useToast } from "@renderer/components/ui/use-toast"
import { ToastAction } from "@renderer/components/ui/toast"
import { apiFetch } from "@renderer/lib/queries/api-fetch"
import { Dialog, DialogTrigger } from "@renderer/components/ui/dialog"
import { CategoryRenameDialog } from "./category-rename-dialog"
import { CategoryRemoveDialog } from "./category-remove-dialog"
import {
AlertDialog,
AlertDialogTrigger,
} from "@renderer/components/ui/alert-dialog"

export function CategoryContextMenu({
name,
Expand All @@ -27,29 +28,36 @@ export function CategoryContextMenu({
feedIdList: string[]
}) {
const [dialogOpen, setDialogOpen] = useState(false)
const { toast } = useToast()

const queryClient = useQueryClient()

return (
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
<ContextMenu onOpenChange={(open) => onOpenChange?.(open)}>
<ContextMenuTrigger className="w-full">{children}</ContextMenuTrigger>
<ContextMenuContent onClick={(e) => e.stopPropagation()}>
<DialogTrigger asChild>
<ContextMenuItem>Rename Category</ContextMenuItem>
</DialogTrigger>
<ContextMenuItem>Remove Category</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem>Unfollow All</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
<CategoryRenameDialog
feedIdList={feedIdList}
view={view}
category={name}
onSuccess={() => setDialogOpen(false)}
/>
<AlertDialog>
<ContextMenu onOpenChange={(open) => onOpenChange?.(open)}>
<ContextMenuTrigger className="w-full">{children}</ContextMenuTrigger>
<ContextMenuContent onClick={(e) => e.stopPropagation()}>
<DialogTrigger asChild>
<ContextMenuItem>Rename Category</ContextMenuItem>
</DialogTrigger>
<AlertDialogTrigger>
<ContextMenuItem>Remove Category</ContextMenuItem>
</AlertDialogTrigger>
<ContextMenuSeparator />
<ContextMenuItem>Unfollow All</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
<CategoryRenameDialog
feedIdList={feedIdList}
view={view}
category={name}
onSuccess={() => setDialogOpen(false)}
/>
<CategoryRemoveDialog
feedIdList={feedIdList}
view={view}
category={name}
onSuccess={() => setDialogOpen(false)}
/>
</AlertDialog>
</Dialog>
)
}
61 changes: 61 additions & 0 deletions src/renderer/src/components/feed-column/category-remove-dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { apiFetch } from "@renderer/lib/queries/api-fetch"
import {
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@renderer/components/ui/alert-dialog"

export function CategoryRemoveDialog({
feedIdList,
onSuccess,
category,
view,
}: {
feedIdList: string[]
onSuccess?: () => void
category: string
view?: number
}) {
const queryClient = useQueryClient()
const renameMutation = useMutation({
mutationFn: async () =>
apiFetch("/categories", {
method: "DELETE",
body: {
feedIdList,
deleteSubscriptions: false,
},
}),
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: ["subscriptions", view],
})
onSuccess?.()
},
})

return (
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
Remove category <span className="font-bold">{category}</span>?
</AlertDialogTitle>
<AlertDialogDescription>
This operation will delete your category, but the feeds it contains
will be retained and grouped by website.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction onClick={() => renameMutation.mutate()}>
Continue
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
)
}
139 changes: 139 additions & 0 deletions src/renderer/src/components/ui/alert-dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import * as React from "react"
import * as AlertDialogPrimitive from "@radix-ui/react-alert-dialog"

import { cn } from "@renderer/lib/utils"
import { buttonVariants } from "@renderer/components/ui/button"

const AlertDialog = AlertDialogPrimitive.Root

const AlertDialogTrigger = AlertDialogPrimitive.Trigger

const AlertDialogPortal = AlertDialogPrimitive.Portal

const AlertDialogOverlay = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Overlay
className={cn(
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className,
)}
{...props}
ref={ref}
/>
))
AlertDialogOverlay.displayName = AlertDialogPrimitive.Overlay.displayName

const AlertDialogContent = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Content>
>(({ className, ...props }, ref) => (
<AlertDialogPortal>
<AlertDialogOverlay />
<AlertDialogPrimitive.Content
ref={ref}
className={cn(
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
className,
)}
{...props}
/>
</AlertDialogPortal>
))
AlertDialogContent.displayName = AlertDialogPrimitive.Content.displayName

const AlertDialogHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col space-y-2 text-center sm:text-left",
className,
)}
{...props}
/>
)
AlertDialogHeader.displayName = "AlertDialogHeader"

const AlertDialogFooter = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
className,
)}
{...props}
/>
)
AlertDialogFooter.displayName = "AlertDialogFooter"

const AlertDialogTitle = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Title>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Title
ref={ref}
className={cn("text-lg font-semibold", className)}
{...props}
/>
))
AlertDialogTitle.displayName = AlertDialogPrimitive.Title.displayName

const AlertDialogDescription = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Description>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Description
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
AlertDialogDescription.displayName =
AlertDialogPrimitive.Description.displayName

const AlertDialogAction = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Action>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Action>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Action
ref={ref}
className={cn(buttonVariants(), className)}
{...props}
/>
))
AlertDialogAction.displayName = AlertDialogPrimitive.Action.displayName

const AlertDialogCancel = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Cancel>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Cancel>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Cancel
ref={ref}
className={cn(
buttonVariants({ variant: "outline" }),
"mt-2 sm:mt-0",
className,
)}
{...props}
/>
))
AlertDialogCancel.displayName = AlertDialogPrimitive.Cancel.displayName

export {
AlertDialog,
AlertDialogPortal,
AlertDialogOverlay,
AlertDialogTrigger,
AlertDialogContent,
AlertDialogHeader,
AlertDialogFooter,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogAction,
AlertDialogCancel,
}

0 comments on commit 2321667

Please sign in to comment.