Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

2.7 KB
Raw Blame
import { useForm } from "react-hook-form";

import { createNotification } from "@app/components/notifications";
import {
  AlertDialog,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  Button,
  Field,
  FieldLabel,
  TextArea
} from "@app/components/v3";
import { useRequestProjectAccess } from "@app/hooks/api";
import { Project } from "@app/hooks/api/projects/types";

type ContentProps = {
  projectId: string;
  onComplete: () => void;
};

const Content = ({ projectId, onComplete }: ContentProps) => {
  const form = useForm<{ note: string }>();
  const requestProjectAccess = useRequestProjectAccess();

  const onFormSubmit = ({ note }: { note: string }) => {
    if (requestProjectAccess.isPending) return;
    requestProjectAccess.mutate(
      {
        comment: note.trim() || undefined,
        projectId
      },
      {
        onSuccess: () => {
          createNotification({
            type: "success",
            title: "Access Request Sent",
            text: "Admins will receive an email of your request"
          });
          onComplete();
        }
      }
    );
  };

  return (
    <form onSubmit={form.handleSubmit(onFormSubmit)} className="flex flex-col gap-4">
      <Field>
        <FieldLabel htmlFor="note">Note</FieldLabel>
        <TextArea id="note" rows={3} {...form.register("note")} />
      </Field>
      <AlertDialogFooter>
        <AlertDialogCancel>Cancel</AlertDialogCancel>
        <Button type="submit" size="sm" variant="project" isPending={form.formState.isSubmitting}>
          Submit Request
        </Button>
      </AlertDialogFooter>
    </form>
  );
};

type RequestProjectAccessModalProps = {
  isOpen: boolean;
  onOpenChange: (isOpen: boolean) => void;
  project?: Project;
  onComplete?: () => void;
  title?: string;
  subTitle?: string;
};

export const RequestProjectAccessModal = ({
  isOpen,
  onOpenChange,
  project,
  onComplete,
  title = "Confirm Access Request",
  subTitle
}: RequestProjectAccessModalProps) => {
  if (!project) return null;

  return (
    <AlertDialog open={isOpen} onOpenChange={onOpenChange}>
      <AlertDialogContent className="sm:max-w-xl!">
        <AlertDialogHeader>
          <AlertDialogTitle>{title}</AlertDialogTitle>
          <AlertDialogDescription>
            {subTitle ??
              `Requesting access to project ${project.name}. You may include an optional note for project admins to review your request.`}
          </AlertDialogDescription>
        </AlertDialogHeader>
        <Content
          projectId={project.id}
          onComplete={() => {
            onOpenChange(false);
            if (onComplete) onComplete();
          }}
        />
      </AlertDialogContent>
    </AlertDialog>
  );
};