You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 EditWorkspaceModal from './edit-workspace-modal'
  13. import Operation from './operation'
  14. import { fetchMembers } from '@/service/common'
  15. import I18n from '@/context/i18n'
  16. import { useAppContext } from '@/context/app-context'
  17. import Avatar from '@/app/components/base/avatar'
  18. import type { InvitationResult } from '@/models/common'
  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. import Tooltip from '@/app/components/base/tooltip'
  27. import { RiPencilLine } from '@remixicon/react'
  28. dayjs.extend(relativeTime)
  29. const MembersPage = () => {
  30. const { t } = useTranslation()
  31. const RoleMap = {
  32. owner: t('common.members.owner'),
  33. admin: t('common.members.admin'),
  34. editor: t('common.members.editor'),
  35. dataset_operator: t('common.members.datasetOperator'),
  36. normal: t('common.members.normal'),
  37. }
  38. const { locale } = useContext(I18n)
  39. const { userProfile, currentWorkspace, isCurrentWorkspaceOwner, isCurrentWorkspaceManager, systemFeatures } = useAppContext()
  40. const { data, mutate } = useSWR(
  41. {
  42. url: '/workspaces/current/members',
  43. params: {},
  44. },
  45. fetchMembers,
  46. )
  47. const [inviteModalVisible, setInviteModalVisible] = useState(false)
  48. const [invitationResults, setInvitationResults] = useState<InvitationResult[]>([])
  49. const [invitedModalVisible, setInvitedModalVisible] = useState(false)
  50. const accounts = data?.accounts || []
  51. const { plan, enableBilling } = useProviderContext()
  52. const isNotUnlimitedMemberPlan = enableBilling && plan.type !== Plan.team && plan.type !== Plan.enterprise
  53. const isMemberFull = enableBilling && isNotUnlimitedMemberPlan && accounts.length >= plan.total.teamMembers
  54. const [editWorkspaceModalVisible, setEditWorkspaceModalVisible] = useState(false)
  55. return (
  56. <>
  57. <div className='flex flex-col'>
  58. <div className='mb-4 flex items-center gap-3 rounded-xl border-l-[0.5px] border-t-[0.5px] border-divider-subtle bg-gradient-to-r from-background-gradient-bg-fill-chat-bg-2 to-background-gradient-bg-fill-chat-bg-1 p-3 pr-5'>
  59. <div className='flex h-12 w-12 items-center justify-center rounded-xl bg-components-icon-bg-blue-solid text-[20px]'>
  60. <span className='bg-gradient-to-r from-components-avatar-shape-fill-stop-0 to-components-avatar-shape-fill-stop-100 bg-clip-text font-semibold uppercase text-shadow-shadow-1 opacity-90'>{currentWorkspace?.name[0]?.toLocaleUpperCase()}</span>
  61. </div>
  62. <div className='grow'>
  63. <div className='system-md-semibold flex items-center gap-1 text-text-secondary'>
  64. <span>{currentWorkspace?.name}</span>
  65. {isCurrentWorkspaceOwner && <span>
  66. <Tooltip
  67. popupContent={t('common.account.editWorkspaceInfo')}
  68. needsDelay
  69. >
  70. <div
  71. className='cursor-pointer rounded-md p-1 hover:bg-black/5'
  72. onClick={() => {
  73. setEditWorkspaceModalVisible(true)
  74. }}
  75. >
  76. <RiPencilLine className='h-4 w-4 text-text-tertiary' />
  77. </div>
  78. </Tooltip>
  79. </span>}
  80. </div>
  81. <div className='system-xs-medium mt-1 text-text-tertiary'>
  82. {enableBilling && isNotUnlimitedMemberPlan
  83. ? (
  84. <div className='flex space-x-1'>
  85. <div>{t('billing.plansCommon.member')}{locale !== LanguagesSupported[1] && accounts.length > 1 && 's'}</div>
  86. <div className=''>{accounts.length}</div>
  87. <div>/</div>
  88. <div>{plan.total.teamMembers === NUM_INFINITE ? t('billing.plansCommon.unlimited') : plan.total.teamMembers}</div>
  89. </div>
  90. )
  91. : (
  92. <div className='flex space-x-1'>
  93. <div>{accounts.length}</div>
  94. <div>{t('billing.plansCommon.memberAfter')}{locale !== LanguagesSupported[1] && accounts.length > 1 && 's'}</div>
  95. </div>
  96. )}
  97. </div>
  98. </div>
  99. {isMemberFull && (
  100. <UpgradeBtn className='mr-2' loc='member-invite' />
  101. )}
  102. <Button variant='primary' className={cn('shrink-0')} disabled={!isCurrentWorkspaceManager || isMemberFull} onClick={() => setInviteModalVisible(true)}>
  103. <RiUserAddLine className='mr-1 h-4 w-4' />
  104. {t('common.members.invite')}
  105. </Button>
  106. </div>
  107. <div className='overflow-visible lg:overflow-visible'>
  108. <div className='flex min-w-[480px] items-center border-b border-divider-regular py-[7px]'>
  109. <div className='system-xs-medium-uppercase grow px-3 text-text-tertiary'>{t('common.members.name')}</div>
  110. <div className='system-xs-medium-uppercase w-[104px] shrink-0 text-text-tertiary'>{t('common.members.lastActive')}</div>
  111. <div className='system-xs-medium-uppercase w-[96px] shrink-0 px-3 text-text-tertiary'>{t('common.members.role')}</div>
  112. </div>
  113. <div className='relative min-w-[480px]'>
  114. {
  115. accounts.map(account => (
  116. <div key={account.id} className='flex border-b border-divider-subtle'>
  117. <div className='flex grow items-center px-3 py-2'>
  118. <Avatar avatar={account.avatar_url} size={24} className='mr-2' name={account.name} />
  119. <div className=''>
  120. <div className='system-sm-medium text-text-secondary'>
  121. {account.name}
  122. {account.status === 'pending' && <span className='system-xs-medium ml-1 text-text-warning'>{t('common.members.pending')}</span>}
  123. {userProfile.email === account.email && <span className='system-xs-regular text-text-tertiary'>{t('common.members.you')}</span>}
  124. </div>
  125. <div className='system-xs-regular text-text-tertiary'>{account.email}</div>
  126. </div>
  127. </div>
  128. <div className='system-sm-regular flex w-[104px] shrink-0 items-center py-2 text-text-secondary'>{dayjs(Number((account.last_active_at || account.created_at)) * 1000).locale(locale === 'zh-Hans' ? 'zh-cn' : 'en').fromNow()}</div>
  129. <div className='flex w-[96px] shrink-0 items-center'>
  130. {
  131. isCurrentWorkspaceOwner && account.role !== 'owner'
  132. ? <Operation member={account} operatorRole={currentWorkspace.role} onOperate={mutate} />
  133. : <div className='system-sm-regular px-3 text-text-secondary'>{RoleMap[account.role] || RoleMap.normal}</div>
  134. }
  135. </div>
  136. </div>
  137. ))
  138. }
  139. </div>
  140. </div>
  141. </div>
  142. {
  143. inviteModalVisible && (
  144. <InviteModal
  145. isEmailSetup={systemFeatures.is_email_setup}
  146. onCancel={() => setInviteModalVisible(false)}
  147. onSend={(invitationResults) => {
  148. setInvitedModalVisible(true)
  149. setInvitationResults(invitationResults)
  150. mutate()
  151. }}
  152. />
  153. )
  154. }
  155. {
  156. invitedModalVisible && (
  157. <InvitedModal
  158. invitationResults={invitationResults}
  159. onCancel={() => setInvitedModalVisible(false)}
  160. />
  161. )
  162. }
  163. {
  164. editWorkspaceModalVisible && (
  165. <EditWorkspaceModal
  166. onCancel={() => setEditWorkspaceModalVisible(false)}
  167. />
  168. )
  169. }
  170. </>
  171. )
  172. }
  173. export default MembersPage