index.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 DetailModal from './detail-modal'
  8. import { useContext } from 'use-context-selector'
  9. import { RiAddLine } from '@remixicon/react'
  10. import { useTranslation } from 'react-i18next'
  11. import { fetchKnowledges } from '@/service/common'
  12. import I18n from '@/context/i18n'
  13. import { useAppContext } from '@/context/app-context'
  14. import LogoEmbeddedChatHeader from '@/app/components/base/logo/logo-embedded-chat-header'
  15. import { useProviderContext } from '@/context/provider-context'
  16. import { Plan } from '@/app/components/billing/type'
  17. import Button from '@/app/components/base/button'
  18. import UpgradeBtn from '@/app/components/billing/upgrade-btn'
  19. import { NUM_INFINITE } from '@/app/components/billing/config'
  20. import { LanguagesSupported } from '@/i18n/language'
  21. import cn from '@/utils/classnames'
  22. dayjs.extend(relativeTime)
  23. const KnowledgesPage = () => {
  24. const { t } = useTranslation()
  25. const { locale } = useContext(I18n)
  26. const ServiceTypeMap: any = {
  27. 1: '智能问答',
  28. 2: '智能搜索',
  29. 3: '智能推荐',
  30. }
  31. const StatusMap: any = {
  32. 0: '未启用',
  33. 1: '已启用',
  34. }
  35. const { userProfile, currentWorkspace, isCurrentWorkspaceOwner, isCurrentWorkspaceManager, systemFeatures } = useAppContext()
  36. const { data, mutate }: any = useSWR(
  37. {
  38. url: '/workspaces/123123',
  39. params: {},
  40. },
  41. fetchKnowledges,
  42. )
  43. const [detailModalVisible, setDetailModalVisible] = useState(false)
  44. const [transfer, setTransfer] = useState<any>({
  45. mode: 'add',
  46. id: null,
  47. })
  48. const knowledgeList = data?.data || []
  49. const { plan, enableBilling } = useProviderContext()
  50. const isNotUnlimitedMemberPlan = enableBilling && plan.type !== Plan.team && plan.type !== Plan.enterprise
  51. const isMemberFull = enableBilling && isNotUnlimitedMemberPlan && knowledgeList.length >= plan.total.teamMembers
  52. return (
  53. <>
  54. <div className='flex flex-col'>
  55. <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'>
  56. <LogoEmbeddedChatHeader className='!h-12 !w-12' />
  57. <div className='grow'>
  58. <div className='system-md-semibold text-text-secondary'>{currentWorkspace?.name}</div>
  59. {enableBilling && (
  60. <div className='system-xs-medium mt-1 text-text-tertiary'>
  61. {isNotUnlimitedMemberPlan
  62. ? (
  63. <div className='flex space-x-1'>
  64. <div>{t('billing.plansCommon.member')}{locale !== LanguagesSupported[1] && knowledgeList.length > 1 && 's'}</div>
  65. <div className=''>{knowledgeList.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>{knowledgeList.length}</div>
  73. <div>{t('billing.plansCommon.memberAfter')}{locale !== LanguagesSupported[1] && knowledgeList.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}
  83. onClick={() => {
  84. setTransfer({ mode: 'add', row: null })
  85. setDetailModalVisible(true)
  86. }}>
  87. <RiAddLine className='mr-1 h-4 w-4' />
  88. 新增
  89. </Button>
  90. </div>
  91. <div className='overflow-visible lg:overflow-visible'>
  92. <div className='flex min-w-[480px] items-center border-b border-divider-regular py-[7px]'>
  93. <div className='system-xs-medium-uppercase w-[100px] shrink-0 text-center text-text-tertiary'>服务类型</div>
  94. <div className='system-xs-medium-uppercase shrink-0 grow text-center text-text-tertiary'>系统名称</div>
  95. <div className='system-xs-medium-uppercase shrink-0 grow text-center text-text-tertiary'>URL</div>
  96. <div className='system-xs-medium-uppercase w-[80px] shrink-0 text-center text-text-tertiary'>连接应用状态</div>
  97. <div className='system-xs-medium-uppercase w-[160px] shrink-0 text-center text-text-tertiary'>更新时间</div>
  98. <div className='system-xs-medium-uppercase w-[120px] shrink-0 px-3 text-center text-text-tertiary'>操作</div>
  99. </div>
  100. <div className='relative min-w-[480px]'>
  101. {
  102. knowledgeList.map((know: any) => (
  103. <div key={know.id} className='flex justify-between border-b border-divider-subtle'>
  104. <div className='system-sm-regular w-[100px] shrink-0 py-2 text-center text-text-secondary'>{ServiceTypeMap[Number(know.serviceType)]}</div>
  105. <div className='system-sm-regular shrink-0 grow py-2 text-center text-text-secondary'>{know.serviceName}</div>
  106. <div className='system-sm-regular shrink-0 grow py-2 text-center text-text-secondary'>{know.url}</div>
  107. <div className='system-sm-regular w-[80px] shrink-0 py-2 text-center text-text-secondary'>{StatusMap[Number(know.status)]}</div>
  108. <div className='system-sm-regular w-[160px] shrink-0 py-2 text-center text-text-secondary'>{know.time}</div>
  109. <div className='flex w-[120px] shrink-0 items-center justify-center'>
  110. <Button variant='ghost-accent' size='small' className={cn('shrink-0')} disabled={!isCurrentWorkspaceManager || isMemberFull}
  111. onClick={() => {
  112. setTransfer({
  113. mode: 'edit',
  114. row: JSON.parse(JSON.stringify(know)),
  115. })
  116. setDetailModalVisible(true)
  117. }}>
  118. <RiAddLine className='mr-1 h-4 w-4' />
  119. 编辑
  120. </Button>
  121. <Button variant='ghost' size='small' className={cn('shrink-0 text-red-600')} disabled={!isCurrentWorkspaceManager || isMemberFull} onClick={() => setDetailModalVisible(true)}>
  122. <RiAddLine className='mr-1 h-4 w-4' />
  123. 刪除
  124. </Button>
  125. </div>
  126. </div>
  127. ))
  128. }
  129. </div>
  130. </div>
  131. </div>
  132. {
  133. detailModalVisible && (
  134. <DetailModal
  135. transfer={transfer}
  136. onCancel={() => setDetailModalVisible(false)}
  137. onSend={() => {
  138. mutate()
  139. }}
  140. />
  141. )
  142. }
  143. </>
  144. )
  145. }
  146. export default KnowledgesPage