index.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. 'use client'
  2. import { Fragment, useCallback, useMemo, useState } from 'react'
  3. import { useContext } from 'use-context-selector'
  4. import { XMarkIcon } from '@heroicons/react/24/outline'
  5. import { useTranslation } from 'react-i18next'
  6. import { ReactMultiEmail } from 'react-multi-email'
  7. import { Listbox, Transition } from '@headlessui/react'
  8. import { CheckIcon } from '@heroicons/react/20/solid'
  9. import cn from 'classnames'
  10. import s from './index.module.css'
  11. import Modal from '@/app/components/base/modal'
  12. import Button from '@/app/components/base/button'
  13. import { inviteMember } from '@/service/common'
  14. import { emailRegex } from '@/config'
  15. import { ToastContext } from '@/app/components/base/toast'
  16. import type { InvitationResult } from '@/models/common'
  17. import 'react-multi-email/dist/style.css'
  18. type IInviteModalProps = {
  19. onCancel: () => void
  20. onSend: (invitationResults: InvitationResult[]) => void
  21. }
  22. const InviteModal = ({
  23. onCancel,
  24. onSend,
  25. }: IInviteModalProps) => {
  26. const { t } = useTranslation()
  27. const [emails, setEmails] = useState<string[]>([])
  28. const { notify } = useContext(ToastContext)
  29. const InvitingRoles = useMemo(() => [
  30. {
  31. name: 'normal',
  32. description: t('common.members.normalTip'),
  33. },
  34. {
  35. name: 'admin',
  36. description: t('common.members.adminTip'),
  37. },
  38. ], [t])
  39. const [role, setRole] = useState(InvitingRoles[0])
  40. const handleSend = useCallback(async () => {
  41. if (emails.map(email => emailRegex.test(email)).every(Boolean)) {
  42. try {
  43. const { result, invitation_results } = await inviteMember({
  44. url: '/workspaces/current/members/invite-email',
  45. body: { emails, role: role.name },
  46. })
  47. if (result === 'success') {
  48. onCancel()
  49. onSend(invitation_results)
  50. }
  51. }
  52. catch (e) {}
  53. }
  54. else {
  55. notify({ type: 'error', message: t('common.members.emailInvalid') })
  56. }
  57. }, [role, emails, notify, onCancel, onSend, t])
  58. return (
  59. <div className={cn(s.wrap)}>
  60. <Modal overflowVisible isShow onClose={() => {}} className={cn(s.modal)} wrapperClassName='z-20'>
  61. <div className='flex justify-between mb-2'>
  62. <div className='text-xl font-semibold text-gray-900'>{t('common.members.inviteTeamMember')}</div>
  63. <XMarkIcon className='w-4 h-4 cursor-pointer' onClick={onCancel} />
  64. </div>
  65. <div className='mb-7 text-[13px] text-gray-500'>{t('common.members.inviteTeamMemberTip')}</div>
  66. <div>
  67. <div className='mb-2 text-sm font-medium text-gray-900'>{t('common.members.email')}</div>
  68. <div className='mb-8 h-36 flex items-stretch'>
  69. <ReactMultiEmail
  70. className={cn('w-full pt-2 px-3 outline-none border-none',
  71. 'appearance-none text-sm text-gray-900 rounded-lg overflow-y-auto',
  72. s.emailsInput,
  73. )}
  74. autoFocus
  75. emails={emails}
  76. inputClassName='bg-transparent'
  77. onChange={setEmails}
  78. getLabel={(email, index, removeEmail) =>
  79. <div data-tag key={index} className={cn(s.emailBackground)}>
  80. <div data-tag-item>{email}</div>
  81. <span data-tag-handle onClick={() => removeEmail(index)}>
  82. ×
  83. </span>
  84. </div>
  85. }
  86. placeholder={t('common.members.emailPlaceholder') || ''}
  87. />
  88. </div>
  89. <Listbox value={role} onChange={setRole}>
  90. <div className="relative pb-6">
  91. <Listbox.Button className="relative w-full py-2 pl-3 pr-10 text-left bg-gray-100 outline-none border-none appearance-none text-sm text-gray-900 rounded-lg">
  92. <span className="block truncate capitalize">{t('common.members.invitedAsRole', { role: t(`common.members.${role.name}`) })}</span>
  93. </Listbox.Button>
  94. <Transition
  95. as={Fragment}
  96. leave="transition ease-in duration-200"
  97. leaveFrom="opacity-200"
  98. leaveTo="opacity-0"
  99. >
  100. <Listbox.Options className="absolute w-full py-1 my-2 overflow-auto text-base bg-white rounded-md shadow-lg max-h-60 ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm">
  101. {InvitingRoles.map(role =>
  102. <Listbox.Option
  103. key={role.name}
  104. className={({ active }) =>
  105. `${active ? ' bg-gray-50 rounded-xl' : ' bg-transparent'}
  106. cursor-default select-none relative py-2 px-4 mx-2 flex flex-col`
  107. }
  108. value={role}
  109. >
  110. {({ selected }) => (
  111. <div className='flex flex-row'>
  112. <span
  113. className={cn(
  114. 'text-indigo-600 w-8',
  115. 'flex items-center',
  116. )}
  117. >
  118. {selected && (<CheckIcon className="h-5 w-5" aria-hidden="true" />)}
  119. </span>
  120. <div className=' flex flex-col flex-grow'>
  121. <span className={`${selected ? 'font-medium' : 'font-normal'} capitalize block truncate`}>
  122. {t(`common.members.${role.name}`)}
  123. </span>
  124. <span className={`${selected ? 'font-medium' : 'font-normal'} capitalize block truncate`}>
  125. {role.description}
  126. </span>
  127. </div>
  128. </div>
  129. )}
  130. </Listbox.Option>,
  131. )}
  132. </Listbox.Options>
  133. </Transition>
  134. </div>
  135. </Listbox>
  136. <Button
  137. tabIndex={0}
  138. className='w-full text-sm font-medium'
  139. onClick={handleSend}
  140. disabled={!emails.length}
  141. type='primary'
  142. >
  143. {t('common.members.sendInvite')}
  144. </Button>
  145. </div>
  146. </Modal>
  147. </div>
  148. )
  149. }
  150. export default InviteModal