|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { observer } from "mobx-react"; |
| 4 | +import { Control, Controller, FieldArrayWithId, FormState } from "react-hook-form"; |
| 5 | +import { X } from "lucide-react"; |
| 6 | +// plane imports |
| 7 | +import { ROLE } from "@plane/constants"; |
| 8 | +import { useTranslation } from "@plane/i18n"; |
| 9 | +import { CustomSelect, Input } from "@plane/ui"; |
| 10 | +import { cn } from "@plane/utils"; |
| 11 | +// hooks |
| 12 | +import { useUserPermissions } from "@/hooks/store"; |
| 13 | +import { InvitationFormValues } from "@/hooks/use-workspace-invitation"; |
| 14 | + |
| 15 | +type TInvitationFieldsProps = { |
| 16 | + workspaceSlug: string; |
| 17 | + fields: FieldArrayWithId<InvitationFormValues, "emails", "id">[]; |
| 18 | + control: Control<InvitationFormValues>; |
| 19 | + formState: FormState<InvitationFormValues>; |
| 20 | + remove: (index: number) => void; |
| 21 | + className?: string; |
| 22 | +}; |
| 23 | + |
| 24 | +export const InvitationFields = observer((props: TInvitationFieldsProps) => { |
| 25 | + const { |
| 26 | + workspaceSlug, |
| 27 | + fields, |
| 28 | + control, |
| 29 | + formState: { errors }, |
| 30 | + remove, |
| 31 | + className, |
| 32 | + } = props; |
| 33 | + // plane hooks |
| 34 | + const { t } = useTranslation(); |
| 35 | + // store hooks |
| 36 | + const { workspaceInfoBySlug } = useUserPermissions(); |
| 37 | + // derived values |
| 38 | + const currentWorkspaceRole = workspaceInfoBySlug(workspaceSlug.toString())?.role; |
| 39 | + |
| 40 | + return ( |
| 41 | + <div className={cn("mb-3 space-y-4", className)}> |
| 42 | + {fields.map((field, index) => ( |
| 43 | + <div key={field.id} className="relative group mb-1 flex items-start justify-between gap-x-4 text-sm w-full"> |
| 44 | + <div className="w-full"> |
| 45 | + <Controller |
| 46 | + control={control} |
| 47 | + name={`emails.${index}.email`} |
| 48 | + rules={{ |
| 49 | + required: t("workspace_settings.settings.members.modal.errors.required"), |
| 50 | + pattern: { |
| 51 | + value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i, |
| 52 | + message: t("workspace_settings.settings.members.modal.errors.invalid"), |
| 53 | + }, |
| 54 | + }} |
| 55 | + render={({ field: { value, onChange, ref } }) => ( |
| 56 | + <> |
| 57 | + <Input |
| 58 | + id={`emails.${index}.email`} |
| 59 | + name={`emails.${index}.email`} |
| 60 | + type="text" |
| 61 | + value={value} |
| 62 | + onChange={onChange} |
| 63 | + ref={ref} |
| 64 | + hasError={Boolean(errors.emails?.[index]?.email)} |
| 65 | + placeholder={t("workspace_settings.settings.members.modal.placeholder")} |
| 66 | + className="w-full text-xs sm:text-sm" |
| 67 | + /> |
| 68 | + {errors.emails?.[index]?.email && ( |
| 69 | + <span className="ml-1 text-xs text-red-500">{errors.emails?.[index]?.email?.message}</span> |
| 70 | + )} |
| 71 | + </> |
| 72 | + )} |
| 73 | + /> |
| 74 | + </div> |
| 75 | + <div className="flex items-center justify-between gap-2 flex-shrink-0 "> |
| 76 | + <div className="flex flex-col gap-1"> |
| 77 | + <Controller |
| 78 | + control={control} |
| 79 | + name={`emails.${index}.role`} |
| 80 | + rules={{ required: true }} |
| 81 | + render={({ field: { value, onChange } }) => ( |
| 82 | + <CustomSelect |
| 83 | + value={value} |
| 84 | + label={<span className="text-xs sm:text-sm">{ROLE[value]}</span>} |
| 85 | + onChange={onChange} |
| 86 | + optionsClassName="w-full" |
| 87 | + className="flex-grow w-24" |
| 88 | + input |
| 89 | + > |
| 90 | + {Object.entries(ROLE).map(([key, value]) => { |
| 91 | + if (currentWorkspaceRole && currentWorkspaceRole >= parseInt(key)) |
| 92 | + return ( |
| 93 | + <CustomSelect.Option key={key} value={parseInt(key)}> |
| 94 | + {value} |
| 95 | + </CustomSelect.Option> |
| 96 | + ); |
| 97 | + })} |
| 98 | + </CustomSelect> |
| 99 | + )} |
| 100 | + /> |
| 101 | + </div> |
| 102 | + {fields.length > 1 && ( |
| 103 | + <div className="flex-item flex w-6"> |
| 104 | + <button type="button" className="place-items-center self-center rounded" onClick={() => remove(index)}> |
| 105 | + <X className="h-4 w-4 text-custom-text-200" /> |
| 106 | + </button> |
| 107 | + </div> |
| 108 | + )} |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + ))} |
| 112 | + </div> |
| 113 | + ); |
| 114 | +}); |
0 commit comments