index.tsx 6.6 KB

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