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

3.8 KB
Raw Blame
import { ApErrorParams, ErrorCode, isNil } from '@activepieces/core-utils';
import { Template } from '@activepieces/shared';
import { useMutation } from '@tanstack/react-query';
import { t } from 'i18next';
import React from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { toast } from 'sonner';

import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader } from '@/components/ui/card';
import { Separator } from '@/components/ui/separator';
import { internalErrorToast } from '@/components/ui/sonner';
import { flowHooks } from '@/features/flows/hooks/flow-hooks';
import { api } from '@/lib/api';
import { authenticationSession } from '@/lib/authentication-session';
import { FROM_QUERY_PARAM } from '@/lib/navigation-utils';

import { PieceIconList } from '../../pieces/components/piece-icon-list';

const TemplateViewer = ({ template }: { template: Template }) => {
  const navigate = useNavigate();
  const location = useLocation();
  const token = authenticationSession.getToken();

  const { mutate, isPending } = useMutation({
    mutationFn: async () => {
      const flows = await flowHooks.importFlowsFromTemplates({
        templates: [template],
        projectId: authenticationSession.getProjectId()!,
      });
      return flows[0];
    },
    onSuccess: (data) => {
      navigate(`/flows/${data.id}`);
    },
    onError: (error) => {
      if (api.isError(error)) {
        const apError = error.response?.data as ApErrorParams;
        if (apError.code === ErrorCode.PERMISSION_DENIED) {
          toast.error(t('Import Failed'), {
            description: t("You don't have permission to import this template"),
            duration: 3000,
          });
          return;
        }
      }
      internalErrorToast();
    },
  });

  const handleUseTemplate = () => {
    if (isNil(token)) {
      navigate(
        `/sign-in?${FROM_QUERY_PARAM}=${location.pathname}${location.search}`,
      );
      return;
    }
    mutate();
  };

  return (
    <Card className="min-w-[500px] shadow-lg border-2">
      <>
        <CardHeader className="gap-3 pb-4">
          <h2 className="text-2xl font-semibold tracking-tight">
            {template.name}
          </h2>
          <Separator />
        </CardHeader>
        <CardContent className="space-y-6">
          <div className="space-y-4">
            <div className="flex flex-row w-full justify-between items-center py-2">
              <span className="text-sm font-medium text-gray-11">
                {t('Steps in this flow')}
              </span>
              {template.flows?.[0]?.trigger && (
                <PieceIconList
                  trigger={template.flows[0].trigger}
                  maxNumberOfIconsToShow={5}
                />
              )}
            </div>
            {template.description && (
              <>
                <Separator />
                <div className="space-y-2 py-2">
                  <h3 className="text-sm font-semibold">{t('Description')}</h3>
                  <p className="text-sm text-gray-11 leading-relaxed">
                    {template.description}
                  </p>
                </div>
              </>
            )}
          </div>
          <div className="flex items-center justify-end pt-2">
            <Button loading={isPending} onClick={handleUseTemplate} size="lg">
              {t('Use Template')}
            </Button>
          </div>
        </CardContent>
      </>
    </Card>
  );
};

const ShareTemplate: React.FC<{ template: Template }> = ({ template }) => {
  return (
    <div className="flex items-center justify-center min-h-screen w-full bg-gradient-to-br from-gray-1 to-gray-3/20 p-6">
      <div className="w-full max-w-2xl">
        <TemplateViewer template={template} />
      </div>
    </div>
  );
};

export { ShareTemplate };