import React, { useEffect, useRef, useState } from "react"; import Image from "next/image"; import { useTheme } from "next-themes"; import { Listbox, Transition } from "@headlessui/react"; import { Control, Controller, FieldArrayWithId, UseFieldArrayRemove, UseFormGetValues, UseFormSetValue, UseFormWatch, useFieldArray, useForm, } from "react-hook-form"; import { Check, ChevronDown, Plus, XCircle } from "lucide-react"; // services import { WorkspaceService } from "services/workspace.service"; // hooks import useToast from "hooks/use-toast"; // ui import { Button, Input } from "@plane/ui"; // components import { OnboardingStepIndicator } from "components/onboarding/step-indicator"; // hooks import useDynamicDropdownPosition from "hooks/use-dynamic-dropdown"; // types import { IUser, IWorkspace, TOnboardingSteps, TUserWorkspaceRole } from "types"; // constants import { ROLE } from "constants/workspace"; // assets import user1 from "public/users/user-1.png"; import user2 from "public/users/user-2.png"; import userDark from "public/onboarding/user-dark.svg"; import userLight from "public/onboarding/user-light.svg"; type Props = { finishOnboarding: () => Promise; stepChange: (steps: Partial) => Promise; user: IUser | undefined; workspace: IWorkspace | undefined; }; type EmailRole = { email: string; role: TUserWorkspaceRole; role_active: boolean; }; type FormValues = { emails: EmailRole[]; }; type InviteMemberFormProps = { index: number; remove: UseFieldArrayRemove; control: Control; setValue: UseFormSetValue; getValues: UseFormGetValues; watch: UseFormWatch; field: FieldArrayWithId; fields: FieldArrayWithId[]; errors: any; isInvitationDisabled: boolean; setIsInvitationDisabled: (value: boolean) => void; }; // services const workspaceService = new WorkspaceService(); const emailRegex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i; const placeholderEmails = [ "charlie.taylor@frstflit.com", "octave.chanute@frstflit.com", "george.spratt@frstflit.com", "frank.coffyn@frstflit.com", "amos.root@frstflit.com", "edward.deeds@frstflit.com", "charles.m.manly@frstflit.com", "glenn.curtiss@frstflit.com", "thomas.selfridge@frstflit.com", "albert.zahm@frstflit.com", ]; const InviteMemberForm: React.FC = (props) => { const { control, index, fields, remove, errors, isInvitationDisabled, setIsInvitationDisabled, setValue, getValues, watch, } = props; const buttonRef = useRef(null); const dropdownRef = useRef(null); const [isDropdownOpen, setIsDropdownOpen] = useState(false); useDynamicDropdownPosition(isDropdownOpen, () => setIsDropdownOpen(false), buttonRef, dropdownRef); const email = watch(`emails.${index}.email`); const emailOnChange = (event: React.ChangeEvent) => { if (event.target.value === "") { const validEmail = fields.map((_, i) => emailRegex.test(getValues(`emails.${i}.email`))).includes(true); if (validEmail) { setIsInvitationDisabled(false); } else { setIsInvitationDisabled(true); } if (getValues(`emails.${index}.role_active`)) { setValue(`emails.${index}.role_active`, false); } } else { if (!getValues(`emails.${index}.role_active`)) { setValue(`emails.${index}.role_active`, true); } if (isInvitationDisabled && emailRegex.test(event.target.value)) { setIsInvitationDisabled(false); } else if (!isInvitationDisabled && !emailRegex.test(event.target.value)) { setIsInvitationDisabled(true); } } }; return (
( { emailOnChange(event); onChange(event); }} ref={ref} hasError={Boolean(errors.emails?.[index]?.email)} placeholder={placeholderEmails[index % placeholderEmails.length]} className="text-xs sm:text-sm w-full h-12 placeholder:text-onboarding-text-400 border-onboarding-border-100" /> )} />
( { onChange(val); setIsDropdownOpen(false); setValue(`emails.${index}.role_active`, true); }} className="flex-shrink-0 text-left w-full" > setIsDropdownOpen((prev) => !prev)} className="flex items-center px-2.5 h-11 py-2 text-xs justify-between gap-1 w-full rounded-md duration-300" > {ROLE[value]}
{Object.entries(ROLE).map(([key, value]) => ( `cursor-pointer select-none truncate rounded px-1 py-1.5 ${ active || selected ? "bg-onboarding-background-400/40" : "" } ${selected ? "text-onboarding-text-100" : "text-custom-text-200"}` } > {({ selected }) => (
{value}
{selected && }
)}
))}
)} />
{fields.length > 1 && ( )}
{email && !emailRegex.test(email) && (
🤥{" "} That doesn{"'"}t look like an email address.
)}
); }; export const InviteMembers: React.FC = (props) => { const { finishOnboarding, stepChange, workspace } = props; const [isInvitationDisabled, setIsInvitationDisabled] = useState(true); const { setToastAlert } = useToast(); const { resolvedTheme } = useTheme(); const { control, watch, getValues, setValue, handleSubmit, formState: { isSubmitting, errors, isValid }, } = useForm(); const { fields, append, remove } = useFieldArray({ control, name: "emails", }); const nextStep = async () => { const payload: Partial = { workspace_invite: true, }; await stepChange(payload); await finishOnboarding(); }; const onSubmit = async (formData: FormValues) => { if (!workspace) return; let payload = { ...formData }; payload = { emails: payload.emails.filter((email) => email.email !== "") }; await workspaceService .inviteWorkspace(workspace.slug, { emails: payload.emails.map((email) => ({ email: email.email, role: email.role, })), }) .then(async () => { setToastAlert({ type: "success", title: "Success!", message: "Invitations sent successfully.", }); await nextStep(); }) .catch((err) => setToastAlert({ type: "error", title: "Error!", message: err?.error, }) ); }; const appendField = () => { append({ email: "", role: 15, role_active: false }); }; useEffect(() => { if (fields.length === 0) { append( [ { email: "", role: 15, role_active: false }, { email: "", role: 15, role_active: false }, { email: "", role: 15, role_active: false }, ], { focusIndex: 0, } ); } }, [fields, append]); return (