Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions apps/cursor/src/actions/delete-company.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"use server";

import { revalidatePath } from "next/cache";
import { z } from "zod";
import { createClient } from "@/utils/supabase/server";
import { ActionError, authActionClient } from "./safe-action";

export const deleteCompanyAction = authActionClient
.metadata({
actionName: "delete-company",
})
.schema(
z.object({
id: z.string(),
slug: z.string(),
}),
)
.action(async ({ parsedInput: { id, slug }, ctx: { userId } }) => {
const supabase = await createClient();

const { data: company, error: companyError } = await supabase
.from("companies")
.select("id, owner_id")
.eq("id", id)
.single();

if (companyError || !company) {
throw new ActionError("Company not found.");
}

if (company.owner_id !== userId) {
throw new ActionError("You do not have permission to delete this company.");
}

const { error: unlinkMcpsError } = await supabase
.from("mcps")
.update({ company_id: null })
.eq("company_id", id)
.eq("owner_id", userId);

if (unlinkMcpsError) {
throw new ActionError(`Failed to unlink MCPs: ${unlinkMcpsError.message}`);
}

const { error: deleteError } = await supabase
.from("companies")
.delete()
.eq("id", id)
.eq("owner_id", userId);

if (deleteError) {
throw new ActionError(`Failed to delete company: ${deleteError.message}`);
}

revalidatePath("/companies");
revalidatePath(`/c/${slug}`);
revalidatePath("/");

return { success: true };
});
4 changes: 3 additions & 1 deletion apps/cursor/src/components/company/company-header.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { DeleteCompanyButton } from "./delete-company-button";
import { EditCompanyModal } from "../modals/edit-company-modal";

export function CompanyHeader({
Expand Down Expand Up @@ -47,7 +48,8 @@ export function CompanyHeader({
</div>

{isOwner && (
<div className="self-start md:ml-auto md:self-center">
<div className="flex self-start gap-2 md:ml-auto md:self-center">
<DeleteCompanyButton id={id} slug={slug} />
<EditCompanyModal
data={{
id,
Expand Down
52 changes: 52 additions & 0 deletions apps/cursor/src/components/company/delete-company-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use client";

import { useAction } from "next-safe-action/hooks";
import { useRouter } from "next/navigation";
import { toast } from "sonner";
import { deleteCompanyAction } from "@/actions/delete-company";
import { Button } from "@/components/ui/button";

export function DeleteCompanyButton({
id,
slug,
}: {
id: string;
slug: string;
}) {
const router = useRouter();

const { execute, isExecuting } = useAction(deleteCompanyAction, {
onSuccess: () => {
toast.success("Company deleted.");
router.push("/companies");
router.refresh();
},
onError: ({ error }) => {
toast.error(error.serverError ?? "Failed to delete company.");
},
});

const onDelete = () => {
const confirmed = window.confirm(
"Delete this company? This cannot be undone.",
);

if (!confirmed) {
return;
}

execute({ id, slug });
};

return (
<Button
type="button"
variant="destructive"
className="h-8 rounded-full"
onClick={onDelete}
disabled={isExecuting}
>
{isExecuting ? "Deleting..." : "Delete Company"}
</Button>
);
}