Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use client'
  2. import React, { useCallback, useEffect, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useBoolean } from 'ahooks'
  5. import AppUnavailable from '../../base/app-unavailable'
  6. import StepsNavBar from './steps-nav-bar'
  7. import StepOne from './step-one'
  8. import StepTwo from './step-two'
  9. import StepThree from './step-three'
  10. import { DataSourceType } from '@/models/datasets'
  11. import type { DataSet, File, createDocumentResponse } from '@/models/datasets'
  12. import { fetchDataSource, fetchTenantInfo } from '@/service/common'
  13. import { fetchDataDetail } from '@/service/datasets'
  14. import type { DataSourceNotionPage } from '@/models/common'
  15. import AccountSetting from '@/app/components/header/account-setting'
  16. type Page = DataSourceNotionPage & { workspace_id: string }
  17. type DatasetUpdateFormProps = {
  18. datasetId?: string
  19. }
  20. const DatasetUpdateForm = ({ datasetId }: DatasetUpdateFormProps) => {
  21. const { t } = useTranslation()
  22. const [hasSetAPIKEY, setHasSetAPIKEY] = useState(true)
  23. const [isShowSetAPIKey, { setTrue: showSetAPIKey, setFalse: hideSetAPIkey }] = useBoolean()
  24. const [hasConnection, setHasConnection] = useState(true)
  25. const [isShowDataSourceSetting, { setTrue: showDataSourceSetting, setFalse: hideDataSourceSetting }] = useBoolean()
  26. const [dataSourceType, setDataSourceType] = useState<DataSourceType>(DataSourceType.FILE)
  27. const [step, setStep] = useState(1)
  28. const [indexingTypeCache, setIndexTypeCache] = useState('')
  29. const [file, setFile] = useState<File | undefined>()
  30. const [result, setResult] = useState<createDocumentResponse | undefined>()
  31. const [hasError, setHasError] = useState(false)
  32. const [notionPages, setNotionPages] = useState<Page[]>([])
  33. const updateNotionPages = (value: Page[]) => {
  34. setNotionPages(value)
  35. }
  36. const updateFile = (file?: File) => {
  37. setFile(file)
  38. }
  39. const updateIndexingTypeCache = (type: string) => {
  40. setIndexTypeCache(type)
  41. }
  42. const updateResultCache = (res?: createDocumentResponse) => {
  43. setResult(res)
  44. }
  45. const nextStep = useCallback(() => {
  46. setStep(step + 1)
  47. }, [step, setStep])
  48. const changeStep = useCallback((delta: number) => {
  49. setStep(step + delta)
  50. }, [step, setStep])
  51. const checkAPIKey = async () => {
  52. const data = await fetchTenantInfo({ url: '/info' })
  53. const hasSetKey = data.providers.some(({ is_valid }) => is_valid)
  54. setHasSetAPIKEY(hasSetKey)
  55. }
  56. const checkNotionConnection = async () => {
  57. const { data } = await fetchDataSource({ url: '/data-source/integrates' })
  58. const hasConnection = data.filter(item => item.provider === 'notion') || []
  59. setHasConnection(hasConnection.length > 0)
  60. }
  61. useEffect(() => {
  62. checkAPIKey()
  63. checkNotionConnection()
  64. }, [])
  65. const [detail, setDetail] = useState<DataSet | null>(null)
  66. useEffect(() => {
  67. (async () => {
  68. if (datasetId) {
  69. try {
  70. const detail = await fetchDataDetail(datasetId)
  71. setDetail(detail)
  72. }
  73. catch (e) {
  74. setHasError(true)
  75. }
  76. }
  77. })()
  78. }, [datasetId])
  79. if (hasError)
  80. return <AppUnavailable code={500} unknownReason={t('datasetCreation.error.unavailable') as string} />
  81. return (
  82. <div className='flex' style={{ height: 'calc(100vh - 56px)' }}>
  83. <div className="flex flex-col w-56 overflow-y-auto bg-white border-r border-gray-200 shrink-0">
  84. <StepsNavBar step={step} datasetId={datasetId} />
  85. </div>
  86. <div className="grow bg-white">
  87. {step === 1 && <StepOne
  88. hasConnection={hasConnection}
  89. onSetting={showDataSourceSetting}
  90. datasetId={datasetId}
  91. dataSourceType={dataSourceType}
  92. dataSourceTypeDisable={!!detail?.data_source_type}
  93. changeType={setDataSourceType}
  94. file={file}
  95. updateFile={updateFile}
  96. notionPages={notionPages}
  97. updateNotionPages={updateNotionPages}
  98. onStepChange={nextStep}
  99. />}
  100. {(step === 2 && (!datasetId || (datasetId && !!detail))) && <StepTwo
  101. hasSetAPIKEY={hasSetAPIKEY}
  102. onSetting={showSetAPIKey}
  103. indexingType={detail?.indexing_technique || ''}
  104. datasetId={datasetId}
  105. dataSourceType={dataSourceType}
  106. file={file}
  107. notionPages={notionPages}
  108. onStepChange={changeStep}
  109. updateIndexingTypeCache={updateIndexingTypeCache}
  110. updateResultCache={updateResultCache}
  111. />}
  112. {step === 3 && <StepThree
  113. datasetId={datasetId}
  114. datasetName={detail?.name}
  115. indexingType={detail?.indexing_technique || indexingTypeCache}
  116. creationCache={result}
  117. />}
  118. </div>
  119. {isShowSetAPIKey && <AccountSetting activeTab="provider" onCancel={async () => {
  120. await checkAPIKey()
  121. hideSetAPIkey()
  122. }} />}
  123. {isShowDataSourceSetting && <AccountSetting activeTab="data-source" onCancel={hideDataSourceSetting}/>}
  124. </div>
  125. )
  126. }
  127. export default DatasetUpdateForm