Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

Feat: delete project button (#421)

Diwakar Kashyap15588041918a
diff --git a/src/features/projects/components/DeleteProjectButton.tsx b/src/features/projects/components/DeleteProjectButton.tsx
new file mode 100644
index 0000000000..a9266c0086
--- /dev/null
+++ b/src/features/projects/components/DeleteProjectButton.tsx
@@ -0,0 +1,139 @@
+import { Button } from "@/src/components/ui/button";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogHeader,
+ DialogTitle,
+ DialogTrigger,
+} from "@/src/components/ui/dialog";
+import {
+ Form,
+ FormControl,
+ FormField,
+ FormItem,
+ FormMessage,
+} from "@/src/components/ui/form";
+import { Input } from "@/src/components/ui/input";
+import { useSession } from "next-auth/react";
+import { api } from "@/src/utils/api";
+import { useHasAccess } from "@/src/features/rbac/utils/checkAccess";
+import { usePostHog } from "posthog-js/react";
+import { useRouter } from "next/router";
+import * as z from "zod";
+import { useForm } from "react-hook-form";
+import { zodResolver } from "@hookform/resolvers/zod";
+
+export function DeleteProjectButton(props: { projectId: string }) {
+ const utils = api.useContext();
+ const router = useRouter();
+ const session = useSession();
+ const posthog = usePostHog();
+
+ //code for dynamic confirmation message
+ const userInfo = session?.data?.user;
+ const currentProject = userInfo?.projects?.find(
+ (project) => project.id == props.projectId,
+ );
+ const confirmMessage =
+ userInfo?.name?.replace(" ", "-") +
+ "/" +
+ currentProject?.name?.replace(" ", "-");
+
+ const formSchema = z.object({
+ name: z.string().includes(confirmMessage, {
+ message: `Please confirm with "${confirmMessage}"`,
+ }),
+ });
+
+ const hasAccess = useHasAccess({
+ projectId: props.projectId,
+ scope: "project:delete",
+ });
+
+ const deleteProject = api.projects.delete.useMutation({
+ onSuccess: () => {
+ void utils.projects.invalidate();
+ void router.push("/");
+ },
+ });
+
+ const form = useForm<z.infer<typeof formSchema>>({
+ resolver: zodResolver(formSchema),
+ defaultValues: {
+ name: "",
+ },
+ });
+
+ // delete project functionality
+ const onSubmit = () => {
+ deleteProject
+ .mutateAsync({
+ projectId: props.projectId,
+ })
+ .then(() => {
+ posthog.capture("project_settings:project_delete");
+ })
+ .catch((error) => {
+ console.error(error);
+ });
+ };
+
+ if (!hasAccess) return null;
+
+ return (
+ <div>
+ <h2 className="mb-6 text-base font-semibold leading-6 text-gray-900">
+ Danger Zone
+ </h2>
+ <Dialog>
+ <DialogTrigger asChild>
+ <Button variant="destructive">Delete Project</Button>
+ </DialogTrigger>
+ <DialogContent className="sm:max-w-[425px]">
+ <DialogHeader>
+ <DialogTitle className="text-lg font-semibold ">
+ Delete Project
+ </DialogTitle>
+ <DialogDescription className=" ">
+ {`To confirm, type "${confirmMessage}" in the input box `}
+ </DialogDescription>
+ </DialogHeader>
+ <Form {...form}>
+ <form
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
+ onSubmit={form.handleSubmit(onSubmit)}
+ className="space-y-8"
+ data-testid="new-project-form"
+ >
+ <FormField
+ control={form.control}
+ name="name"
+ render={({ field }) => (
+ <FormItem>
+ <FormControl>
+ <Input
+ placeholder="my-llm-project"
+ {...field}
+ data-testid="new-project-name-input"
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <Button
+ type="submit"
+ variant="destructive"
+ loading={deleteProject.isLoading}
+ className="w-full"
+ >
+ Delete project
+ </Button>
+ </form>
+ </Form>
+ </DialogContent>
+ </Dialog>
+ </div>
+ );
+}
diff --git a/src/features/projects/server/projectsRouter.ts b/src/features/projects/server/projectsRouter.ts
index 7b6d7aad60..edf89017bf 100644
--- a/src/features/projects/server/projectsRouter.ts
+++ b/src/features/projects/server/projectsRouter.ts
@@ -1,5 +1,6 @@
import { createTRPCRouter, protectedProcedure } from "@/src/server/api/trpc";
import * as z from "zod";
+import { throwIfNoAccess } from "@/src/features/rbac/utils/checkAccess";
export const projectsRouter = createTRPCRouter({
all: protectedProcedure.query(async ({ ctx }) => {
@@ -44,4 +45,26 @@ export const projectsRouter = createTRPCRouter({
role: "OWNER",
};
}),
+
+ delete: protectedProcedure
+ .input(
+ z.object({
+ projectId: z.string(),
+ }),
+ )
+ .mutation(async ({ input, ctx }) => {
+ throwIfNoAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "project:delete",
+ });
+
+ await ctx.prisma.project.delete({
+ where: {
+ id: input.projectId,
+ },
+ });
+
+ return true;
+ }),
});
diff --git a/src/features/rbac/constants/roleAccessRights.ts b/src/features/rbac/constants/roleAccessRights.ts
index a6d8d4a337..6e53ad963e 100644
--- a/src/features/rbac/constants/roleAccessRights.ts
+++ b/src/features/rbac/constants/roleAccessRights.ts
@@ -12,6 +12,8 @@ const scopes = [
"traces:publish",
"scores:CUD",
+
+ "project:delete",
] as const;
// type string of all Resource:Action, e.g. "members:read"
@@ -27,6 +29,7 @@ export const roleAccessRights: Record<MembershipRole, Scope[]> = {
"apiKeys:delete",
"traces:publish",
"scores:CUD",
+ "project:delete",
],
ADMIN: [
"members:read",
diff --git a/src/pages/project/[projectId]/settings.tsx b/src/pages/project/[projectId]/settings.tsx
index 80eaa6ebbe..735bcb2d35 100644
--- a/src/pages/project/[projectId]/settings.tsx
+++ b/src/pages/project/[projectId]/settings.tsx
@@ -6,6 +6,7 @@ import { ApiKeyList } from "@/src/features/public-api/components/ApiKeyList";
import { useRouter } from "next/router";
import { Code, Bird, GraduationCap } from "lucide-react";
import { ProjectMembersTable } from "@/src/features/rbac/components/ProjectMembersTable";
+import { DeleteProjectButton } from "@/src/features/projects/components/DeleteProjectButton";
export default function SettingsPage() {
const router = useRouter();
@@ -17,6 +18,7 @@ export default function SettingsPage() {
<ProjectMembersTable projectId={projectId} />
<ApiKeyList projectId={projectId} />
<Instructions />
+ <DeleteProjectButton projectId={projectId} />
</div>
</div>
);