| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 | 
							- import { useTranslation } from 'react-i18next'
 - import { AddDocumentsStep } from './types'
 - import type { DataSourceOption, Datasource } from '@/app/components/rag-pipeline/components/panel/test-run/types'
 - import { useCallback, useMemo, useRef, useState } from 'react'
 - import { BlockEnum, type Node } from '@/app/components/workflow/types'
 - import type { DataSourceNodeType } from '@/app/components/workflow/nodes/data-source/types'
 - import type { DatasourceType } from '@/models/pipeline'
 - import type { CrawlResultItem, DocumentItem, FileItem } from '@/models/datasets'
 - import produce from 'immer'
 - import type { NotionPage } from '@/models/common'
 - 
 - export const useAddDocumentsSteps = () => {
 -   const { t } = useTranslation()
 -   const [currentStep, setCurrentStep] = useState(1)
 - 
 -   const handleNextStep = useCallback(() => {
 -     setCurrentStep(preStep => preStep + 1)
 -   }, [])
 - 
 -   const handleBackStep = useCallback(() => {
 -     setCurrentStep(preStep => preStep - 1)
 -   }, [])
 - 
 -   const steps = [
 -     {
 -       label: t('datasetPipeline.addDocuments.steps.chooseDatasource'),
 -       value: AddDocumentsStep.dataSource,
 -     },
 -     {
 -       label: t('datasetPipeline.addDocuments.steps.processDocuments'),
 -       value: AddDocumentsStep.processDocuments,
 -     },
 -     {
 -       label: t('datasetPipeline.addDocuments.steps.processingDocuments'),
 -       value: AddDocumentsStep.processingDocuments,
 -     },
 -   ]
 - 
 -   return {
 -     steps,
 -     currentStep,
 -     handleNextStep,
 -     handleBackStep,
 -   }
 - }
 - 
 - export const useDatasourceOptions = (pipelineNodes: Node<DataSourceNodeType>[]) => {
 -   const datasourceNodes = pipelineNodes.filter(node => node.data.type === BlockEnum.DataSource)
 -   const datasources: Datasource[] = useMemo(() => {
 -     return datasourceNodes.map((node) => {
 -       return {
 -         nodeId: node.id,
 -         type: node.data.provider_type as DatasourceType,
 -         description: node.data.datasource_label,
 -         docTitle: 'How to use?',
 -         docLink: '',
 -         fileExtensions: node.data.fileExtensions || [],
 -       }
 -     })
 -   }, [datasourceNodes])
 - 
 -   const options = useMemo(() => {
 -     const options: DataSourceOption[] = []
 -     datasourceNodes.forEach((node) => {
 -       const label = node.data.title
 -       options.push({
 -         label,
 -         value: node.id,
 -         data: node.data,
 -       })
 -     })
 -     return options
 -   }, [datasourceNodes])
 - 
 -   return { datasources, options }
 - }
 - 
 - export const useLocalFile = () => {
 -   const [fileList, setFileList] = useState<FileItem[]>([])
 -   const [currentFile, setCurrentFile] = useState<File | undefined>()
 - 
 -   const previewFile = useRef<DocumentItem>()
 - 
 -   const allFileLoaded = useMemo(() => (fileList.length > 0 && fileList.every(file => file.file.id)), [fileList])
 - 
 -   const updateFile = (fileItem: FileItem, progress: number, list: FileItem[]) => {
 -     const newList = produce(list, (draft) => {
 -       const targetIndex = draft.findIndex(file => file.fileID === fileItem.fileID)
 -       draft[targetIndex] = {
 -         ...draft[targetIndex],
 -         progress,
 -       }
 -     })
 -     setFileList(newList)
 -     previewFile.current = newList[0].file as DocumentItem
 -   }
 - 
 -   const updateFileList = useCallback((preparedFiles: FileItem[]) => {
 -     setFileList(preparedFiles)
 -   }, [])
 - 
 -   const updateCurrentFile = useCallback((file: File) => {
 -     setCurrentFile(file)
 -   }, [])
 - 
 -   const hideFilePreview = useCallback(() => {
 -     setCurrentFile(undefined)
 -   }, [])
 - 
 -   return {
 -     fileList,
 -     previewFile,
 -     allFileLoaded,
 -     updateFile,
 -     updateFileList,
 -     currentFile,
 -     updateCurrentFile,
 -     hideFilePreview,
 -   }
 - }
 - 
 - export const useOnlineDocuments = () => {
 -   const [onlineDocuments, setOnlineDocuments] = useState<NotionPage[]>([])
 -   const [currentDocuments, setCurrentDocuments] = useState<NotionPage | undefined>()
 - 
 -   const previewOnlineDocument = useRef<NotionPage>(onlineDocuments[0])
 - 
 -   const updateOnlineDocuments = (value: NotionPage[]) => {
 -     setOnlineDocuments(value)
 -   }
 - 
 -   const updateCurrentPage = useCallback((page: NotionPage) => {
 -     setCurrentDocuments(page)
 -   }, [])
 - 
 -   const hideOnlineDocumentPreview = useCallback(() => {
 -     setCurrentDocuments(undefined)
 -   }, [])
 - 
 -   return {
 -     onlineDocuments,
 -     previewOnlineDocument,
 -     updateOnlineDocuments,
 -     currentDocuments,
 -     updateCurrentPage,
 -     hideOnlineDocumentPreview,
 -   }
 - }
 - 
 - export const useWebsiteCrawl = () => {
 -   const [websitePages, setWebsitePages] = useState<CrawlResultItem[]>([])
 -   const [currentWebsite, setCurrentWebsite] = useState<CrawlResultItem | undefined>()
 - 
 -   const previewWebsitePage = useRef<CrawlResultItem>(websitePages[0])
 - 
 -   const updateCurrentWebsite = useCallback((website: CrawlResultItem) => {
 -     setCurrentWebsite(website)
 -   }, [])
 - 
 -   const hideWebsitePreview = useCallback(() => {
 -     setCurrentWebsite(undefined)
 -   }, [])
 - 
 -   const updataCheckedCrawlResultChange = useCallback((checkedCrawlResult: CrawlResultItem[]) => {
 -     setWebsitePages(checkedCrawlResult)
 -     previewWebsitePage.current = checkedCrawlResult[0]
 -   }, [])
 - 
 -   return {
 -     websitePages,
 -     previewWebsitePage,
 -     updataCheckedCrawlResultChange,
 -     currentWebsite,
 -     updateCurrentWebsite,
 -     hideWebsitePreview,
 -   }
 - }
 
 
  |