import React, { useEffect } from "react"; import { useRouter } from "next/router"; import { observer } from "mobx-react-lite"; import { useForm, Controller, useFieldArray } from "react-hook-form"; import { Dialog, Transition } from "@headlessui/react"; import { ChevronDown, Plus, X } from "lucide-react"; // hooks import { useApplication, useMember, useUser, useWorkspace } from "hooks/store"; import useToast from "hooks/use-toast"; // ui import { Avatar, Button, CustomSelect, CustomSearchSelect } from "@plane/ui"; // constants import { ROLE } from "constants/workspace"; import { EUserProjectRoles } from "constants/project"; type Props = { isOpen: boolean; onClose: () => void; onSuccess?: () => void; }; type member = { role: EUserProjectRoles; member_id: string; }; type FormValues = { members: member[]; }; const defaultValues: FormValues = { members: [ { role: 5, member_id: "", }, ], }; export const SendProjectInvitationModal: React.FC = observer((props) => { const { isOpen, onClose, onSuccess } = props; // router const router = useRouter(); const { workspaceSlug, projectId } = router.query; // toast alert const { setToastAlert } = useToast(); // store hooks const { eventTracker: { postHogEventTracker }, } = useApplication(); const { membership: { currentProjectRole }, } = useUser(); const { currentWorkspace } = useWorkspace(); const { project: { projectMemberIds, bulkAddMembersToProject }, workspace: { workspaceMemberIds, getWorkspaceMemberDetails }, } = useMember(); // form info const { formState: { errors, isSubmitting }, reset, handleSubmit, control, } = useForm(); const { fields, append, remove } = useFieldArray({ control, name: "members", }); const uninvitedPeople = workspaceMemberIds?.filter((userId) => { const isInvited = projectMemberIds?.find((u) => u === userId); return !isInvited; }); const onSubmit = async (formData: FormValues) => { if (!workspaceSlug || !projectId || isSubmitting) return; const payload = { ...formData }; await bulkAddMembersToProject(workspaceSlug.toString(), projectId.toString(), payload) .then((res) => { if (onSuccess) onSuccess(); onClose(); setToastAlert({ title: "Success", type: "success", message: "Members added successfully.", }); postHogEventTracker( "MEMBER_ADDED", { ...res, state: "SUCCESS", }, { isGrouping: true, groupType: "Workspace_metrics", groupId: currentWorkspace?.id!, } ); }) .catch((error) => { console.error(error); postHogEventTracker( "MEMBER_ADDED", { state: "FAILED", }, { isGrouping: true, groupType: "Workspace_metrics", groupId: currentWorkspace?.id!, } ); }) .finally(() => { reset(defaultValues); }); }; const handleClose = () => { onClose(); const timeout = setTimeout(() => { reset(defaultValues); clearTimeout(timeout); }, 500); }; const appendField = () => { append({ role: 5, member_id: "", }); }; useEffect(() => { if (fields.length === 0) { append([ { role: 5, member_id: "", }, ]); } }, [fields, append]); const options = uninvitedPeople?.map((userId) => { const memberDetails = getWorkspaceMemberDetails(userId); return { value: `${memberDetails?.member.id}`, query: `${memberDetails?.member.first_name} ${ memberDetails?.member.last_name } ${memberDetails?.member.display_name.toLowerCase()}`, content: (
{memberDetails?.member.display_name} ( {memberDetails?.member.first_name + " " + memberDetails?.member.last_name})
), }; }); return (
Invite Members

Invite members to work on your project.

{fields.map((field, index) => (
{ const selectedMember = getWorkspaceMemberDetails(value); return ( {value && value !== "" ? (
{selectedMember?.member.display_name}
) : (
Select co-worker
)}
( {field.value ? ROLE[field.value] : "Select role"}
} input optionsClassName="w-full" > {Object.entries(ROLE).map(([key, label]) => { if (parseInt(key) > (currentProjectRole ?? EUserProjectRoles.GUEST)) return null; return ( {label} ); })} )} /> {errors.members && errors.members[index]?.role && ( {errors.members[index]?.role?.message} )}
{fields.length > 1 && ( )}
))}
); });