index.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use client'
  2. import React, { useEffect, useMemo, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useBoolean } from 'ahooks'
  5. import { useContext } from 'use-context-selector'
  6. import { useRouter } from 'next/navigation'
  7. import DatasetDetailContext from '@/context/dataset-detail'
  8. import type { CrawlOptions, CustomFile, FullDocumentDetail } from '@/models/datasets'
  9. import type { MetadataType } from '@/service/datasets'
  10. import { fetchDocumentDetail } from '@/service/datasets'
  11. import Loading from '@/app/components/base/loading'
  12. import StepTwo from '@/app/components/datasets/create/step-two'
  13. import AccountSetting from '@/app/components/header/account-setting'
  14. import AppUnavailable from '@/app/components/base/app-unavailable'
  15. import { useDefaultModel } from '@/app/components/header/account-setting/model-provider-page/hooks'
  16. import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  17. import type { NotionPage } from '@/models/common'
  18. type DocumentSettingsProps = {
  19. datasetId: string
  20. documentId: string
  21. }
  22. const DocumentSettings = ({ datasetId, documentId }: DocumentSettingsProps) => {
  23. const { t } = useTranslation()
  24. const router = useRouter()
  25. const [isShowSetAPIKey, { setTrue: showSetAPIKey, setFalse: hideSetAPIkey }] = useBoolean()
  26. const [hasError, setHasError] = useState(false)
  27. const { indexingTechnique, dataset } = useContext(DatasetDetailContext)
  28. const { data: embeddingsDefaultModel } = useDefaultModel(ModelTypeEnum.textEmbedding)
  29. const saveHandler = () => router.push(`/datasets/${datasetId}/documents/${documentId}`)
  30. const cancelHandler = () => router.back()
  31. const [documentDetail, setDocumentDetail] = useState<FullDocumentDetail | null>(null)
  32. const currentPage = useMemo(() => {
  33. return {
  34. workspace_id: documentDetail?.data_source_info.notion_workspace_id,
  35. page_id: documentDetail?.data_source_info.notion_page_id,
  36. page_name: documentDetail?.name,
  37. page_icon: documentDetail?.data_source_info.notion_page_icon,
  38. type: documentDetail?.data_source_type,
  39. }
  40. }, [documentDetail])
  41. useEffect(() => {
  42. (async () => {
  43. try {
  44. const detail = await fetchDocumentDetail({
  45. datasetId,
  46. documentId,
  47. params: { metadata: 'without' as MetadataType },
  48. })
  49. setDocumentDetail(detail)
  50. }
  51. catch (e) {
  52. setHasError(true)
  53. }
  54. })()
  55. }, [datasetId, documentId])
  56. if (hasError)
  57. return <AppUnavailable code={500} unknownReason={t('datasetCreation.error.unavailable') as string} />
  58. return (
  59. <div className='flex' style={{ height: 'calc(100vh - 56px)' }}>
  60. <div className="grow bg-white">
  61. {!documentDetail && <Loading type='app' />}
  62. {dataset && documentDetail && (
  63. <StepTwo
  64. isAPIKeySet={!!embeddingsDefaultModel}
  65. onSetting={showSetAPIKey}
  66. datasetId={datasetId}
  67. dataSourceType={documentDetail.data_source_type}
  68. notionPages={[currentPage as unknown as NotionPage]}
  69. websitePages={[
  70. {
  71. title: documentDetail.name,
  72. source_url: documentDetail.data_source_info?.url,
  73. markdown: '',
  74. description: '',
  75. },
  76. ]}
  77. websiteCrawlProvider={documentDetail.data_source_info?.provider}
  78. websiteCrawlJobId={documentDetail.data_source_info?.job_id}
  79. crawlOptions={documentDetail.data_source_info as unknown as CrawlOptions}
  80. indexingType={indexingTechnique || ''}
  81. isSetting
  82. documentDetail={documentDetail}
  83. files={[documentDetail.data_source_info.upload_file as CustomFile]}
  84. onSave={saveHandler}
  85. onCancel={cancelHandler}
  86. />
  87. )}
  88. </div>
  89. {isShowSetAPIKey && <AccountSetting activeTab="provider" onCancel={async () => {
  90. hideSetAPIkey()
  91. }} />}
  92. </div>
  93. )
  94. }
  95. export default DocumentSettings