Skip to content

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

3.2 KB
Raw Blame
import type { JSX } from "react";
import { CircleCheckIcon, CircleXIcon } from "lucide-react";
import { twMerge } from "tailwind-merge";

import { PolicyBlock, PolicyLine } from "./VaultPolicyAnalyzer.utils";

type Props = {
  blocks: PolicyBlock[];
  lines: PolicyLine[];
};

export const VaultPolicyPreview = ({ blocks, lines }: Props) => {
  const blockMap = new Map(blocks.map((block) => [block.id, block]));

  return (
    <div className="flex h-80 flex-col rounded-md border border-border bg-container xl:h-[30rem]">
      <div className="flex-1 overflow-auto font-mono text-xs">
        {lines.map((line) => {
          const block = line.belongsToBlock ? blockMap.get(line.belongsToBlock) : null;
          const isPartOfBlock = line.type === "part-of-block";
          const isComment = line.type === "comment";
          const isEmpty = line.type === "empty";

          let bgColorClass = "";
          let borderColorClass = "";
          let textColorClass = "text-accent";
          let showIndicator = false;
          let indicator: JSX.Element | null = null;

          if (isPartOfBlock && block) {
            showIndicator = line.lineNumber === block.startLine;
            if (block.canTranslate) {
              bgColorClass = "bg-success/10";
              borderColorClass = "border-l-2 border-success/50";
              textColorClass = "text-foreground";
              if (showIndicator) {
                indicator = (
                  <div className="flex items-center gap-2 text-success">
                    <CircleCheckIcon className="size-3" />
                    <span className="text-xs">Can translate</span>
                  </div>
                );
              }
            } else {
              bgColorClass = "bg-danger/10";
              borderColorClass = "border-l-2 border-danger/50";
              textColorClass = "text-foreground";
              if (showIndicator) {
                indicator = (
                  <div className="flex items-center gap-2 text-danger">
                    <CircleXIcon className="size-3" />
                    <span className="text-xs">{block.reason || "Cannot translate"}</span>
                  </div>
                );
              }
            }
          } else if (isComment) {
            textColorClass = "text-muted italic";
          }

          return (
            <div key={line.id} className="group relative">
              {showIndicator && indicator && (
                <div
                  className={twMerge(
                    "flex items-center px-4 py-1.5",
                    block?.canTranslate ? "bg-success/5" : "bg-danger/5"
                  )}
                >
                  {indicator}
                </div>
              )}
              <div
                className={twMerge(
                  "px-4 py-0.5 leading-6",
                  bgColorClass,
                  borderColorClass,
                  isEmpty && "min-h-[1.5rem]"
                )}
              >
                <span className={twMerge("font-mono whitespace-pre", textColorClass)}>
                  {line.text || " "}
                </span>
              </div>
            </div>
          );
        })}
      </div>
    </div>
  );
};