import React, { useEffect } from "react"; import { mutate } from "swr"; import { Controller, useFieldArray, useForm } from "react-hook-form"; import { Dialog, Transition } from "@headlessui/react"; // services import { WorkspaceService } from "services/workspace.service"; // hooks import useToast from "hooks/use-toast"; // ui import { Button, CustomSelect, Input } from "@plane/ui"; // icons import { Plus, X } from "lucide-react"; // types import { IUser, TUserWorkspaceRole } from "types"; // constants import { ROLE } from "constants/workspace"; // fetch-keys import { WORKSPACE_INVITATIONS } from "constants/fetch-keys"; type Props = { isOpen: boolean; onClose: () => void; workspaceSlug: string; user: IUser | undefined; onSuccess?: () => Promise; }; type EmailRole = { email: string; role: TUserWorkspaceRole; }; type FormValues = { emails: EmailRole[]; }; const defaultValues: FormValues = { emails: [ { email: "", role: 15, }, ], }; const workspaceService = new WorkspaceService(); export const SendWorkspaceInvitationModal: React.FC = (props) => { const { isOpen, onClose, workspaceSlug, user, onSuccess } = props; const { control, reset, handleSubmit, formState: { isSubmitting, errors }, } = useForm(); const { fields, append, remove } = useFieldArray({ control, name: "emails", }); const { setToastAlert } = useToast(); const handleClose = () => { onClose(); const timeout = setTimeout(() => { reset(defaultValues); clearTimeout(timeout); }, 350); }; const onSubmit = async (formData: FormValues) => { if (!workspaceSlug) return; await workspaceService .inviteWorkspace(workspaceSlug, formData, user) .then(async () => { if (onSuccess) await onSuccess(); handleClose(); setToastAlert({ type: "success", title: "Success!", message: "Invitations sent successfully.", }); }) .catch((err) => setToastAlert({ type: "error", title: "Error!", message: `${err.error ?? "Something went wrong. Please try again."}`, }) ) .finally(() => mutate(WORKSPACE_INVITATIONS)); }; const appendField = () => { append({ email: "", role: 15 }); }; useEffect(() => { if (fields.length === 0) append([{ email: "", role: 15 }]); }, [fields, append]); return (
{ if (e.code === "Enter") e.preventDefault(); }} >
Invite people to collaborate

Invite members to work on your workspace.

{fields.map((field, index) => (
( <> {errors.emails?.[index]?.email && ( {errors.emails?.[index]?.email?.message} )} )} />
( {ROLE[value]}} onChange={onChange} width="w-full" input > {Object.entries(ROLE).map(([key, value]) => ( {value} ))} )} />
{fields.length > 1 && ( )}
))}
); };