index.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 Button from '@/app/components/base/button'
  22. import UpgradeBtn from '@/app/components/billing/upgrade-btn'
  23. import { NUM_INFINITE } from '@/app/components/billing/config'
  24. import { LanguagesSupported } from '@/i18n/language'
  25. import cn from '@/utils/classnames'
  26. dayjs.extend(relativeTime)
  27. const MembersPage = () => {
  28. const { t } = useTranslation()
  29. const RoleMap = {
  30. owner: t('common.members.owner'),
  31. admin: t('common.members.admin'),
  32. editor: t('common.members.editor'),
  33. dataset_operator: t('common.members.datasetOperator'),
  34. normal: t('common.members.normal'),
  35. }
  36. const { locale } = useContext(I18n)
  37. const { userProfile, currentWorkspace, isCurrentWorkspaceOwner, isCurrentWorkspaceManager, systemFeatures } = useAppContext()
  38. const { data, mutate } = useSWR(
  39. {
  40. url: '/workspaces/current/members',
  41. params: {},
  42. },
  43. fetchMembers,
  44. )
  45. const [inviteModalVisible, setInviteModalVisible] = useState(false)
  46. const [invitationResults, setInvitationResults] = useState<InvitationResult[]>([])
  47. const [invitedModalVisible, setInvitedModalVisible] = useState(false)
  48. const accounts = data?.accounts || []
  49. const { plan, enableBilling } = useProviderContext()
  50. const isNotUnlimitedMemberPlan = enableBilling && plan.type !== Plan.team && plan.type !== Plan.enterprise
  51. const isMemberFull = enableBilling && isNotUnlimitedMemberPlan && accounts.length >= plan.total.teamMembers
  52. return (
  53. <>
  54. <div className='flex flex-col'>
  55. <div className='flex items-center mb-4 p-3 pr-5 gap-3 bg-gradient-to-r from-background-gradient-bg-fill-chat-bg-2 to-background-gradient-bg-fill-chat-bg-1 rounded-xl border-t-[0.5px] border-l-[0.5px] border-divider-subtle'>
  56. <LogoEmbeddedChatHeader className='!w-12 !h-12' />
  57. <div className='grow'>
  58. <div className='system-md-semibold text-text-secondary'>{currentWorkspace?.name}</div>
  59. {enableBilling && (
  60. <div className='mt-1 system-xs-medium text-text-tertiary'>
  61. {isNotUnlimitedMemberPlan
  62. ? (
  63. <div className='flex space-x-1'>
  64. <div>{t('billing.plansCommon.member')}{locale !== LanguagesSupported[1] && accounts.length > 1 && 's'}</div>
  65. <div className=''>{accounts.length}</div>
  66. <div>/</div>
  67. <div>{plan.total.teamMembers === NUM_INFINITE ? t('billing.plansCommon.unlimited') : plan.total.teamMembers}</div>
  68. </div>
  69. )
  70. : (
  71. <div className='flex space-x-1'>
  72. <div>{accounts.length}</div>
  73. <div>{t('billing.plansCommon.memberAfter')}{locale !== LanguagesSupported[1] && accounts.length > 1 && 's'}</div>
  74. </div>
  75. )}
  76. </div>
  77. )}
  78. </div>
  79. {isMemberFull && (
  80. <UpgradeBtn className='mr-2' loc='member-invite' />
  81. )}
  82. <Button variant='primary' className={cn('shrink-0')} disabled={!isCurrentWorkspaceManager || isMemberFull} onClick={() => setInviteModalVisible(true)}>
  83. <RiUserAddLine className='w-4 h-4 mr-1' />
  84. {t('common.members.invite')}
  85. </Button>
  86. </div>
  87. <div className='overflow-visible lg:overflow-visible'>
  88. <div className='flex items-center py-[7px] border-b border-divider-regular min-w-[480px]'>
  89. <div className='grow px-3 system-xs-medium-uppercase text-text-tertiary'>{t('common.members.name')}</div>
  90. <div className='shrink-0 w-[104px] system-xs-medium-uppercase text-text-tertiary'>{t('common.members.lastActive')}</div>
  91. <div className='shrink-0 w-[96px] px-3 system-xs-medium-uppercase text-text-tertiary'>{t('common.members.role')}</div>
  92. </div>
  93. <div className='min-w-[480px] relative'>
  94. {
  95. accounts.map(account => (
  96. <div key={account.id} className='flex border-b border-divider-subtle'>
  97. <div className='grow flex items-center py-2 px-3'>
  98. <Avatar avatar={account.avatar_url} size={24} className='mr-2' name={account.name} />
  99. <div className=''>
  100. <div className='text-text-secondary system-sm-medium'>
  101. {account.name}
  102. {account.status === 'pending' && <span className='ml-1 system-xs-medium text-text-warning'>{t('common.members.pending')}</span>}
  103. {userProfile.email === account.email && <span className='system-xs-regular text-text-tertiary'>{t('common.members.you')}</span>}
  104. </div>
  105. <div className='text-text-tertiary system-xs-regular'>{account.email}</div>
  106. </div>
  107. </div>
  108. <div className='shrink-0 flex items-center w-[104px] py-2 system-sm-regular text-text-secondary'>{dayjs(Number((account.last_active_at || account.created_at)) * 1000).locale(locale === 'zh-Hans' ? 'zh-cn' : 'en').fromNow()}</div>
  109. <div className='shrink-0 w-[96px] flex items-center'>
  110. {
  111. ((isCurrentWorkspaceOwner && account.role !== 'owner') || (isCurrentWorkspaceManager && !['owner', 'admin'].includes(account.role)))
  112. ? <Operation member={account} operatorRole={currentWorkspace.role} onOperate={mutate} />
  113. : <div className='px-3 system-sm-regular text-text-secondary'>{RoleMap[account.role] || RoleMap.normal}</div>
  114. }
  115. </div>
  116. </div>
  117. ))
  118. }
  119. </div>
  120. </div>
  121. </div>
  122. {
  123. inviteModalVisible && (
  124. <InviteModal
  125. isEmailSetup={systemFeatures.is_email_setup}
  126. onCancel={() => setInviteModalVisible(false)}
  127. onSend={(invitationResults) => {
  128. setInvitedModalVisible(true)
  129. setInvitationResults(invitationResults)
  130. mutate()
  131. }}
  132. />
  133. )
  134. }
  135. {
  136. invitedModalVisible && (
  137. <InvitedModal
  138. invitationResults={invitationResults}
  139. onCancel={() => setInvitedModalVisible(false)}
  140. />
  141. )
  142. }
  143. </>
  144. )
  145. }
  146. export default MembersPage