Skip to content

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

6.2 KB
Raw Blame
import { useState } from "react";
import { subject } from "@casl/ability";
import { faChevronLeft } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { Link, useNavigate, useParams, useSearch } from "@tanstack/react-router";

import { createNotification } from "@app/components/notifications";
import { PageTitle } from "@app/components/PageTitle";
import { ProjectPermissionCan } from "@app/components/permissions";
import { PageHeader } from "@app/components/v3";
import { ConfirmationAlertDialog } from "@app/components/v3/platform";
import { ROUTE_PATHS } from "@app/const/routes";
import { useOrganization } from "@app/context";
import {
  ProjectPermissionCertificateProfileActions,
  ProjectPermissionSub
} from "@app/context/ProjectPermissionContext/types";
import {
  useDeleteCertificateProfile,
  useGetCertificateProfileById
} from "@app/hooks/api/certificateProfiles";
import { ProjectType } from "@app/hooks/api/projects/types";

import { CreateProfileModal } from "../PoliciesPage/components/CertificateProfilesTab/CreateProfileModal";
import { ProfileDefaultsSection, ProfileOverviewSection } from "./components";

const Page = () => {
  const { currentOrg } = useOrganization();
  const navigate = useNavigate();
  const params = useParams({
    from: ROUTE_PATHS.CertManager.CertificateProfileDetailsByIDPage.id
  });
  const { profileId } = params as { profileId: string };
  const search = useSearch({
    from: ROUTE_PATHS.CertManager.CertificateProfileDetailsByIDPage.id
  }) as { from?: "settings" | "application"; applicationName?: string };

  const { data: profile } = useGetCertificateProfileById({ profileId });
  const { mutateAsync: deleteProfile } = useDeleteCertificateProfile();

  const [isEditModalOpen, setIsEditModalOpen] = useState(false);
  const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);

  const cameFromApplication = search.from === "application" && Boolean(search.applicationName);

  const navigateBack = () => {
    if (cameFromApplication && search.applicationName) {
      navigate({
        to: "/organizations/$orgId/cert-manager/applications/$applicationName",
        params: {
          orgId: currentOrg.id,
          applicationName: search.applicationName
        }
      });
      return;
    }

    navigate({
      to: "/organizations/$orgId/cert-manager/certificate-profiles",
      params: {
        orgId: currentOrg.id
      }
    });
  };

  const handleDeleteConfirm = async () => {
    if (!profile) return;

    try {
      await deleteProfile({ profileId: profile.id });

      createNotification({
        text: `Certificate profile "${profile.slug}" deleted successfully`,
        type: "success"
      });

      setIsDeleteModalOpen(false);
      navigateBack();
    } catch (error) {
      createNotification({
        text: `Failed to delete certificate profile: ${(error as Error)?.message || "Unknown error"}`,
        type: "error"
      });
    }
  };

  return (
    <div className="mx-auto flex flex-col justify-between bg-page text-foreground-inverse">
      {profile && (
        <ProjectPermissionCan
          renderGuardBanner
          I={ProjectPermissionCertificateProfileActions.Read}
          a={subject(ProjectPermissionSub.CertificateProfiles, { slug: profile.slug })}
        >
          {(isAllowed) =>
            isAllowed ? (
              <div className="mx-auto mb-6 flex w-full max-w-8xl flex-col">
                <PageHeader
                  backLink={
                    cameFromApplication && search.applicationName ? (
                      <Link
                        to="/organizations/$orgId/cert-manager/applications/$applicationName"
                        params={{
                          orgId: currentOrg.id,
                          applicationName: search.applicationName
                        }}
                      >
                        <FontAwesomeIcon icon={faChevronLeft} />
                        {search.applicationName}
                      </Link>
                    ) : (
                      <Link
                        to="/organizations/$orgId/cert-manager/certificate-profiles"
                        params={{
                          orgId: currentOrg.id
                        }}
                      >
                        <FontAwesomeIcon icon={faChevronLeft} />
                        Certificate Profiles
                      </Link>
                    )
                  }
                  scope={ProjectType.CertificateManager}
                  description="Manage certificate profile"
                  title={profile.slug}
                />
                <div className="flex flex-col gap-5 lg:flex-row">
                  <div className="w-full lg:max-w-[24rem]">
                    <ProfileOverviewSection
                      profile={profile}
                      onEdit={() => setIsEditModalOpen(true)}
                      onDelete={() => setIsDeleteModalOpen(true)}
                      backContext={{
                        from: search.from,
                        applicationName: search.applicationName
                      }}
                    />
                  </div>
                  <div className="flex min-w-0 flex-1 flex-col gap-y-5">
                    <ProfileDefaultsSection profile={profile} />
                  </div>
                </div>

                <CreateProfileModal
                  isOpen={isEditModalOpen}
                  onClose={() => setIsEditModalOpen(false)}
                  profile={profile}
                  mode="edit"
                />

                <ConfirmationAlertDialog
                  isOpen={isDeleteModalOpen}
                  title={`Delete Certificate Profile ${profile.slug}?`}
                  description="This action is irreversible."
                  onOpenChange={(isOpen) => setIsDeleteModalOpen(isOpen)}
                  confirmKey={profile.slug}
                  onConfirm={handleDeleteConfirm}
                />
              </div>
            ) : null
          }
        </ProjectPermissionCan>
      )}
    </div>
  );
};

export const CertificateProfileDetailsByIDPage = () => {
  return (
    <>
      <PageTitle title="Certificate Profile" />
      <link rel="icon" href="/infisical.ico" />
      <Page />
    </>
  );
};