index.tsx 6.9 KB

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