index.tsx 6.9 KB

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