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

5.5 KB
Raw Blame
import { isNil } from '@activepieces/core-utils';
import { zodResolver } from '@hookform/resolvers/zod';
import { t } from 'i18next';
import { Lock, Unlock } from 'lucide-react';
import { useState, forwardRef } from 'react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';

import { adminLayout } from '@/app/components/admin';
import { Button } from '@/components/ui/button';
import {
  Dialog,
  DialogContent,
  DialogFooter,
  DialogTitle,
  DialogTrigger,
} from '@/components/ui/dialog';
import { Form, FormField, FormItem, FormMessage } from '@/components/ui/form';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from '@/components/ui/tooltip';
import { oauthAppsMutations, oauthAppsQueries } from '@/features/connections';
import { AdminControl, adminControl } from '@/lib/admin-control';

type ConfigurePieceOAuth2DialogProps = {
  pieceName: string;
  onConfigurationDone: () => void;
  isEnabled: boolean;
};

const OAuth2FormValues = z.object({
  clientId: z.string().min(1),
  clientSecret: z.string().min(1),
});
type OAuth2FormValues = z.infer<typeof OAuth2FormValues>;

export const ConfigurePieceOAuth2Dialog = forwardRef<
  HTMLButtonElement,
  ConfigurePieceOAuth2DialogProps
>(({ pieceName, onConfigurationDone, isEnabled }, ref) => {
  const [open, setOpen] = useState(false);
  const form = useForm<OAuth2FormValues>({
    resolver: zodResolver(OAuth2FormValues),
  });

  const { oauth2App, refetch } =
    oauthAppsQueries.useOAuthAppConfigured(pieceName);
  const { mutate: deleteOAuth2App, isPending: isDeleting } =
    oauthAppsMutations.useDeleteOAuthApp(refetch, setOpen);
  const { mutate: upsert, isPending: isUpserting } =
    oauthAppsMutations.useUpsertOAuthApp(refetch, setOpen, onConfigurationDone);

  return (
    <Dialog
      open={open}
      onOpenChange={(open) => {
        if (!open) {
          form.reset();
        }
        setOpen(open);
      }}
    >
      <DialogTrigger asChild>
        <Tooltip>
          <TooltipTrigger asChild>
            <Button
              {...adminControl(
                isNil(oauth2App)
                  ? AdminControl.PIECES_OAUTH_CONFIGURE_OPEN
                  : AdminControl.PIECES_OAUTH_DELETE_RUN,
              )}
              ref={ref}
              size={'sm'}
              variant={'ghost'}
              loading={isUpserting || isDeleting}
              disabled={!isEnabled}
              onClick={(e) => {
                if (!isEnabled) {
                  e.preventDefault();
                  return;
                }
                if (isNil(oauth2App)) {
                  setOpen(true);
                } else {
                  deleteOAuth2App(oauth2App.id);
                  onConfigurationDone();
                }
                e.preventDefault();
                e.stopPropagation();
              }}
            >
              {isNil(oauth2App) ? (
                <Unlock className="size-4" />
              ) : (
                <Lock className="size-4 text-danger-11" />
              )}
            </Button>
          </TooltipTrigger>
          <TooltipContent>
            {isNil(oauth2App)
              ? t('Configure OAuth2 App')
              : t('Delete OAuth2 App')}
          </TooltipContent>
        </Tooltip>
      </DialogTrigger>
      <DialogContent className={adminLayout.dialog.md}>
        <DialogTitle>{t('Configure OAuth2 App')}</DialogTitle>

        <Form {...form}>
          <form
            className="grid space-y-4 mt-4"
            onSubmit={form.handleSubmit((data) => {
              upsert({
                clientId: data.clientId,
                clientSecret: data.clientSecret,
                pieceName,
              });
            })}
          >
            <FormField
              name="clientId"
              render={({ field }) => (
                <FormItem className="grid space-y-4">
                  <Label htmlFor="clientId" showRequiredIndicator>
                    {t('Client ID')}
                  </Label>
                  <Input {...field} required id="clientId" />
                  <FormMessage />
                </FormItem>
              )}
            />
            <FormField
              name="clientSecret"
              render={({ field }) => (
                <FormItem className="grid space-y-4">
                  <Label htmlFor="clientSecret" showRequiredIndicator>
                    {t('Client Secret')}
                  </Label>
                  <Input
                    {...field}
                    required
                    id="clientSecret"
                    type="password"
                  />
                  <FormMessage />
                </FormItem>
              )}
            />
            {form?.formState?.errors?.root?.serverError && (
              <FormMessage>
                {form.formState.errors.root.serverError.message}
              </FormMessage>
            )}

            <DialogFooter>
              <Button variant="outline" onClick={() => setOpen(false)}>
                {t('Cancel')}
              </Button>
              <Button
                {...adminControl(AdminControl.PIECES_OAUTH_CONFIGURE_SUBMIT)}
                loading={isUpserting}
                disabled={!form.formState.isValid}
                type="submit"
              >
                {t('Save')}
              </Button>
            </DialogFooter>
          </form>
        </Form>
      </DialogContent>
    </Dialog>
  );
});

ConfigurePieceOAuth2Dialog.displayName = 'ConfigurePieceOAuth2Dialog';