|
| 1 | +import { LOCALSTORAGE_KEYS } from "@/web/lib/localstorage-keys"; |
| 2 | +import { KEYS } from "@/web/lib/query-keys"; |
| 3 | +import { track } from "@/web/lib/posthog-client"; |
| 4 | +import { |
| 5 | + SELF_MCP_ALIAS_ID, |
| 6 | + useMCPClient, |
| 7 | + useProjectContext, |
| 8 | +} from "@decocms/mesh-sdk"; |
| 9 | +import { |
| 10 | + AlertDialog, |
| 11 | + AlertDialogAction, |
| 12 | + AlertDialogCancel, |
| 13 | + AlertDialogContent, |
| 14 | + AlertDialogDescription, |
| 15 | + AlertDialogFooter, |
| 16 | + AlertDialogHeader, |
| 17 | + AlertDialogTitle, |
| 18 | +} from "@deco/ui/components/alert-dialog.tsx"; |
| 19 | +import { Button } from "@deco/ui/components/button.tsx"; |
| 20 | +import { Input } from "@deco/ui/components/input.tsx"; |
| 21 | +import { useMutation, useQueryClient } from "@tanstack/react-query"; |
| 22 | +import { useNavigate } from "@tanstack/react-router"; |
| 23 | +import { |
| 24 | + SettingsCard, |
| 25 | + SettingsCardItem, |
| 26 | + SettingsSection, |
| 27 | +} from "@/web/components/settings/settings-section"; |
| 28 | +import { useState } from "react"; |
| 29 | +import { toast } from "sonner"; |
| 30 | + |
| 31 | +export function DeleteOrganizationSection() { |
| 32 | + const { org } = useProjectContext(); |
| 33 | + const navigate = useNavigate(); |
| 34 | + const queryClient = useQueryClient(); |
| 35 | + const [confirmOpen, setConfirmOpen] = useState(false); |
| 36 | + const [confirmName, setConfirmName] = useState(""); |
| 37 | + |
| 38 | + const selfClient = useMCPClient({ |
| 39 | + connectionId: SELF_MCP_ALIAS_ID, |
| 40 | + orgId: org.id, |
| 41 | + }); |
| 42 | + |
| 43 | + const deleteMutation = useMutation({ |
| 44 | + mutationFn: async () => { |
| 45 | + const result = await selfClient.callTool({ |
| 46 | + name: "ORGANIZATION_DELETE", |
| 47 | + arguments: { id: org.id }, |
| 48 | + }); |
| 49 | + if (result.isError) { |
| 50 | + const content = result.content; |
| 51 | + const text = |
| 52 | + Array.isArray(content) && |
| 53 | + content[0]?.type === "text" && |
| 54 | + typeof content[0].text === "string" |
| 55 | + ? content[0].text |
| 56 | + : "Failed to delete organization"; |
| 57 | + throw new Error(text); |
| 58 | + } |
| 59 | + }, |
| 60 | + onSuccess: () => { |
| 61 | + track("organization_deleted", { organization_id: org.id }); |
| 62 | + |
| 63 | + // Drop the cached slug so homeRoute doesn't try to redirect us back here |
| 64 | + if (localStorage.getItem(LOCALSTORAGE_KEYS.lastOrgSlug()) === org.slug) { |
| 65 | + localStorage.removeItem(LOCALSTORAGE_KEYS.lastOrgSlug()); |
| 66 | + } |
| 67 | + |
| 68 | + // Drop active-org caches that might still hold the archived org |
| 69 | + queryClient.removeQueries({ |
| 70 | + queryKey: KEYS.activeOrganization(org.slug), |
| 71 | + }); |
| 72 | + queryClient.invalidateQueries({ queryKey: KEYS.organizations() }); |
| 73 | + |
| 74 | + toast.success("Organization deleted"); |
| 75 | + // homeRoute redirects to next available org or onboarding |
| 76 | + navigate({ to: "/" }); |
| 77 | + }, |
| 78 | + onError: (error) => { |
| 79 | + toast.error( |
| 80 | + error instanceof Error |
| 81 | + ? error.message |
| 82 | + : "Failed to delete organization", |
| 83 | + ); |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + return ( |
| 88 | + <> |
| 89 | + <SettingsSection |
| 90 | + title="Danger Zone" |
| 91 | + description="Irreversible actions that affect your entire organization." |
| 92 | + > |
| 93 | + <SettingsCard className="border-destructive/40"> |
| 94 | + <SettingsCardItem |
| 95 | + title="Delete organization" |
| 96 | + description="Permanently delete this organization and all of its data. This action cannot be undone." |
| 97 | + action={ |
| 98 | + <Button |
| 99 | + variant="destructive" |
| 100 | + size="sm" |
| 101 | + onClick={() => setConfirmOpen(true)} |
| 102 | + disabled={deleteMutation.isPending} |
| 103 | + > |
| 104 | + Delete |
| 105 | + </Button> |
| 106 | + } |
| 107 | + /> |
| 108 | + </SettingsCard> |
| 109 | + </SettingsSection> |
| 110 | + |
| 111 | + <AlertDialog |
| 112 | + open={confirmOpen} |
| 113 | + onOpenChange={(open) => { |
| 114 | + setConfirmOpen(open); |
| 115 | + if (!open) setConfirmName(""); |
| 116 | + }} |
| 117 | + > |
| 118 | + <AlertDialogContent> |
| 119 | + <AlertDialogHeader> |
| 120 | + <AlertDialogTitle>Delete Organization?</AlertDialogTitle> |
| 121 | + <AlertDialogDescription asChild> |
| 122 | + <div> |
| 123 | + <p> |
| 124 | + This will permanently delete all data associated with{" "} |
| 125 | + <span className="font-medium text-foreground"> |
| 126 | + {org.name} |
| 127 | + </span> |
| 128 | + . This action cannot be undone. |
| 129 | + </p> |
| 130 | + <p className="mt-3 mb-1.5"> |
| 131 | + Type{" "} |
| 132 | + <span className="font-medium text-foreground"> |
| 133 | + {org.name} |
| 134 | + </span>{" "} |
| 135 | + to confirm: |
| 136 | + </p> |
| 137 | + <Input |
| 138 | + value={confirmName} |
| 139 | + onChange={(e) => setConfirmName(e.target.value)} |
| 140 | + placeholder={org.name} |
| 141 | + autoFocus |
| 142 | + /> |
| 143 | + </div> |
| 144 | + </AlertDialogDescription> |
| 145 | + </AlertDialogHeader> |
| 146 | + <AlertDialogFooter> |
| 147 | + <AlertDialogCancel>Cancel</AlertDialogCancel> |
| 148 | + <AlertDialogAction |
| 149 | + onClick={() => deleteMutation.mutate()} |
| 150 | + disabled={confirmName !== org.name || deleteMutation.isPending} |
| 151 | + className="bg-destructive text-destructive-foreground hover:bg-destructive/90 disabled:opacity-50" |
| 152 | + > |
| 153 | + {deleteMutation.isPending ? "Deleting…" : "Delete organization"} |
| 154 | + </AlertDialogAction> |
| 155 | + </AlertDialogFooter> |
| 156 | + </AlertDialogContent> |
| 157 | + </AlertDialog> |
| 158 | + </> |
| 159 | + ); |
| 160 | +} |
0 commit comments