index.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use client'
  2. import { useCallback, 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 RoleSelector from './role-selector'
  8. import s from './index.module.css'
  9. import cn from '@/utils/classnames'
  10. import Modal from '@/app/components/base/modal'
  11. import Button from '@/app/components/base/button'
  12. import { inviteMember } from '@/service/common'
  13. import { emailRegex } from '@/config'
  14. import { ToastContext } from '@/app/components/base/toast'
  15. import type { InvitationResult } from '@/models/common'
  16. import I18n from '@/context/i18n'
  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 { locale } = useContext(I18n)
  30. const [role, setRole] = useState<string>('normal')
  31. const handleSend = useCallback(async () => {
  32. if (emails.map((email: string) => emailRegex.test(email)).every(Boolean)) {
  33. try {
  34. const { result, invitation_results } = await inviteMember({
  35. url: '/workspaces/current/members/invite-email',
  36. body: { emails, role, language: locale },
  37. })
  38. if (result === 'success') {
  39. onCancel()
  40. onSend(invitation_results)
  41. }
  42. }
  43. catch (e) { }
  44. }
  45. else {
  46. notify({ type: 'error', message: t('common.members.emailInvalid') })
  47. }
  48. }, [role, emails, notify, onCancel, onSend, t])
  49. return (
  50. <div className={cn(s.wrap)}>
  51. <Modal overflowVisible isShow onClose={() => { }} className={cn(s.modal)}>
  52. <div className='flex justify-between mb-2'>
  53. <div className='text-xl font-semibold text-gray-900'>{t('common.members.inviteTeamMember')}</div>
  54. <XMarkIcon className='w-4 h-4 cursor-pointer' onClick={onCancel} />
  55. </div>
  56. <div className='mb-7 text-[13px] text-gray-500'>{t('common.members.inviteTeamMemberTip')}</div>
  57. <div>
  58. <div className='mb-2 text-sm font-medium text-gray-900'>{t('common.members.email')}</div>
  59. <div className='mb-8 h-36 flex items-stretch'>
  60. <ReactMultiEmail
  61. className={cn('w-full pt-2 px-3 outline-none border-none',
  62. 'appearance-none text-sm text-gray-900 rounded-lg overflow-y-auto',
  63. s.emailsInput,
  64. )}
  65. autoFocus
  66. emails={emails}
  67. inputClassName='bg-transparent'
  68. onChange={setEmails}
  69. getLabel={(email, index, removeEmail) =>
  70. <div data-tag key={index} className={cn(s.emailBackground)}>
  71. <div data-tag-item>{email}</div>
  72. <span data-tag-handle onClick={() => removeEmail(index)}>
  73. ×
  74. </span>
  75. </div>
  76. }
  77. placeholder={t('common.members.emailPlaceholder') || ''}
  78. />
  79. </div>
  80. <div className='mb-6'>
  81. <RoleSelector value={role} onChange={setRole} />
  82. </div>
  83. <Button
  84. tabIndex={0}
  85. className='w-full'
  86. onClick={handleSend}
  87. disabled={!emails.length}
  88. variant='primary'
  89. >
  90. {t('common.members.sendInvite')}
  91. </Button>
  92. </div>
  93. </Modal>
  94. </div>
  95. )
  96. }
  97. export default InviteModal