2023-05-15 08:51:32 +08:00
|
|
|
|
'use client'
|
2024-07-04 16:21:40 +08:00
|
|
|
|
import { useCallback, useState } from 'react'
|
2023-05-15 08:51:32 +08:00
|
|
|
|
import { useContext } from 'use-context-selector'
|
|
|
|
|
import { XMarkIcon } from '@heroicons/react/24/outline'
|
|
|
|
|
import { useTranslation } from 'react-i18next'
|
2023-08-31 01:18:31 +08:00
|
|
|
|
import { ReactMultiEmail } from 'react-multi-email'
|
|
|
|
|
import cn from 'classnames'
|
2023-07-14 11:19:26 +08:00
|
|
|
|
import s from './index.module.css'
|
2024-07-04 16:21:40 +08:00
|
|
|
|
import RoleSelector from './role-selector'
|
2023-05-15 08:51:32 +08:00
|
|
|
|
import Modal from '@/app/components/base/modal'
|
|
|
|
|
import Button from '@/app/components/base/button'
|
|
|
|
|
import { inviteMember } from '@/service/common'
|
|
|
|
|
import { emailRegex } from '@/config'
|
|
|
|
|
import { ToastContext } from '@/app/components/base/toast'
|
2023-08-31 01:18:31 +08:00
|
|
|
|
import type { InvitationResult } from '@/models/common'
|
2024-01-28 19:56:09 +08:00
|
|
|
|
import I18n from '@/context/i18n'
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
2023-08-31 01:18:31 +08:00
|
|
|
|
import 'react-multi-email/dist/style.css'
|
2023-07-14 11:19:26 +08:00
|
|
|
|
type IInviteModalProps = {
|
|
|
|
|
onCancel: () => void
|
2023-08-31 01:18:31 +08:00
|
|
|
|
onSend: (invitationResults: InvitationResult[]) => void
|
2023-05-15 08:51:32 +08:00
|
|
|
|
}
|
2023-08-31 01:18:31 +08:00
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
|
const InviteModal = ({
|
|
|
|
|
onCancel,
|
|
|
|
|
onSend,
|
|
|
|
|
}: IInviteModalProps) => {
|
|
|
|
|
const { t } = useTranslation()
|
2023-08-31 01:18:31 +08:00
|
|
|
|
const [emails, setEmails] = useState<string[]>([])
|
2023-05-15 08:51:32 +08:00
|
|
|
|
const { notify } = useContext(ToastContext)
|
|
|
|
|
|
2024-01-28 19:56:09 +08:00
|
|
|
|
const { locale } = useContext(I18n)
|
2024-07-04 16:21:40 +08:00
|
|
|
|
const [role, setRole] = useState<string>('normal')
|
2023-08-31 01:18:31 +08:00
|
|
|
|
|
|
|
|
|
const handleSend = useCallback(async () => {
|
2024-01-28 19:56:09 +08:00
|
|
|
|
if (emails.map((email: string) => emailRegex.test(email)).every(Boolean)) {
|
2023-05-15 08:51:32 +08:00
|
|
|
|
try {
|
2023-08-31 01:18:31 +08:00
|
|
|
|
const { result, invitation_results } = await inviteMember({
|
|
|
|
|
url: '/workspaces/current/members/invite-email',
|
2024-07-04 16:21:40 +08:00
|
|
|
|
body: { emails, role, language: locale },
|
2023-08-31 01:18:31 +08:00
|
|
|
|
})
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
2023-08-31 01:18:31 +08:00
|
|
|
|
if (result === 'success') {
|
2023-05-15 08:51:32 +08:00
|
|
|
|
onCancel()
|
2023-08-31 01:18:31 +08:00
|
|
|
|
onSend(invitation_results)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-19 14:13:16 +08:00
|
|
|
|
catch (e) { }
|
2023-07-14 11:19:26 +08:00
|
|
|
|
}
|
|
|
|
|
else {
|
2023-05-15 08:51:32 +08:00
|
|
|
|
notify({ type: 'error', message: t('common.members.emailInvalid') })
|
|
|
|
|
}
|
2023-08-31 01:18:31 +08:00
|
|
|
|
}, [role, emails, notify, onCancel, onSend, t])
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
|
|
return (
|
2023-10-13 14:58:53 +08:00
|
|
|
|
<div className={cn(s.wrap)}>
|
2024-06-15 21:09:19 +08:00
|
|
|
|
<Modal overflowVisible isShow onClose={() => {}} className={cn(s.modal)}>
|
2023-05-15 08:51:32 +08:00
|
|
|
|
<div className='flex justify-between mb-2'>
|
|
|
|
|
<div className='text-xl font-semibold text-gray-900'>{t('common.members.inviteTeamMember')}</div>
|
|
|
|
|
<XMarkIcon className='w-4 h-4 cursor-pointer' onClick={onCancel} />
|
|
|
|
|
</div>
|
|
|
|
|
<div className='mb-7 text-[13px] text-gray-500'>{t('common.members.inviteTeamMemberTip')}</div>
|
|
|
|
|
<div>
|
|
|
|
|
<div className='mb-2 text-sm font-medium text-gray-900'>{t('common.members.email')}</div>
|
2023-08-31 01:18:31 +08:00
|
|
|
|
<div className='mb-8 h-36 flex items-stretch'>
|
|
|
|
|
<ReactMultiEmail
|
|
|
|
|
className={cn('w-full pt-2 px-3 outline-none border-none',
|
|
|
|
|
'appearance-none text-sm text-gray-900 rounded-lg overflow-y-auto',
|
|
|
|
|
s.emailsInput,
|
|
|
|
|
)}
|
|
|
|
|
autoFocus
|
|
|
|
|
emails={emails}
|
|
|
|
|
inputClassName='bg-transparent'
|
|
|
|
|
onChange={setEmails}
|
|
|
|
|
getLabel={(email, index, removeEmail) =>
|
|
|
|
|
<div data-tag key={index} className={cn(s.emailBackground)}>
|
|
|
|
|
<div data-tag-item>{email}</div>
|
|
|
|
|
<span data-tag-handle onClick={() => removeEmail(index)}>
|
2024-06-19 14:13:16 +08:00
|
|
|
|
×
|
2023-08-31 01:18:31 +08:00
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
placeholder={t('common.members.emailPlaceholder') || ''}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2024-07-04 16:21:40 +08:00
|
|
|
|
<div className='mb-6'>
|
|
|
|
|
<RoleSelector value={role} onChange={setRole} />
|
|
|
|
|
</div>
|
2023-07-14 11:19:26 +08:00
|
|
|
|
<Button
|
2023-08-31 01:18:31 +08:00
|
|
|
|
tabIndex={0}
|
2024-06-21 14:17:45 +08:00
|
|
|
|
className='w-full'
|
2023-05-15 08:51:32 +08:00
|
|
|
|
onClick={handleSend}
|
2023-08-31 01:18:31 +08:00
|
|
|
|
disabled={!emails.length}
|
2024-06-19 14:13:16 +08:00
|
|
|
|
variant='primary'
|
2023-05-15 08:51:32 +08:00
|
|
|
|
>
|
|
|
|
|
{t('common.members.sendInvite')}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</Modal>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default InviteModal
|