index.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use client'
  2. import { useState } from 'react'
  3. import useSWR from 'swr'
  4. import dayjs from 'dayjs'
  5. import 'dayjs/locale/zh-cn'
  6. import relativeTime from 'dayjs/plugin/relativeTime'
  7. import { useContext } from 'use-context-selector'
  8. import { UserPlusIcon } from '@heroicons/react/24/outline'
  9. import { useTranslation } from 'react-i18next'
  10. import InviteModal from './invite-modal'
  11. import InvitedModal from './invited-modal'
  12. import Operation from './operation'
  13. import { fetchMembers } from '@/service/common'
  14. import I18n from '@/context/i18n'
  15. import { useAppContext } from '@/context/app-context'
  16. import Avatar from '@/app/components/base/avatar'
  17. import type { InvitationResult } from '@/models/common'
  18. import LogoEmbededChatHeader from '@/app/components/base/logo/logo-embeded-chat-header'
  19. import { useProviderContext } from '@/context/provider-context'
  20. import { Plan } from '@/app/components/billing/type'
  21. import UpgradeBtn from '@/app/components/billing/upgrade-btn'
  22. import { NUM_INFINITE } from '@/app/components/billing/config'
  23. import { LanguagesSupported } from '@/i18n/language'
  24. dayjs.extend(relativeTime)
  25. const MembersPage = () => {
  26. const { t } = useTranslation()
  27. const RoleMap = {
  28. owner: t('common.members.owner'),
  29. admin: t('common.members.admin'),
  30. normal: t('common.members.normal'),
  31. }
  32. const { locale } = useContext(I18n)
  33. const { userProfile, currentWorkspace, isCurrentWorkspaceManager } = useAppContext()
  34. const { data, mutate } = useSWR({ url: '/workspaces/current/members' }, fetchMembers)
  35. const [inviteModalVisible, setInviteModalVisible] = useState(false)
  36. const [invitationResults, setInvitationResults] = useState<InvitationResult[]>([])
  37. const [invitedModalVisible, setInvitedModalVisible] = useState(false)
  38. const accounts = data?.accounts || []
  39. const owner = accounts.filter(account => account.role === 'owner')?.[0]?.email === userProfile.email
  40. const { plan, enableBilling } = useProviderContext()
  41. const isNotUnlimitedMemberPlan = enableBilling && plan.type !== Plan.team && plan.type !== Plan.enterprise
  42. const isMemberFull = enableBilling && isNotUnlimitedMemberPlan && accounts.length >= plan.total.teamMembers
  43. return (
  44. <>
  45. <div className='flex flex-col'>
  46. <div className='flex items-center mb-4 p-3 bg-gray-50 rounded-2xl'>
  47. <LogoEmbededChatHeader className='!w-10 !h-10' />
  48. <div className='grow mx-2'>
  49. <div className='text-sm font-medium text-gray-900'>{currentWorkspace?.name}</div>
  50. {enableBilling && (
  51. <div className='text-xs text-gray-500'>
  52. {isNotUnlimitedMemberPlan
  53. ? (
  54. <div className='flex space-x-1'>
  55. <div>{t('billing.plansCommon.member')}{locale !== LanguagesSupported[1] && accounts.length > 1 && 's'}</div>
  56. <div className='text-gray-700'>{accounts.length}</div>
  57. <div>/</div>
  58. <div>{plan.total.teamMembers === NUM_INFINITE ? t('billing.plansCommon.unlimited') : plan.total.teamMembers}</div>
  59. </div>
  60. )
  61. : (
  62. <div className='flex space-x-1'>
  63. <div>{accounts.length}</div>
  64. <div>{t('billing.plansCommon.memberAfter')}{locale !== LanguagesSupported[1] && accounts.length > 1 && 's'}</div>
  65. </div>
  66. )}
  67. </div>
  68. )}
  69. </div>
  70. {isMemberFull && (
  71. <UpgradeBtn className='mr-2' loc='member-invite' />
  72. )}
  73. <div className={
  74. `shrink-0 flex items-center py-[7px] px-3 border-[0.5px] border-gray-200
  75. text-[13px] font-medium text-primary-600 bg-white
  76. shadow-xs rounded-lg ${(isCurrentWorkspaceManager && !isMemberFull) ? 'cursor-pointer' : 'grayscale opacity-50 cursor-default'}`
  77. } onClick={() => (isCurrentWorkspaceManager && !isMemberFull) && setInviteModalVisible(true)}>
  78. <UserPlusIcon className='w-4 h-4 mr-2 ' />
  79. {t('common.members.invite')}
  80. </div>
  81. </div>
  82. <div className='overflow-visible lg:overflow-visible'>
  83. <div className='flex items-center py-[7px] border-b border-gray-200 min-w-[480px]'>
  84. <div className='grow px-3 text-xs font-medium text-gray-500'>{t('common.members.name')}</div>
  85. <div className='shrink-0 w-[104px] text-xs font-medium text-gray-500'>{t('common.members.lastActive')}</div>
  86. <div className='shrink-0 w-[96px] px-3 text-xs font-medium text-gray-500'>{t('common.members.role')}</div>
  87. </div>
  88. <div className='min-w-[480px] relative'>
  89. {
  90. accounts.map(account => (
  91. <div key={account.id} className='flex border-b border-gray-100'>
  92. <div className='grow flex items-center py-2 px-3'>
  93. <Avatar size={24} className='mr-2' name={account.name} />
  94. <div className=''>
  95. <div className='text-[13px] font-medium text-gray-700 leading-[18px]'>
  96. {account.name}
  97. {account.status === 'pending' && <span className='ml-1 text-xs text-[#DC6803]'>{t('common.members.pending')}</span>}
  98. {userProfile.email === account.email && <span className='text-xs text-gray-500'>{t('common.members.you')}</span>}
  99. </div>
  100. <div className='text-xs text-gray-500 leading-[18px]'>{account.email}</div>
  101. </div>
  102. </div>
  103. <div className='shrink-0 flex items-center w-[104px] py-2 text-[13px] text-gray-700'>{dayjs(Number((account.last_login_at || account.created_at)) * 1000).locale(locale === 'zh-Hans' ? 'zh-cn' : 'en').fromNow()}</div>
  104. <div className='shrink-0 w-[96px] flex items-center'>
  105. {
  106. (owner && account.role !== 'owner')
  107. ? <Operation member={account} onOperate={mutate} />
  108. : <div className='px-3 text-[13px] text-gray-700'>{RoleMap[account.role] || RoleMap.normal}</div>
  109. }
  110. </div>
  111. </div>
  112. ))
  113. }
  114. </div>
  115. </div>
  116. </div>
  117. {
  118. inviteModalVisible && (
  119. <InviteModal
  120. onCancel={() => setInviteModalVisible(false)}
  121. onSend={(invitationResults) => {
  122. setInvitedModalVisible(true)
  123. setInvitationResults(invitationResults)
  124. mutate()
  125. }}
  126. />
  127. )
  128. }
  129. {
  130. invitedModalVisible && (
  131. <InvitedModal
  132. invitationResults={invitationResults}
  133. onCancel={() => setInvitedModalVisible(false)}
  134. />
  135. )
  136. }
  137. </>
  138. )
  139. }
  140. export default MembersPage