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

8.2 KB
Raw Blame
import { ApErrorParams, ErrorCode, isNil } from '@activepieces/core-utils';
import {
  PieceMetadataModelSummary,
  PropertyType,
} from '@activepieces/pieces-framework';
import { OAuth2GrantType, PieceScope, PieceType } from '@activepieces/shared';
import { ColumnDef } from '@tanstack/react-table';
import { t } from 'i18next';
import {
  CheckIcon,
  Package,
  Hash,
  GitBranch,
  Puzzle,
  Trash,
} from 'lucide-react';
import { useMemo } from 'react';
import { useSearchParams } from 'react-router-dom';
import { toast } from 'sonner';

import {
  AdminDataTable,
  AdminPage,
  AdminPageHeader,
  adminPageResources,
} from '@/app/components/admin';
import { CustomizeSelectorDialog } from '@/app/routes/platform/setup/pieces/customize-selector-dialog';
import { DownloadPiecesReportButton } from '@/app/routes/platform/setup/pieces/download-pieces-report';
import { PieceActions } from '@/app/routes/platform/setup/pieces/piece-actions';
import { PiecesLockedBanner } from '@/app/routes/platform/setup/pieces/pieces-locked-banner';
import { SyncPiecesButton } from '@/app/routes/platform/setup/pieces/sync-pieces';
import { ConfigurePieceOAuth2Dialog } from '@/app/routes/platform/setup/pieces/update-oauth2-dialog';
import { RowDataWithActions } from '@/components/custom/data-table';
import { DataTableColumnHeader } from '@/components/custom/data-table/data-table-column-header';
import { ConfirmationDeleteDialog } from '@/components/custom/delete-dialog';
import { Button } from '@/components/ui/button';
import { oauthAppsQueries } from '@/features/connections';
import {
  InstallPieceDialog,
  PieceIcon,
  piecesApi,
  piecesHooks,
} from '@/features/pieces';
import { platformHooks } from '@/hooks/platform-hooks';
import { AdminControl, adminControl } from '@/lib/admin-control';
import { api } from '@/lib/api';

