import React, { useEffect, useRef, useState } from "react"; // headless ui import { Listbox, Transition } from "@headlessui/react"; // react-hook-form import { Control, Controller, FieldArrayWithId, UseFieldArrayRemove, useFieldArray, useForm } from "react-hook-form"; // services import { WorkspaceService } from "services/workspace.service"; // hooks import useToast from "hooks/use-toast"; // ui import { Button, Input } from "@plane/ui"; // hooks import useDynamicDropdownPosition from "hooks/use-dynamic-dropdown"; // icons import { Check, ChevronDown, Plus, X } from "lucide-react"; // types import { IUser, IWorkspace, TOnboardingSteps, TUserWorkspaceRole } from "types"; // constants import { ROLE } from "constants/workspace"; type Props = { finishOnboarding: () => Promise; stepChange: (steps: Partial) => Promise; user: IUser | undefined; workspace: IWorkspace | undefined; }; type EmailRole = { email: string; role: TUserWorkspaceRole; }; type FormValues = { emails: EmailRole[]; }; type InviteMemberFormProps = { index: number; remove: UseFieldArrayRemove; control: Control; field: FieldArrayWithId; fields: FieldArrayWithId[]; errors: any; }; // services const workspaceService = new WorkspaceService(); const InviteMemberForm: React.FC = (props) => { const { control, index, fields, remove, errors } = props; const buttonRef = useRef(null); const dropdownRef = useRef(null); const [isDropdownOpen, setIsDropdownOpen] = useState(false); useDynamicDropdownPosition(isDropdownOpen, () => setIsDropdownOpen(false), buttonRef, dropdownRef); return (
( )} />
( { onChange(val); setIsDropdownOpen(false); }} className="flex-shrink-0 text-left w-full" > setIsDropdownOpen((prev) => !prev)} className="flex items-center px-2.5 py-2 text-xs justify-between gap-1 w-full rounded-md border border-custom-border-300 shadow-sm duration-300 focus:outline-none" > {ROLE[value]}
{Object.entries(ROLE).map(([key, value]) => ( `cursor-pointer select-none truncate rounded px-1 py-1.5 ${ active || selected ? "bg-custom-background-80" : "" } ${selected ? "text-custom-text-100" : "text-custom-text-200"}` } > {({ selected }) => (
{value}
{selected && }
)}
))}
)} />
{fields.length > 1 && ( )}
); }; export const InviteMembers: React.FC = (props) => { const { finishOnboarding, stepChange, user, workspace } = props; const { setToastAlert } = useToast(); const { control, handleSubmit, formState: { isSubmitting, errors, isValid }, } = useForm(); const { fields, append, remove } = useFieldArray({ control, name: "emails", }); const nextStep = async () => { const payload: Partial = { workspace_invite: true, workspace_join: true, }; await stepChange(payload); await finishOnboarding(); }; const onSubmit = async (formData: FormValues) => { if (!workspace) return; const payload = { ...formData }; await workspaceService .inviteWorkspace(workspace.slug, payload, user) .then(async () => { setToastAlert({ type: "success", title: "Success!", message: "Invitations sent successfully.", }); await nextStep(); }) .catch((err) => console.log(err)); }; const appendField = () => { append({ email: "", role: 15 }); }; useEffect(() => { if (fields.length === 0) { append([ { email: "", role: 15 }, { email: "", role: 15 }, { email: "", role: 15 }, ]); } }, [fields, append]); return (
{ if (e.code === "Enter") e.preventDefault(); }} >

Invite people to collaborate

Co-workers Email
Role
{fields.map((field, index) => ( ))}
); };