index.tsx 6.3 KB

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