index.tsx 6.4 KB

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