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.5 KB
Raw Blame
import { FlowOperationType, PopulatedFlow } from '@activepieces/shared';
import { zodResolver } from '@hookform/resolvers/zod';
import { useMutation } from '@tanstack/react-query';
import { t } from 'i18next';
import { useEffect, useState } from 'react';
import { FormProvider, useForm } from 'react-hook-form';
import { toast } from 'sonner';
import { z } from 'zod';

import { Button } from '@/components/ui/button';
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from '@/components/ui/dialog';
import { FormField, FormItem, FormMessage } from '@/components/ui/form';
import {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from '@/components/ui/select';
import { projectMembersHooks } from '@/features/members/hooks/project-members-hooks';

import { flowsApi } from '../api/flows-api';

const ChangeOwnerFormSchema = z.object({
  ownerId: z.string({ message: t('Please select an owner') }),
});

type ChangeOwnerFormSchema = z.infer<typeof ChangeOwnerFormSchema>;

type ChangeOwnerDialogProps = {
  children: React.ReactNode;
  flow: PopulatedFlow;
  onOwnerChange: () => void;
};

const ChangeOwnerDialog = ({
  children,
  flow,
  onOwnerChange,
}: ChangeOwnerDialogProps) => {
  const { projectMembers, isLoading } = projectMembersHooks.useProjectMembers();
  const [isDialogOpened, setIsDialogOpened] = useState(false);

  const form = useForm<ChangeOwnerFormSchema>({
    resolver: zodResolver(ChangeOwnerFormSchema),
    defaultValues: {
      ownerId: flow.ownerId ?? '',
    },
  });

  useEffect(() => {
    if (isDialogOpened) {
      form.reset({
        ownerId: flow.ownerId ?? '',
      });
    }
  }, [isDialogOpened, flow.ownerId, form]);
  const { mutate, isPending } = useMutation<
    PopulatedFlow,
    Error,
    ChangeOwnerFormSchema
  >({
    mutationFn: async (data) => {
      return await flowsApi.update(flow.id, {
        type: FlowOperationType.UPDATE_OWNER,
        request: {
          ownerId: data.ownerId,
        },
      });
    },
    onSuccess: () => {
      onOwnerChange();
      setIsDialogOpened(false);
      toast.success(t('Flow owner has been updated'));
    },
  });

  return (
    <Dialog onOpenChange={setIsDialogOpened} open={isDialogOpened}>
      <DialogTrigger asChild>{children}</DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>{t('Change Flow Owner')}</DialogTitle>
          <DialogDescription>
            {t('Select a team member to take ownership of this flow.')}
          </DialogDescription>
        </DialogHeader>
        <FormProvider {...form}>
          <form onSubmit={form.handleSubmit((data) => mutate(data))}>
            <FormField
              control={form.control}
              name="ownerId"
              render={({ field }) => (
                <FormItem>
                  <Select
                    onValueChange={field.onChange}
                    value={field.value || undefined}
                    disabled={
                      isLoading ||
                      !projectMembers ||
                      projectMembers.length === 0
                    }
                  >
                    <SelectTrigger>
                      <SelectValue placeholder={t('Select Owner')} />
                    </SelectTrigger>
                    <SelectContent>
                      <SelectGroup>
                        {projectMembers &&
                          projectMembers.length > 0 &&
                          projectMembers.map((member) => (
                            <SelectItem
                              key={member.userId}
                              value={member.userId}
                            >
                              {member.user.firstName} {member.user.lastName} (
                              {member.user.email})
                            </SelectItem>
                          ))}
                      </SelectGroup>
                    </SelectContent>
                  </Select>
                  <FormMessage />
                </FormItem>
              )}
            />
            {form?.formState?.errors?.root?.serverError && (
              <FormMessage>
                {form.formState.errors.root.serverError.message}
              </FormMessage>
            )}
            <DialogFooter>
              <Button type="submit" loading={isPending}>
                {t('Transfer')}
              </Button>
            </DialogFooter>
          </form>
        </FormProvider>
      </DialogContent>
    </Dialog>
  );
};

export { ChangeOwnerDialog };