index.tsx 6.3 KB

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