🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
refactor(web): Clean up `DeleteProjectButton` (#16669)
Ben Bachema345719ee083
diff --git a/web/src/features/projects/components/DeleteProjectButton.tsx b/web/src/features/projects/components/DeleteProjectButton.tsxdeleted file mode 100644index 10551144bf..0000000000--- a/web/src/features/projects/components/DeleteProjectButton.tsx+++ /dev/null@@ -1,120 +0,0 @@-/* eslint-disable @repo/no-abstracted-overlay-trigger */-import { Button } from "@/src/components/ui/button";-import {- Dialog,- DialogBody,- DialogContent,- DialogFooter,- 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 { api, reportNonTrpcError } from "@/src/utils/api";-import * as z from "zod";-import { useForm } from "react-hook-form";-import { zodResolver } from "@hookform/resolvers/zod";-import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";-import { useQueryProject } from "@/src/features/projects/hooks";-import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";-import { env } from "@/src/env.mjs";--export function DeleteProjectButton() {- const capture = usePostHogClientCapture();-- //code for dynamic confirmation message- const { project, organization } = useQueryProject();- const confirmMessage = (organization?.name + "/" + project?.name)- .replaceAll(" ", "-")- .toLowerCase();-- const formSchema = z.object({- name: z.string().includes(confirmMessage, {- message: `Please confirm with "${confirmMessage}"`,- }),- });-- const hasAccess = useHasProjectAccess({- projectId: project?.id,- scope: "project:delete",- });-- const deleteProject = api.projects.delete.useMutation();-- const form = useForm({- resolver: zodResolver(formSchema),- defaultValues: {- name: "",- },- });-- // delete project functionality- const onSubmit = () => {- if (!project) return;- capture("project_settings:project_delete");- deleteProject- .mutateAsync({- projectId: project.id,- })- .then(() => {- window.location.href = env.NEXT_PUBLIC_BASE_PATH ?? "/"; // browser reload to refresh jwt- })- .catch((error) => reportNonTrpcError(error, "projects"));- };-- return (- <Dialog>- <DialogTrigger asChild>- <Button variant="destructive-secondary" disabled={!hasAccess}>- Delete Project- </Button>- </DialogTrigger>- <DialogContent className="sm:max-w-[425px]">- <DialogHeader>- <DialogTitle className="text-lg font-bold">- Delete Project- </DialogTitle>- <DialogDescription className=" ">- {`To confirm, type "${confirmMessage}" in the input box `}- </DialogDescription>- </DialogHeader>- <Form {...form}>- <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">- <DialogBody>- <FormField- control={form.control}- name="name"- render={({ field }) => (- <FormItem>- <FormControl>- <Input placeholder={confirmMessage} {...field} />- </FormControl>- <FormMessage />- </FormItem>- )}- />- </DialogBody>- <DialogFooter>- <Button- type="submit"- variant="destructive"- loading={deleteProject.isPending}- className="w-full"- >- Delete project- </Button>- </DialogFooter>- </form>- </Form>- </DialogContent>- </Dialog>- );-}diff --git a/web/src/features/projects/components/DeleteProjectDialog.stories.tsx b/web/src/features/projects/components/DeleteProjectDialog.stories.tsxnew file mode 100644index 0000000000..73d795818a--- /dev/null+++ b/web/src/features/projects/components/DeleteProjectDialog.stories.tsx@@ -0,0 +1,61 @@+import { expect, fn, userEvent, within } from "storybook/test";++import { Dialog, DialogContent } from "@/src/components/ui/dialog";+import preview from "../../../../.storybook/preview";+import {+ DeleteProjectDialog,+ type DeleteProjectDialogProps,+} from "./DeleteProjectDialog";++const meta = preview.meta({+ component: DeleteProjectDialog,+});++export default meta;++const renderDialog = (args: DeleteProjectDialogProps) => (+ <Dialog open onOpenChange={fn()}>+ <DialogContent className="sm:max-w-[425px]">+ <DeleteProjectDialog {...args} />+ </DialogContent>+ </Dialog>+);++export const Default = meta.story({+ args: {+ confirmMessage: "acme/my-project",+ isPending: false,+ onSubmit: fn(),+ },+ render: renderDialog,+});++export const Loading = meta.story({+ args: {+ confirmMessage: "acme/my-project",+ isPending: true,+ onSubmit: fn(),+ },+ render: renderDialog,+});++export const ConfirmsDeletion = meta.story({+ name: "(Test) Confirms deletion",+ args: {+ confirmMessage: "acme/my-project",+ isPending: false,+ onSubmit: fn(),+ },+ render: renderDialog,+ play: async ({ args, canvasElement }) => {+ const body = within(canvasElement.ownerDocument.body);++ await userEvent.type(+ body.getByPlaceholderText("acme/my-project"),+ "acme/my-project",+ );+ await userEvent.click(body.getByRole("button", { name: "Delete project" }));++ await expect(args.onSubmit).toHaveBeenCalledOnce();+ },+});diff --git a/web/src/features/projects/components/DeleteProjectDialog.tsx b/web/src/features/projects/components/DeleteProjectDialog.tsxnew file mode 100644index 0000000000..df8b5a5e63--- /dev/null+++ b/web/src/features/projects/components/DeleteProjectDialog.tsx@@ -0,0 +1,88 @@+import { zodResolver } from "@hookform/resolvers/zod";+import { useForm } from "react-hook-form";+import * as z from "zod";++import { Button } from "@/src/components/ui/button";+import {+ DialogBody,+ DialogDescription,+ DialogFooter,+ DialogHeader,+ DialogTitle,+} from "@/src/components/ui/dialog";+import {+ Form,+ FormControl,+ FormField,+ FormItem,+ FormMessage,+} from "@/src/components/ui/form";+import { Input } from "@/src/components/ui/input";++type DeleteProjectForm = {+ name: string;+};++export type DeleteProjectDialogProps = {+ confirmMessage: string;+ isPending: boolean;+ onSubmit: () => void;+};++export function DeleteProjectDialog({+ confirmMessage,+ isPending,+ onSubmit,+}: DeleteProjectDialogProps) {+ const formSchema = z.object({+ name: z.string().includes(confirmMessage, {+ message: `Please confirm with "${confirmMessage}"`,+ }),+ });++ const form = useForm<DeleteProjectForm>({+ resolver: zodResolver(formSchema),+ defaultValues: {+ name: "",+ },+ });++ return (+ <>+ <DialogHeader>+ <DialogTitle className="text-lg font-bold">Delete Project</DialogTitle>+ <DialogDescription>+ {`To confirm, type "${confirmMessage}" in the input box`}+ </DialogDescription>+ </DialogHeader>+ <Form {...form}>+ <form onSubmit={form.handleSubmit(onSubmit)}>+ <DialogBody>+ <FormField+ control={form.control}+ name="name"+ render={({ field }) => (+ <FormItem>+ <FormControl>+ <Input placeholder={confirmMessage} {...field} />+ </FormControl>+ <FormMessage />+ </FormItem>+ )}+ />+ </DialogBody>+ <DialogFooter>+ <Button+ type="submit"+ variant="destructive"+ loading={isPending}+ className="w-full"+ >+ Delete project+ </Button>+ </DialogFooter>+ </form>+ </Form>+ </>+ );+}diff --git a/web/src/features/projects/components/DeleteProjectDialogController.tsx b/web/src/features/projects/components/DeleteProjectDialogController.tsxnew file mode 100644index 0000000000..c50ac55ec0--- /dev/null+++ b/web/src/features/projects/components/DeleteProjectDialogController.tsx@@ -0,0 +1,64 @@+import { type ReactNode } from "react";++import {+ DialogController,+ type DialogTrigger,+} from "@/src/components/ui/dialog";+import { env } from "@/src/env.mjs";+import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";+import { useQueryProject } from "@/src/features/projects/hooks";+import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";+import { api, reportNonTrpcError } from "@/src/utils/api";+import { DeleteProjectDialog } from "./DeleteProjectDialog";++type DeleteProjectDialogControllerProps = {+ children: (control: {+ hasAccess: boolean;+ Trigger: typeof DialogTrigger;+ }) => ReactNode;+};++export function DeleteProjectDialogController({+ children,+}: DeleteProjectDialogControllerProps) {+ const capture = usePostHogClientCapture();+ const { project, organization } = useQueryProject();+ const confirmMessage = `${organization?.name}/${project?.name}`+ .replaceAll(" ", "-")+ .toLowerCase();+ const hasAccess = useHasProjectAccess({+ projectId: project?.id,+ scope: "project:delete",+ });+ const deleteProject = api.projects.delete.useMutation();++ const handleDelete = () => {+ if (!project) return;++ capture("project_settings:project_delete");+ deleteProject+ .mutateAsync({+ projectId: project.id,+ })+ .then(() => {+ window.location.href = env.NEXT_PUBLIC_BASE_PATH ?? "/";+ })+ .catch((error) => reportNonTrpcError(error, "projects"));+ };++ return (+ <DialogController+ closeOnInteractionOutside={false}+ size="default"+ renderContent={() => (+ <DeleteProjectDialog+ confirmMessage={confirmMessage}+ isPending={deleteProject.isPending}+ onSubmit={handleDelete}+ />+ )}+ >+ {({ Trigger }) => children({ hasAccess, Trigger })}+ </DialogController>+ );+}diff --git a/web/src/pages/project/[projectId]/settings/index.tsx b/web/src/pages/project/[projectId]/settings/index.tsxindex f211a7ce93..082a6b4d05 100644--- a/web/src/pages/project/[projectId]/settings/index.tsx+++ b/web/src/pages/project/[projectId]/settings/index.tsx@@ -1,6 +1,6 @@import Header from "@/src/components/layouts/header";import { ApiKeyList } from "@/src/features/public-api/components/ApiKeyList";-import { DeleteProjectButton } from "@/src/features/projects/components/DeleteProjectButton";+import { DeleteProjectDialogController } from "@/src/features/projects/components/DeleteProjectDialogController";import { HostNameProject } from "@/src/features/projects/components/HostNameProject";import RenameProject from "@/src/features/projects/components/RenameProject";import { Button } from "@/src/components/ui/button";@@ -141,7 +141,20 @@ export const getProjectSettingsPages = ({title: "Delete this project",description:"Once you delete a project, there is no going back. Please be certain.",- button: <DeleteProjectButton />,+ button: (+ <DeleteProjectDialogController>+ {({ hasAccess, Trigger }) => (+ <Trigger asChild>+ <Button+ variant="destructive-secondary"+ disabled={!hasAccess}+ >+ Delete Project+ </Button>+ </Trigger>+ )}+ </DeleteProjectDialogController>+ ),},]}/>