Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

7.6 KB
Raw Blame
import { isNil, SuggestionType } from '@activepieces/shared';
import { t } from 'i18next';
import { ExternalLink, Info, Puzzle, TriangleAlert } from 'lucide-react';
import { useMemo, useState } from 'react';
import { Link } from 'react-router-dom';
import { useDebounce } from 'use-debounce';

import { DataFetchErrorState } from '@/components/custom/data-fetch-error-state';
import { LockedAlert } from '@/components/custom/locked-alert';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { Button } from '@/components/ui/button';
import {
  Empty,
  EmptyDescription,
  EmptyHeader,
  EmptyMedia,
  EmptyTitle,
} from '@/components/ui/empty';
import { Skeleton } from '@/components/ui/skeleton';
import { VirtualizedList } from '@/components/ui/virtualized-list';
import { RequestTrial } from '@/features/billing';
import { pieceSetQueries } from '@/features/piece-sets';
import { piecesHooks } from '@/features/pieces/hooks/pieces-hooks';
import { useIsPlatformAdmin } from '@/hooks/authorization-hooks';
import { platformHooks } from '@/hooks/platform-hooks';

import {
  isProjectAccessError,
  ProjectAccessDeniedAlert,
} from '../project-access';

import { PieceRow } from './piece-row';
import { piecesUtils } from './pieces-utils';

const COLLAPSED_ROW_LIMIT = 6;
const COLLAPSED_ROW_HEIGHT = 50;
const PIECE_SETS_LIST_ROUTE = '/platform/pieces/piece-sets';
const SEARCH_DEBOUNCE_MS = 300;

export function PiecesPanel({
  projectId,
  searchQuery,
  isRunActionDisabled,
  isRunActionDisabledByPlatform,
  onShowBuiltIn,
}: PiecesPanelProps) {
  const [debouncedSearchQuery] = useDebounce(
    searchQuery.trim(),
    SEARCH_DEBOUNCE_MS,
  );
  const [showAll, setShowAll] = useState(false);

  const isSearching = debouncedSearchQuery !== '';
  const { pieces, isLoading, isError, error, refetch } = piecesHooks.usePieces({
    projectId: projectId ?? undefined,
    searchQuery: isSearching ? debouncedSearchQuery : undefined,
    suggestionType: SuggestionType.ACTION,
    enabled: !isNil(projectId),
    keepPreviousResults: true,
  });
  const rows = useMemo(
    () =>
      piecesUtils.toReachablePieces({
        pieces: pieces ?? [],
        isSearching,
      }),
    [pieces, isSearching],
  );
  const visibleRows = useMemo(
    () => (isSearching || showAll ? rows : rows.slice(0, COLLAPSED_ROW_LIMIT)),
    [rows, isSearching, showAll],
  );
  const hiddenCount = rows.length - visibleRows.length;

  return (
    <div className="flex flex-col gap-4">
      <PieceSetBanner projectId={projectId} />

      {(isRunActionDisabled || isRunActionDisabledByPlatform) && (
        <RunActionDisabledAlert
          isDisabledByPlatform={isRunActionDisabledByPlatform}
          onShowBuiltIn={onShowBuiltIn}
        />
      )}

      {isLoading ? (
        <div className="flex flex-col gap-2">
          {Array.from({ length: COLLAPSED_ROW_LIMIT }).map((_, index) => (
            <Skeleton key={index} className="h-16 w-full" />
          ))}
        </div>
      ) : isError ? (
        <PiecesUnavailableAlert error={error} onRetry={refetch} />
      ) : rows.length === 0 ? (
        <Empty className="rounded-xl border py-12">
          <EmptyHeader>
            <EmptyMedia variant="icon">
              <Puzzle />
            </EmptyMedia>
            <EmptyTitle>
              {isSearching
                ? t('No piece or action matches your search.')
                : t('No pieces are reachable in this project.')}
            </EmptyTitle>
            {!isSearching && (
              <EmptyDescription>
                {t(
                  'Pieces show up here once they are allowed in this project.',
                )}
              </EmptyDescription>
            )}
          </EmptyHeader>
        </Empty>
      ) : (
        <div className="rounded-xl border">
          <VirtualizedList
            items={visibleRows}
            estimateSize={COLLAPSED_ROW_HEIGHT}
            getItemKey={(index) => visibleRows[index].piece.name}
            renderItem={(row, index) => (
              <PieceRow
                row={row}
                isLastRow={index === visibleRows.length - 1}
              />
            )}
          />
          {hiddenCount > 0 && (
            <button
              type="button"
              onClick={() => setShowAll(true)}
              className="w-full border-t px-4 py-3 text-sm font-medium hover:bg-gray-3/40"
            >
              {t('Show {count} more pieces', { count: hiddenCount })}
            </button>
          )}
        </div>
      )}
    </div>
  );
}

