Skip to content

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

12.6 KB
Raw Blame
import { useState } from "react";
import { format, intervalToDuration } from "date-fns";
import {
  ArrowDownIcon,
  ArrowUpIcon,
  Ellipsis,
  HourglassIcon,
  PencilIcon,
  RotateCcwIcon,
  Trash2Icon
} from "lucide-react";

import { createNotification } from "@app/components/notifications";
import { ProjectPermissionCan } from "@app/components/permissions";
import {
  Badge,
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
  Empty,
  EmptyDescription,
  EmptyHeader,
  EmptyTitle,
  IconButton,
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
  Tooltip,
  TooltipContent,
  TooltipTrigger
} from "@app/components/v3";
import {
  ProjectPermissionActions,
  ProjectPermissionSub,
  useProject,
  useSubscription
} from "@app/context";
import { useUpdateWsEnvironment } from "@app/hooks/api";
import { ProjectDeletedEnvActor } from "@app/hooks/api/projects/types";
import { UsePopUpState } from "@app/hooks/usePopUp";

type PopUpKeys = "updateEnv" | "deleteEnv" | "restoreEnv" | "hardDeleteEnv";

type EnvPayload = { name: string; slug: string; id: string; deleteAfter?: string };

type Props = {
  handlePopUpOpen: (popUpName: keyof UsePopUpState<[PopUpKeys]>, env: EnvPayload) => void;
  isExternalMutationPending: boolean;
  onMutationPendingChange: (isPending: boolean) => void;
};

export const getActorLabel = (actor: ProjectDeletedEnvActor | null): string => {
  if (!actor) return "Someone";
  if (actor.type === "identity") return actor.name || "An identity";

  return actor.firstName || actor.username || actor.email || "A user";
};

export const formatRemainingDuration = (target: Date): string | null => {
  const now = new Date();
  if (target.getTime() <= now.getTime()) return null;

  const duration = intervalToDuration({ start: now, end: target });
  const parts: Array<[number | undefined, string]> = [
    [duration.years, "y"],
    [duration.months, "mo"],
    [duration.days, "d"],
    [duration.hours, "h"],
    [duration.minutes, "m"]
  ];

  const nonZero = parts.filter(([value]) => value && value > 0).slice(0, 2);
  if (nonZero.length === 0) return "<1m";

  return nonZero.map(([value, suffix]) => `${value}${suffix}`).join(" ");
};

