import { getFormProps, getInputProps, useForm } from "@conform-to/react"; import { parseWithZod } from "@conform-to/zod"; import { DialogClose } from "@radix-ui/react-dialog"; import { Form, useActionData, useLocation } from "@remix-run/react"; import { type ActionFunctionArgs } from "@remix-run/server-runtime"; import { z } from "zod"; import { ArchiveIcon } from "~/assets/icons/ArchiveIcon"; import { Button } from "~/components/primitives/Buttons"; import { Dialog, DialogContent, DialogHeader, DialogTrigger } from "~/components/primitives/Dialog"; import { FormButtons } from "~/components/primitives/FormButtons"; import { FormError } from "~/components/primitives/FormError"; import { Paragraph } from "~/components/primitives/Paragraph"; import { redirectWithErrorMessage, redirectWithSuccessMessage } from "~/models/message.server"; import { ArchiveBranchService } from "~/services/archiveBranch.server"; import { requireUserId } from "~/services/session.server"; import { branchesDevPath, branchesPath } from "~/utils/pathBuilder"; const ArchiveBranchOptions = z.object({ environmentId: z.string(), }); const schema = ArchiveBranchOptions.and( z.object({ redirectPath: z.string(), }) ); export async function action({ request }: ActionFunctionArgs) { const userId = await requireUserId(request); const formData = await request.formData(); const submission = parseWithZod(formData, { schema }); if (submission.status !== "success") { return redirectWithErrorMessage("/", request, "Invalid form data"); } const archiveBranchService = new ArchiveBranchService(); const result = await archiveBranchService.call( { type: "userMembership", userId }, { environmentId: submission.value.environmentId, } ); if (result.success) { return redirectWithSuccessMessage( result.branch.type === "DEVELOPMENT" ? branchesDevPath(result.organization, result.project, result.branch) : branchesPath(result.organization, result.project, result.branch), request, `Branch "${result.branch.branchName}" archived` ); } return redirectWithErrorMessage(submission.value.redirectPath, request, result.error); } export function ArchiveButton({ environment, disabled, }: { environment: { id: string; branchName: string }; disabled?: boolean; }) { const lastSubmission = useActionData(); const location = useLocation(); const [form, { environmentId, redirectPath }] = useForm({ id: "archive-branch", lastResult: lastSubmission as any, onValidate({ formData }) { return parseWithZod(formData, { schema }); }, shouldRevalidate: "onInput", }); return ( Archive "{environment.branchName}"
This will permanently make this branch{" "} read-only. You won't be able to trigger runs, execute runs, or use the API for this branch. You will still be able to view the branch and its associated runs. Once archived you can create a new branch with the same name. {form.errors} Archive branch } cancelButton={ } />
); }