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.4KB

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