Skip to content

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

3.3 KB
Raw Blame
import { useState } from "react";
import { format } from "date-fns";

import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
  Loader,
  Pagination,
  RowLink,
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow
} from "@app/components/v3";
import { useProject } from "@app/context";
import { getInstancePinningLink } from "@app/helpers/project";
import { useListPkiInstallations } from "@app/hooks/api";
import { getEndpoint, getGatewayLabel } from "@app/pages/cert-manager/pki-discovery-utils";

type Props = {
  certificateId: string;
};

const PER_PAGE_INIT = 10;

export const CertificateInstallationsSection = ({ certificateId }: Props) => {
  const { currentProject } = useProject();
  const projectId = currentProject.id;
  const [page, setPage] = useState(1);
  const [perPage, setPerPage] = useState(PER_PAGE_INIT);

  const { data, isPending } = useListPkiInstallations({
    projectId,
    certificateId,
    offset: (page - 1) * perPage,
    limit: perPage
  });

  const installations = data?.installations;
  const totalCount = data?.totalCount ?? 0;

  if (isPending) {
    return (
      <Card>
        <CardHeader className="border-b">
          <CardTitle>Installations</CardTitle>
          <CardDescription>Locations where this certificate was discovered</CardDescription>
        </CardHeader>
        <CardContent>
          <div className="flex h-40 w-full items-center justify-center">
            <Loader />
          </div>
        </CardContent>
      </Card>
    );
  }

  if (!installations || installations.length === 0) {
    return null;
  }

  const paginatedInstallations = installations || [];

  return (
    <Card>
      <CardHeader className="border-b">
        <CardTitle>Installations</CardTitle>
        <CardDescription>Locations where this certificate was discovered</CardDescription>
      </CardHeader>
      <CardContent className="p-0">
        <Table>
          <TableHeader>
            <TableRow>
              <TableHead>Name</TableHead>
              <TableHead>Gateway</TableHead>
              <TableHead>Last Seen</TableHead>
            </TableRow>
          </TableHeader>
          <TableBody>
            {paginatedInstallations.map((installation) => (
              <TableRow key={installation.id} linked>
                <TableCell className="max-w-80">
                  <RowLink
                    {...getInstancePinningLink(
                      currentProject.orgId,
                      currentProject,
                      `discovery/installations/${installation.id}`
                    )!}
                    title={installation.name || getEndpoint(installation)}
                  >
                    {installation.name || getEndpoint(installation)}
                  </RowLink>
                </TableCell>
                <TableCell>{getGatewayLabel(installation) || "N/A"}</TableCell>
                <TableCell>
                  {format(new Date(installation.lastSeenAt), "MMM dd, yyyy HH:mm")}
                </TableCell>
              </TableRow>
            ))}
          </TableBody>
        </Table>
        {Boolean(totalCount) && (
          <Pagination
            count={totalCount}
            page={page}
            perPage={perPage}
            onChangePage={setPage}
            onChangePerPage={setPerPage}
          />
        )}
      </CardContent>
    </Card>
  );
};