🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Refactor account deletion components and enhance organization deletion dialog
Bill Yang13049366cdc2
diff --git a/client/src/app/settings/account/components/DeleteAccount.tsx b/client/src/app/settings/account/components/DeleteAccount.tsxindex 0f7929bf8..46554b0a2 100644--- a/client/src/app/settings/account/components/DeleteAccount.tsx+++ b/client/src/app/settings/account/components/DeleteAccount.tsx@@ -2,7 +2,6 @@import { useQueryClient } from "@tanstack/react-query";import { AlertTriangle } from "lucide-react";-import { useRouter } from "next/navigation";import { useState } from "react";import { toast } from "sonner";import {@@ -24,7 +23,6 @@ export function DeleteAccount() {const { data: subscription } = useStripeSubscription();const [isDeleting, setIsDeleting] = useState(false);const [isOpen, setIsOpen] = useState(false);- const router = useRouter();const queryClient = useQueryClient();const handleAccountDeletion = async () => {diff --git a/client/src/app/settings/organization/members/components/DeleteOrganizationDialog.tsx b/client/src/app/settings/organization/members/components/DeleteOrganizationDialog.tsxindex a47c0dcd4..bbd8600ae 100644--- a/client/src/app/settings/organization/members/components/DeleteOrganizationDialog.tsx+++ b/client/src/app/settings/organization/members/components/DeleteOrganizationDialog.tsx@@ -1,21 +1,26 @@"use client";+import { useQueryClient } from "@tanstack/react-query";+import { AlertTriangle } from "lucide-react";import { useState } from "react";+import { toast } from "sonner";+import {+ AlertDialog,+ AlertDialogAction,+ AlertDialogCancel,+ AlertDialogContent,+ AlertDialogDescription,+ AlertDialogFooter,+ AlertDialogHeader,+ AlertDialogTitle,+ AlertDialogTrigger,+} from "@/components/ui/alert-dialog";import { Button } from "@/components/ui/button";-import { Dialog } from "@/components/ui/dialog";-import { DialogContent } from "@/components/ui/dialog";-import { DialogHeader } from "@/components/ui/dialog";-import { DialogTitle } from "@/components/ui/dialog";-import { DialogTrigger } from "@/components/ui/dialog";-import { DialogFooter } from "@/components/ui/dialog";-import { DialogDescription } from "@/components/ui/dialog";import { Input } from "@/components/ui/input";-import { AlertTriangle, Trash } from "lucide-react";import { authClient } from "@/lib/auth";-import { toast } from "sonner";-import { Organization } from "../page";+import { useStripeSubscription } from "@/lib/subscription/useStripeSubscription";import { USER_ORGANIZATIONS_QUERY_KEY } from "../../../../../api/admin/organizations";-import { useQueryClient } from "@tanstack/react-query";+import { Organization } from "../page";interface DeleteOrganizationDialogProps {organization: Organization;@@ -23,18 +28,22 @@ interface DeleteOrganizationDialogProps {}export function DeleteOrganizationDialog({ organization, onSuccess }: DeleteOrganizationDialogProps) {- const [isLoading, setIsLoading] = useState(false);- const [open, setOpen] = useState(false);+ const { data: subscription } = useStripeSubscription();+ const [isDeleting, setIsDeleting] = useState(false);+ const [isOpen, setIsOpen] = useState(false);const [confirmText, setConfirmText] = useState("");const queryClient = useQueryClient();+ const hasActiveSubscription =+ subscription?.planName.startsWith("standard") || subscription?.planName.startsWith("pro");+const handleDelete = async () => {if (confirmText !== organization.name) {toast.error("Please type the organization name to confirm deletion");return;}- setIsLoading(true);+ setIsDeleting(true);try {await authClient.organization.delete({organizationId: organization.id,@@ -45,56 +54,73 @@ export function DeleteOrganizationDialog({ organization, onSuccess }: DeleteOrgaauthClient.organization.setActive({organizationId: null,});- setOpen(false);+ setIsOpen(false);onSuccess();} catch (error: any) {toast.error(error.message || "Failed to delete organization");} finally {- setIsLoading(false);+ setIsDeleting(false);}};+ const handleClose = () => {+ setIsOpen(false);+ setConfirmText("");+ };++ const canDelete = !hasActiveSubscription && confirmText === organization.name && !isDeleting;+return (- <Dialog open={open} onOpenChange={setOpen}>- <DialogTrigger asChild>- <Button size="sm" variant="destructive">- <Trash className="h-4 w-4 mr-1" />- Delete+ <AlertDialog open={isOpen} onOpenChange={setIsOpen}>+ <AlertDialogTrigger asChild>+ <Button variant="destructive" className="w-full" onClick={() => setIsOpen(true)}>+ Delete Organization</Button>- </DialogTrigger>- <DialogContent className="max-w-lg">- <DialogHeader>- <DialogTitle className="text-red-400 flex items-center gap-2">- <AlertTriangle className="h-5 w-5" />- Delete Organization- </DialogTitle>- <DialogDescription>- This action cannot be undone. This will permanently delete the organization and remove all associated data.- </DialogDescription>- </DialogHeader>- <div className="grid gap-4 py-4">- <p>- Please type <strong>{organization.name}</strong> to confirm.- </p>- <Input- value={confirmText}- onChange={(e: React.ChangeEvent<HTMLInputElement>) => setConfirmText(e.target.value)}- placeholder={organization.name}- />- </div>- <DialogFooter>- <Button variant="outline" onClick={() => setOpen(false)}>+ </AlertDialogTrigger>+ <AlertDialogContent>+ <AlertDialogHeader>+ <AlertDialogTitle className="flex items-center gap-2">+ <AlertTriangle className="h-5 w-5" color="hsl(var(--red-500))" />+ {hasActiveSubscription ? "Cannot delete organization" : "Delete your organization?"}+ </AlertDialogTitle>+ <AlertDialogDescription>+ {hasActiveSubscription+ ? "You have an active subscription. Please cancel your subscription before deleting your organization."+ : "This action cannot be undone. This will permanently delete the organization and remove all associated data."}+ </AlertDialogDescription>+ </AlertDialogHeader>++ {!hasActiveSubscription && (+ <div className="py-4">+ <p className="text-sm mb-2">+ Please type <strong>{organization.name}</strong> to confirm.+ </p>+ <Input+ value={confirmText}+ onChange={(e) => setConfirmText(e.target.value)}+ placeholder={organization.name}+ />+ </div>+ )}++ <AlertDialogFooter>+ <AlertDialogCancel onClick={handleClose} disabled={isDeleting}>Cancel- </Button>- <Button- variant="destructive"- onClick={handleDelete}- disabled={isLoading || confirmText !== organization.name}- >- {isLoading ? "Deleting..." : "Delete Organization"}- </Button>- </DialogFooter>- </DialogContent>- </Dialog>+ </AlertDialogCancel>+ {!hasActiveSubscription && (+ <AlertDialogAction+ onClick={(e) => {+ e.preventDefault();+ handleDelete();+ }}+ variant="destructive"+ disabled={!canDelete}+ >+ {isDeleting ? "Deleting..." : "Delete Organization"}+ </AlertDialogAction>+ )}+ </AlertDialogFooter>+ </AlertDialogContent>+ </AlertDialog>);}diff --git a/client/src/app/settings/organization/members/page.tsx b/client/src/app/settings/organization/members/page.tsxindex 790d55c6b..1d302ed4e 100644--- a/client/src/app/settings/organization/members/page.tsx+++ b/client/src/app/settings/organization/members/page.tsx@@ -14,6 +14,7 @@ import { Input } from "../../../../components/ui/input";import { useSetPageTitle } from "../../../../hooks/useSetPageTitle";import { IS_CLOUD } from "../../../../lib/const";import { CreateUserDialog } from "./components/CreateUserDialog";+import { DeleteOrganizationDialog } from "./components/DeleteOrganizationDialog";import { Invitations } from "./components/Invitations";import { InviteMemberDialog } from "./components/InviteMemberDialog";import { RemoveMemberDialog } from "./components/RemoveMemberDialog";@@ -117,7 +118,13 @@ function Organization({</Button></div></div>- {/* <DeleteOrganizationDialog organization={org} onSuccess={handleRefresh} /> */}+ <div className="pt-4 border-t mt-4 space-y-2">+ <h4 className="text-sm font-medium">Delete Organization</h4>+ <p className="text-xs text-neutral-500">Permanently delete this organization and all its data</p>+ <div className="w-[200px]">+ <DeleteOrganizationDialog organization={org} onSuccess={handleRefresh} />+ </div>+ </div></CardContent></Card>)}diff --git a/client/src/components/ui/alert-dialog.tsx b/client/src/components/ui/alert-dialog.tsxindex 468f7d9f8..3728801be 100644--- a/client/src/components/ui/alert-dialog.tsx+++ b/client/src/components/ui/alert-dialog.tsx@@ -6,96 +6,101 @@ import * as AlertDialogPrimitive from "@radix-ui/react-alert-dialog";import { cn } from "@/lib/utils";import { buttonVariants } from "@/components/ui/button";-const AlertDialog = AlertDialogPrimitive.Root;+function AlertDialog({ ...props }: React.ComponentProps<typeof AlertDialogPrimitive.Root>) {+ return <AlertDialogPrimitive.Root data-slot="alert-dialog" {...props} />;+}-const AlertDialogTrigger = AlertDialogPrimitive.Trigger;+function AlertDialogTrigger({ ...props }: React.ComponentProps<typeof AlertDialogPrimitive.Trigger>) {+ return <AlertDialogPrimitive.Trigger data-slot="alert-dialog-trigger" {...props} />;+}-const AlertDialogPortal = AlertDialogPrimitive.Portal;+function AlertDialogPortal({ ...props }: React.ComponentProps<typeof AlertDialogPrimitive.Portal>) {+ return <AlertDialogPrimitive.Portal data-slot="alert-dialog-portal" {...props} />;+}-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}+function AlertDialogOverlay({ className, ...props }: React.ComponentProps<typeof AlertDialogPrimitive.Overlay>) {+ return (+ <AlertDialogPrimitive.Overlay+ data-slot="alert-dialog-overlay"className={cn(- "fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border border-neutral-150 bg-white 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 md:w-full dark:border-neutral-800 dark:bg-neutral-950",+ "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 fixed inset-0 z-50 bg-black/50",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";+function AlertDialogContent({ className, ...props }: React.ComponentProps<typeof AlertDialogPrimitive.Content>) {+ return (+ <AlertDialogPortal>+ <AlertDialogOverlay />+ <AlertDialogPrimitive.Content+ data-slot="alert-dialog-content"+ className={cn(+ "bg-background 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 fixed top-[50%] left-[50%] z-50 grid w-full max-w-[calc(100%-2rem)] translate-x-[-50%] translate-y-[-50%] gap-4 rounded-lg border p-6 shadow-lg duration-200 sm:max-w-lg",+ className+ )}+ {...props}+ />+ </AlertDialogPortal>+ );+}-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";+function AlertDialogHeader({ className, ...props }: React.ComponentProps<"div">) {+ return (+ <div+ data-slot="alert-dialog-header"+ className={cn("flex flex-col gap-2 text-center sm:text-left", className)}+ {...props}+ />+ );+}++function AlertDialogFooter({ className, ...props }: React.ComponentProps<"div">) {+ return (+ <div+ data-slot="alert-dialog-footer"+ className={cn("flex flex-col-reverse gap-2 sm:flex-row sm:justify-end", className)}+ {...props}+ />+ );+}-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;+function AlertDialogTitle({ className, ...props }: React.ComponentProps<typeof AlertDialogPrimitive.Title>) {+ return (+ <AlertDialogPrimitive.Title+ data-slot="alert-dialog-title"+ className={cn("text-lg font-semibold", className)}+ {...props}+ />+ );+}-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-neutral-500 dark:text-neutral-400", className)}- {...props}- />-));-AlertDialogDescription.displayName = AlertDialogPrimitive.Description.displayName;+function AlertDialogDescription({+ className,+ ...props+}: React.ComponentProps<typeof AlertDialogPrimitive.Description>) {+ return (+ <AlertDialogPrimitive.Description+ data-slot="alert-dialog-description"+ className={cn("text-muted-foreground text-sm", className)}+ {...props}+ />+ );+}-const AlertDialogAction = React.forwardRef<- React.ElementRef<typeof AlertDialogPrimitive.Action>,- React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Action> & {- variant?: "default" | "destructive" | "outline" | "secondary" | "ghost" | "link" | "accent" | "warning";- }->(({ className, variant = "default", ...props }, ref) => (- <AlertDialogPrimitive.Action ref={ref} className={cn(buttonVariants({ variant }), className)} {...props} />-));-AlertDialogAction.displayName = AlertDialogPrimitive.Action.displayName;+function AlertDialogAction({+ className,+ variant,+ ...props+}: React.ComponentProps<typeof AlertDialogPrimitive.Action> & { variant?: "default" | "destructive" | "outline" | "secondary" | "ghost" | "link" }) {+ return <AlertDialogPrimitive.Action className={cn(buttonVariants({ variant }), className)} {...props} />;+}-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;+function AlertDialogCancel({ className, ...props }: React.ComponentProps<typeof AlertDialogPrimitive.Cancel>) {+ return <AlertDialogPrimitive.Cancel className={cn(buttonVariants({ variant: "outline" }), className)} {...props} />;+}export {AlertDialog,