export const EnvironmentTable = ({
  handlePopUpOpen,
  isExternalMutationPending,
  onMutationPendingChange
}: Props) => {
  const { currentProject } = useProject();
  const { subscription } = useSubscription();

  const updateEnvironment = useUpdateWsEnvironment();
  const [pendingAction, setPendingAction] = useState<
    { id: string; direction: "up" | "down" } | undefined
  >();

  const activeEnvironments = currentProject.environments ?? [];
  const deletedEnvironments = currentProject.deletedEnvironments ?? [];

  const handleReorderEnv = async (id: string, position: number, direction: "up" | "down") => {
    if (!currentProject?.id || isExternalMutationPending || updateEnvironment.isPending) {
      return;
    }

    setPendingAction({ id, direction });
    onMutationPendingChange(true);

    try {
      await updateEnvironment.mutateAsync({
        projectId: currentProject.id,
        id,
        position
      });

      createNotification({
        text: "Successfully re-ordered environments",
        type: "success"
      });
    } finally {
      setPendingAction(undefined);
      onMutationPendingChange(false);
    }
  };

  const isMutationPending = isExternalMutationPending || updateEnvironment.isPending;

  const isMoreEnvironmentsAllowed =
    subscription?.environmentLimit && activeEnvironments
      ? activeEnvironments.length <= subscription.environmentLimit
      : true;

  const environmentsOverPlanLimit =
    subscription?.environmentLimit && activeEnvironments
      ? Math.max(0, activeEnvironments.length - subscription.environmentLimit)
      : 0;

  if (!activeEnvironments.length && !deletedEnvironments.length) {
    return (
      <Empty className="border">
        <EmptyHeader>
          <EmptyTitle>No environments found</EmptyTitle>
          <EmptyDescription>
            Create your first environment to organize secrets by stage.
          </EmptyDescription>
        </EmptyHeader>
      </Empty>
    );
  }

  return (
    <Table>
      <TableHeader>
        <TableRow>
          <TableHead>Name</TableHead>
          <TableHead>Slug</TableHead>
        </TableRow>
      </TableHeader>
      <TableBody>
        {activeEnvironments.map(({ name, slug, id }, pos) => (
          <TableRow key={id}>
            <TableCell>{name}</TableCell>
            <TableCell>{slug}</TableCell>
            <TableCell>
              <div className="flex items-center justify-end gap-1">
                <ProjectPermissionCan
                  I={ProjectPermissionActions.Edit}
                  a={ProjectPermissionSub.Environments}
                >
                  {(isAllowed) => (
                    <IconButton
                      aria-label="Move down"
                      variant="ghost-muted"
                      onClick={() =>
                        handleReorderEnv(id, Math.min(activeEnvironments.length, pos + 2), "down")
                      }
                      isPending={pendingAction?.id === id && pendingAction.direction === "down"}
                      isDisabled={
                        pos === activeEnvironments.length - 1 || !isAllowed || isMutationPending
                      }
                    >
                      <ArrowDownIcon />
                    </IconButton>
                  )}
                </ProjectPermissionCan>
                <ProjectPermissionCan
                  I={ProjectPermissionActions.Edit}
                  a={ProjectPermissionSub.Environments}
                >
                  {(isAllowed) => (
                    <IconButton
                      aria-label="Move up"
                      variant="ghost-muted"
                      onClick={() => handleReorderEnv(id, Math.max(1, pos), "up")}
                      isPending={pendingAction?.id === id && pendingAction.direction === "up"}
                      isDisabled={pos === 0 || !isAllowed || isMutationPending}
                    >
                      <ArrowUpIcon />
                    </IconButton>
                  )}
                </ProjectPermissionCan>
                <DropdownMenu>
                  <DropdownMenuTrigger asChild>
                    <IconButton
                      aria-label="Environment options"
                      variant="ghost-muted"
                      isDisabled={isMutationPending}
                    >
                      <Ellipsis />
                    </IconButton>
                  </DropdownMenuTrigger>
                  <DropdownMenuContent align="end">
                    <ProjectPermissionCan
                      I={ProjectPermissionActions.Edit}
                      a={ProjectPermissionSub.Environments}
                    >
                      {(isAllowed) => (
                        <Tooltip open={!isMoreEnvironmentsAllowed ? undefined : false}>
                          <TooltipTrigger asChild>
                            <DropdownMenuItem
                              isDisabled={
                                !isAllowed || !isMoreEnvironmentsAllowed || isMutationPending
                              }
                              onClick={() => handlePopUpOpen("updateEnv", { name, slug, id })}
                            >
                              <PencilIcon />
                              Edit environment
                            </DropdownMenuItem>
                          </TooltipTrigger>
                          <TooltipContent side="left">
                            You have exceeded the number of environments allowed by your plan. To
                            edit an existing environment, either upgrade your plan or remove at
                            least {environmentsOverPlanLimit} environment
                            {environmentsOverPlanLimit === 1 ? "" : "s"}.
                          </TooltipContent>
                        </Tooltip>
                      )}
                    </ProjectPermissionCan>
                    <ProjectPermissionCan
                      I={ProjectPermissionActions.Delete}
                      a={ProjectPermissionSub.Environments}
                    >
                      {(isAllowed) => (
                        <DropdownMenuItem
                          variant="danger"
                          isDisabled={!isAllowed || isMutationPending}
                          onClick={() => handlePopUpOpen("deleteEnv", { name, slug, id })}
                        >
                          <Trash2Icon />
                          Delete environment
                        </DropdownMenuItem>
                      )}
                    </ProjectPermissionCan>
                  </DropdownMenuContent>
                </DropdownMenu>
              </div>
            </TableCell>
          </TableRow>
        ))}
        {deletedEnvironments.map(({ name, slug, id, deleteAfter, softDeletedAt, deletedBy }) => {
          const deleteAfterDate = new Date(deleteAfter);
          const remaining = formatRemainingDuration(deleteAfterDate);

          return (
            <TableRow key={id} className="bg-warning/[0.025]">
              <TableCell className="text-warning/80 line-through">{name}</TableCell>
              <TableCell className="text-warning/80 line-through">{slug}</TableCell>
              <TableCell>
                <div className="flex items-center justify-end gap-2">
                  <Tooltip>
                    <TooltipTrigger asChild>
                      <Badge variant="warning">
                        <HourglassIcon />
                        Pending deletion
                      </Badge>
                    </TooltipTrigger>
                    <TooltipContent className="max-w-xs">
                      <div className="flex flex-col gap-1">
                        <span className="text-sm font-semibold text-foreground">
                          {remaining
                            ? `${remaining} until permanent deletion`
                            : "Will be permanently deleted soon"}
                        </span>
                        <span className="text-xs text-accent">
                          {remaining
                            ? `Scheduled for ${format(deleteAfterDate, "MMM d, yyyy, h:mm a")}`
                            : "Awaiting the next daily cleanup sweep"}
                        </span>
                        <span className="text-xs text-accent">
                          Soft-deleted by {getActorLabel(deletedBy)} ·{" "}
                          {format(new Date(softDeletedAt), "MMM d, yyyy")}
                        </span>
                      </div>
                    </TooltipContent>
                  </Tooltip>
                  <ProjectPermissionCan
                    I={ProjectPermissionActions.Delete}
                    a={ProjectPermissionSub.Environments}
                  >
                    {(isAllowed) => (
                      <DropdownMenu>
                        <DropdownMenuTrigger asChild disabled={!isAllowed || isMutationPending}>
                          <IconButton
                            aria-label="Environment options"
                            variant="ghost-muted"
                            isDisabled={!isAllowed || isMutationPending}
                          >
                            <Ellipsis />
                          </IconButton>
                        </DropdownMenuTrigger>
                        <DropdownMenuContent align="end">
                          <DropdownMenuItem
                            isDisabled={isMutationPending}
                            onClick={() =>
                              handlePopUpOpen("restoreEnv", { name, slug, id, deleteAfter })
                            }
                          >
                            <RotateCcwIcon />
                            Restore environment
                          </DropdownMenuItem>
                          <DropdownMenuItem
                            variant="danger"
                            isDisabled={isMutationPending}
                            onClick={() =>
                              handlePopUpOpen("hardDeleteEnv", { name, slug, id, deleteAfter })
                            }
                          >
                            <Trash2Icon />
                            Delete Now
                          </DropdownMenuItem>
                        </DropdownMenuContent>
                      </DropdownMenu>
                    )}
                  </ProjectPermissionCan>
                </div>
              </TableCell>
            </TableRow>
          );
        })}
      </TableBody>
    </Table>
  );
};