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

4.0 KB
Raw Blame
import {
  ApEdition,
  ApFlagId,
  PlatformAdminSurface,
} from '@activepieces/shared';
import { t } from 'i18next';
import { Check, Gem, ExternalLink } from 'lucide-react';
import { ReactNode, useState } from 'react';

import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from '@/components/ui/dialog';
import { flagsHooks } from '@/hooks/flags-hooks';
import { AdminControl, adminControl } from '@/lib/admin-control';

import { FeatureKey } from '../components/request-trial';
import { FeatureTier, TIER_LABELS } from '../utils/feature-tier';

import { useUpgradeClick } from './use-upgrade-click';

export function useFeatureGate({ locked, feature }: UseFeatureGateParams) {
  const [open, setOpen] = useState(false);

  return {
    locked,
    crown: locked ? (
      <Gem className="size-3.5 shrink-0 text-on-accent/90" />
    ) : null,
    open: () => setOpen(true),
    dialog: (
      <UpgradeFeatureDialog open={open} onOpenChange={setOpen} {...feature} />
    ),
  };
}

export function UpgradeFeatureDialog({
  open,
  onOpenChange,
  title,
  description,
  bullets,
  tier,
  documentationUrl,
  featureKey,
}: UpgradeFeatureDialogProps) {
  const upgradeClick = useUpgradeClick();
  const { data: edition } = flagsHooks.useFlag<ApEdition>(ApFlagId.EDITION);
  const isCommunity = edition === ApEdition.COMMUNITY;
  const docsUrl = documentationUrl ?? ENTERPRISE_DOCUMENTATION_URL;

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-[28rem]">
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2">
            {t(title)}
            {tier !== undefined && !isCommunity && (
              <Badge variant="outline">{TIER_LABELS[tier]}</Badge>
            )}
          </DialogTitle>
          <DialogDescription>
            {isCommunity
              ? t('This is an Enterprise feature, available on our paid plans.')
              : t(description)}
          </DialogDescription>
        </DialogHeader>

        {!isCommunity && bullets !== undefined && bullets.length > 0 && (
          <ul className="flex flex-col gap-2">
            {bullets.map((bullet) => (
              <li key={bullet} className="flex items-start gap-2 text-sm">
                <Check className="mt-0.5 size-4 shrink-0 text-accent-11" />
                <span>{t(bullet)}</span>
              </li>
            ))}
          </ul>
        )}

        <DialogFooter className="sm:justify-start">
          {isCommunity ? (
            <a
              {...adminControl(AdminControl.PLAN_DIALOG_LINK)}
              href={docsUrl}
              target="_blank"
              rel="noopener noreferrer"
              className="inline-flex items-center gap-1 text-sm font-medium text-accent-11 hover:underline"
            >
              {t('Read the docs')}
              <ExternalLink className="size-3.5" />
            </a>
          ) : (
            <Button
              onClick={() => {
                onOpenChange(false);
                upgradeClick({
                  feature: featureKey,
                  tier,
                  surface: PlatformAdminSurface.DIALOG,
                });
              }}
            >
              {t('Upgrade plan')}
            </Button>
          )}
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}

const ENTERPRISE_DOCUMENTATION_URL =
  'https://www.activepieces.com/docs/install/configuration/overview#enterprise-edition-optional';

export type PlatformFeature = {
  featureKey: FeatureKey;
  title: string;
  description: string;
  bullets?: string[];
  tier?: FeatureTier;
  documentationUrl?: string;
  videoUrl?: string;
};

export type UseFeatureGateParams = {
  locked: boolean;
  feature: PlatformFeature;
};

export type UpgradeFeatureDialogProps = PlatformFeature & {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  children?: ReactNode;
};