function PiecesUnavailableAlert({
  error,
  onRetry,
}: PiecesUnavailableAlertProps) {
  if (isProjectAccessError(error)) {
    return <ProjectAccessDeniedAlert />;
  }

  return (
    <div className="rounded-xl border">
      <DataFetchErrorState entity={t('pieces')} onRetry={onRetry} />
    </div>
  );
}

function RunActionDisabledAlert({
  isDisabledByPlatform,
  onShowBuiltIn,
}: {
  isDisabledByPlatform: boolean;
  onShowBuiltIn: () => void;
}) {
  return (
    <Alert variant="warning">
      <TriangleAlert />
      <AlertTitle>{t('Nothing below can run right now')}</AlertTitle>
      <AlertDescription>
        {isDisabledByPlatform
          ? t(
              'A platform admin switched Run action off for the whole platform. Clients can still see the list, but every call fails.',
            )
          : t(
              'Running piece actions is switched off for this project. Clients can still see the list, but every call fails.',
            )}
      </AlertDescription>
      {!isDisabledByPlatform && (
        <Button
          variant="outline"
          size="sm"
          className="col-start-2 mt-3 w-fit"
          onClick={onShowBuiltIn}
        >
          {t('Turn on Run action')}
        </Button>
      )}
    </Alert>
  );
}

function PieceSetBanner({ projectId }: { projectId: string | null }) {
  const { platform } = platformHooks.useCurrentPlatform();
  const isPlatformAdmin = useIsPlatformAdmin();
  const { data: pieceSet } = pieceSetQueries.useProjectPieceSet(projectId);

  if (!platform.plan.managePiecesEnabled) {
    return (
      <LockedAlert
        title={t('Control Pieces')}
        description={t(
          'Every piece below is reachable by any connected client. Restricting the list to a chosen set is an enterprise feature.',
        )}
        button={
          <RequestTrial featureKey="ENTERPRISE_PIECES" buttonVariant="basic" />
        }
      />
    );
  }

  return (
    <Alert
      variant="primary"
      className="flex flex-wrap items-center gap-x-3 gap-y-2"
    >
      <Info />
      <AlertDescription className="min-w-60 flex-1">
        {isPlatformAdmin
          ? t("This project's pieces are controlled by a Piece Set.")
          : t(
              "This project's pieces are controlled by a Piece Set. Contact a platform admin to change it.",
            )}
      </AlertDescription>
      {isPlatformAdmin && (
        <Button
          asChild
          variant="outline"
          size="sm"
          className="ml-auto shrink-0"
        >
          <Link
            to={
              pieceSet
                ? `/platform/pieces/piece-sets/${pieceSet.id}`
                : PIECE_SETS_LIST_ROUTE
            }
          >
            {pieceSet?.name ?? t('Review piece set')}
            <ExternalLink className="size-3.5" />
          </Link>
        </Button>
      )}
    </Alert>
  );
}

type PiecesUnavailableAlertProps = {
  error: Error | null;
  onRetry: () => void;
};

type PiecesPanelProps = {
  projectId: string | null;
  searchQuery: string;
  isRunActionDisabled: boolean;
  isRunActionDisabledByPlatform: boolean;
  onShowBuiltIn: () => void;
};