Skip to content

Automate browser based workflows with AI

4.9 KB
Raw Blame
import { Skeleton } from "@/components/ui/skeleton";
import { useRowSelection } from "@/hooks/useRowSelection";
import { CredentialItem } from "./CredentialItem";
import { CredentialsBulkBar } from "./CredentialsBulkBar";
import { useCredentialsQuery } from "@/routes/workflows/hooks/useCredentialsQuery";
import {
  Pagination,
  PaginationContent,
  PaginationItem,
  PaginationLink,
  PaginationNext,
  PaginationPrevious,
} from "@/components/ui/pagination";
import { cn } from "@/util/utils";
import { useState } from "react";

type CredentialFilter = "password" | "credit_card" | "secret";

type Props = {
  filter?: CredentialFilter;
  search?: string;
  folderId?: string | null;
  isResolvingFolder?: boolean;
  onStartBackgroundTest?: (
    credentialId: string,
    url: string,
    userContext?: string,
  ) => void;
};

const EMPTY_MESSAGE: Record<CredentialFilter, string> = {
  password: "No password credentials stored yet.",
  credit_card: "No credit cards stored yet.",
  secret: "No secrets stored yet.",
};

const PAGE_SIZE = 25;

function CredentialsList({
  filter,
  search,
  folderId,
  isResolvingFolder,
  onStartBackgroundTest,
}: Props = {}) {
  const trimmedSearch = search?.trim() ?? "";
  const [page, setPage] = useState(1);
  const [prevSearch, setPrevSearch] = useState(trimmedSearch);
  const [prevFolderId, setPrevFolderId] = useState(folderId);

  // Reset to page 1 synchronously when the search or folder filter changes —
  // avoids the extra fetch with the stale page that a post-render effect would trigger.
  if (prevSearch !== trimmedSearch || prevFolderId !== folderId) {
    setPrevSearch(trimmedSearch);
    setPrevFolderId(folderId);
    setPage(1);
  }

  const { data: credentials, isLoading } = useCredentialsQuery({
    page,
    page_size: PAGE_SIZE,
    credential_type: filter,
    search: trimmedSearch || undefined,
    folder_id: folderId || undefined,
    // Hold the query until a deep-linked ?folder= slug resolves, so the
    // unfiltered credential bank doesn't flash before the filter applies.
    enabled: !isResolvingFolder,
  });

  const [isBulkOperating, setIsBulkOperating] = useState(false);
  const {
    selected,
    selectedItems: selectedCredentials,
    isSelected,
    handleSelect,
    clearSelection,
    replaceSelection,
  } = useRowSelection({
    items: credentials ?? [],
    getId: (credential) => credential.credential_id,
    resetKey: JSON.stringify([page, trimmedSearch, folderId ?? null]),
  });

  if (isResolvingFolder || isLoading) {
    return (
      <div className="space-y-5">
        <Skeleton className="h-20 w-full" />
        <Skeleton className="h-20 w-full" />
      </div>
    );
  }

  if (!credentials) {
    return null;
  }

  if (credentials.length === 0 && page === 1) {
    const emptyMessage = trimmedSearch
      ? `No credentials match “${trimmedSearch}”.`
      : filter
        ? EMPTY_MESSAGE[filter]
        : "No credentials stored yet.";
    return (
      <div className="rounded-md border border-slate-700 bg-slate-elevation1 p-6 text-sm text-neutral-600 dark:text-slate-300">
        {emptyMessage}
      </div>
    );
  }

  const hasNextPage = credentials.length === PAGE_SIZE;

  return (
    <div className="space-y-5">
      <div className="space-y-5">
        {credentials.map((credential, index) => (
          <CredentialItem
            key={credential.credential_id}
            credential={credential}
            onStartBackgroundTest={onStartBackgroundTest}
            index={index}
            selected={isSelected(credential.credential_id)}
            hasSelection={selected.size > 0}
            onSelect={handleSelect}
          />
        ))}
      </div>
      {(page > 1 || hasNextPage) && (
        <Pagination>
          <PaginationContent>
            <PaginationItem>
              <PaginationPrevious
                className={cn({ "cursor-not-allowed": page === 1 })}
                onClick={() => {
                  if (page > 1) {
                    setPage((prev) => Math.max(1, prev - 1));
                  }
                }}
              />
            </PaginationItem>
            <PaginationItem>
              <PaginationLink>{page}</PaginationLink>
            </PaginationItem>
            <PaginationItem>
              <PaginationNext
                className={cn({ "cursor-not-allowed": !hasNextPage })}
                onClick={() => {
                  if (hasNextPage) {
                    setPage((prev) => prev + 1);
                  }
                }}
              />
            </PaginationItem>
          </PaginationContent>
        </Pagination>
      )}
      {selectedCredentials.length > 0 && (
        <CredentialsBulkBar
          selectedCredentials={selectedCredentials}
          isOperating={isBulkOperating}
          onOperatingChange={setIsBulkOperating}
          onClear={clearSelection}
          onReplaceSelection={replaceSelection}
        />
      )}
    </div>
  );
}

export { CredentialsList };