export const PiecesListTab = () => {
  const { platform } = platformHooks.useCurrentPlatform();
  const isEnabled = platform.plan.managePiecesEnabled;
  const [searchParams] = useSearchParams();
  const searchQuery = searchParams.get('name') ?? '';
  const {
    pieces,
    refetch: refetchPieces,
    isLoading,
    isError,
  } = piecesHooks.usePieces({
    searchQuery,
    includeHidden: true,
    isTableQuery: true,
  });

  const { refetch: refetchPiecesOAuth2AppsMap } =
    oauthAppsQueries.usePiecesOAuth2AppsMap();

  const columns: ColumnDef<RowDataWithActions<PieceMetadataModelSummary>>[] =
    useMemo(
      () => [
        {
          accessorKey: 'displayName',
          size: 300,
          header: ({ column }) => (
            <DataTableColumnHeader
              column={column}
              title={t('Name')}
              icon={Puzzle}
            />
          ),
          cell: ({ row }) => {
            return (
              <div className="flex items-center gap-2">
                <PieceIcon
                  size={'sm'}
                  border={true}
                  displayName={row.original.displayName}
                  logoUrl={row.original.logoUrl}
                  showTooltip={false}
                />
                <div className="flex flex-col gap-0.5">
                  <span>{row.original.displayName}</span>
                </div>
              </div>
            );
          },
        },
        {
          accessorKey: 'packageName',
          size: 250,
          header: ({ column }) => (
            <DataTableColumnHeader
              column={column}
              title={t('Package Name')}
              icon={Hash}
            />
          ),
          cell: ({ row }) => {
            return <div className="text-left">{row.original.name}</div>;
          },
        },
        {
          accessorKey: 'version',
          size: 80,
          header: ({ column }) => (
            <DataTableColumnHeader
              column={column}
              title={t('Version')}
              icon={GitBranch}
            />
          ),
          cell: ({ row }) => {
            return <div className="text-left">{row.original.version}</div>;
          },
        },
        {
          id: 'actions',
          size: 190,
          cell: ({ row }) => {
            return (
              <div className="flex justify-end">
                {shouldShowOauth2SettingForPiece(row.original) && (
                  <ConfigurePieceOAuth2Dialog
                    pieceName={row.original.name}
                    onConfigurationDone={() => {
                      refetchPieces();
                      refetchPiecesOAuth2AppsMap();
                    }}
                    isEnabled={isEnabled}
                  />
                )}
                <PieceActions
                  pieceName={row.original.name}
                  isEnabled={isEnabled}
                />
                {row.original.pieceType === PieceType.CUSTOM && (
                  <ConfirmationDeleteDialog
                    title={t('Delete {name}', { name: row.original.name })}
                    entityName={t('Piece')}
                    controlId={AdminControl.PIECES_DELETE_CONFIRM}
                    message={t(
                      'This will permanently delete this piece, all steps using it will fail.',
                    )}
                    mutationFn={async () => {
                      await piecesApi.delete(row.original.id!);
                      await refetchPieces();
                    }}
                    onError={(error) => {
                      if (api.isError(error)) {
                        const apError = error.response?.data as ApErrorParams;
                        if (apError?.code === ErrorCode.VALIDATION) {
                          toast.error(apError.params.message);
                          return;
                        }
                      }
                      toast.error(t('Failed to delete piece'));
                    }}
                  >
                    <Button
                      {...adminControl(AdminControl.PIECES_DELETE_OPEN)}
                      variant="ghost"
                      size={'sm'}
                      disabled={!isEnabled}
                    >
                      <Trash className="size-4 text-danger-11" />
                    </Button>
                  </ConfirmationDeleteDialog>
                )}
              </div>
            );
          },
        },
      ],
      [isEnabled, refetchPieces, refetchPiecesOAuth2AppsMap],
    );

  return (
    <AdminPage fill>
      <AdminPageHeader
        title={t('Pieces')}
        description={t('Manage the pieces that are available to your users')}
        resources={adminPageResources.pieces}
      >
        <InstallPieceDialog
          onInstallPiece={() => refetchPieces()}
          scope={PieceScope.PLATFORM}
        />
      </AdminPageHeader>
      <PiecesLockedBanner
        message={t(
          "Showing and hiding pieces needs a higher plan. You can browse the catalog, but changes won't stick.",
        )}
      />
      <AdminDataTable
        emptyStateTextTitle={t('No pieces found')}
        emptyStateTextDescription={t(
          'Start by installing pieces that you want to use in your automations',
        )}
        emptyStateIcon={<Package />}
        columns={columns}
        filters={[
          {
            type: 'input',
            title: t('Piece Name'),
            accessorKey: 'name',
            icon: CheckIcon,
          },
        ]}
        page={{
          data: pieces ?? [],
          next: null,
          previous: null,
        }}
        isLoading={isLoading}
        isError={isError}
        errorStateEntity={t('pieces')}
        onRetry={refetchPieces}
        toolbarButtons={[
          <CustomizeSelectorDialog key="customize" isEnabled={isEnabled} />,
          <DownloadPiecesReportButton key="download-report" />,
          <SyncPiecesButton key="sync" />,
        ]}
        virtualizeRows={true}
        hidePagination={true}
      />
    </AdminPage>
  );
};

function shouldShowOauth2SettingForPiece(piece: PieceMetadataModelSummary) {
  const pieceAuth = Array.isArray(piece.auth)
    ? piece.auth.find((auth) => auth.type === PropertyType.OAUTH2)
    : piece.auth;
  if (isNil(pieceAuth)) {
    return false;
  }
  if (pieceAuth.type !== PropertyType.OAUTH2) {
    return false;
  }
  if (pieceAuth.grantType === OAuth2GrantType.CLIENT_CREDENTIALS) {
    return false;
  }
  return true;
}