You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hooks.ts 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { useTranslation } from 'react-i18next'
  2. import { AddDocumentsStep } from './types'
  3. import type { DataSourceOption } from '@/app/components/rag-pipeline/components/panel/test-run/types'
  4. import { useCallback, useMemo, useState } from 'react'
  5. import { BlockEnum, type Node } from '@/app/components/workflow/types'
  6. import type { DataSourceNodeType } from '@/app/components/workflow/nodes/data-source/types'
  7. import { useDataSourceStore, useDataSourceStoreWithSelector } from './data-source/store'
  8. import type { DataSourceNotionPageMap, DataSourceNotionWorkspace } from '@/models/common'
  9. import { useShallow } from 'zustand/react/shallow'
  10. export const useAddDocumentsSteps = () => {
  11. const { t } = useTranslation()
  12. const [currentStep, setCurrentStep] = useState(1)
  13. const handleNextStep = useCallback(() => {
  14. setCurrentStep(preStep => preStep + 1)
  15. }, [])
  16. const handleBackStep = useCallback(() => {
  17. setCurrentStep(preStep => preStep - 1)
  18. }, [])
  19. const steps = [
  20. {
  21. label: t('datasetPipeline.addDocuments.steps.chooseDatasource'),
  22. value: AddDocumentsStep.dataSource,
  23. },
  24. {
  25. label: t('datasetPipeline.addDocuments.steps.processDocuments'),
  26. value: AddDocumentsStep.processDocuments,
  27. },
  28. {
  29. label: t('datasetPipeline.addDocuments.steps.processingDocuments'),
  30. value: AddDocumentsStep.processingDocuments,
  31. },
  32. ]
  33. return {
  34. steps,
  35. currentStep,
  36. handleNextStep,
  37. handleBackStep,
  38. }
  39. }
  40. export const useDatasourceOptions = (pipelineNodes: Node<DataSourceNodeType>[]) => {
  41. const datasourceNodes = pipelineNodes.filter(node => node.data.type === BlockEnum.DataSource)
  42. const options = useMemo(() => {
  43. const options: DataSourceOption[] = []
  44. datasourceNodes.forEach((node) => {
  45. const label = node.data.title
  46. options.push({
  47. label,
  48. value: node.id,
  49. data: node.data,
  50. })
  51. })
  52. return options
  53. }, [datasourceNodes])
  54. return options
  55. }
  56. export const useLocalFile = () => {
  57. const {
  58. localFileList: fileList,
  59. currentLocalFile,
  60. } = useDataSourceStoreWithSelector(useShallow(state => ({
  61. localFileList: state.localFileList,
  62. currentLocalFile: state.currentLocalFile,
  63. })))
  64. const dataSourceStore = useDataSourceStore()
  65. const allFileLoaded = useMemo(() => (fileList.length > 0 && fileList.every(file => file.file.id)), [fileList])
  66. const hidePreviewLocalFile = useCallback(() => {
  67. const { setCurrentLocalFile } = dataSourceStore.getState()
  68. setCurrentLocalFile(undefined)
  69. }, [dataSourceStore])
  70. return {
  71. fileList,
  72. allFileLoaded,
  73. currentLocalFile,
  74. hidePreviewLocalFile,
  75. }
  76. }
  77. export const useOnlineDocuments = () => {
  78. const {
  79. documentsData,
  80. currentWorkspaceId,
  81. onlineDocuments,
  82. currentDocument,
  83. } = useDataSourceStoreWithSelector(useShallow(state => ({
  84. documentsData: state.documentsData,
  85. currentWorkspaceId: state.currentWorkspaceId,
  86. onlineDocuments: state.onlineDocuments,
  87. currentDocument: state.currentDocument,
  88. })))
  89. const dataSourceStore = useDataSourceStore()
  90. const currentWorkspace = documentsData.find(workspace => workspace.workspace_id === currentWorkspaceId)
  91. const PagesMapAndSelectedPagesId: DataSourceNotionPageMap = useMemo(() => {
  92. const pagesMap = (documentsData || []).reduce((prev: DataSourceNotionPageMap, next: DataSourceNotionWorkspace) => {
  93. next.pages.forEach((page) => {
  94. prev[page.page_id] = {
  95. ...page,
  96. workspace_id: next.workspace_id,
  97. }
  98. })
  99. return prev
  100. }, {})
  101. return pagesMap
  102. }, [documentsData])
  103. const hidePreviewOnlineDocument = useCallback(() => {
  104. const { setCurrentDocument } = dataSourceStore.getState()
  105. setCurrentDocument(undefined)
  106. }, [dataSourceStore])
  107. return {
  108. currentWorkspace,
  109. onlineDocuments,
  110. currentDocument,
  111. PagesMapAndSelectedPagesId,
  112. hidePreviewOnlineDocument,
  113. }
  114. }
  115. export const useWebsiteCrawl = () => {
  116. const {
  117. websitePages,
  118. currentWebsite,
  119. } = useDataSourceStoreWithSelector(useShallow(state => ({
  120. websitePages: state.websitePages,
  121. currentWebsite: state.currentWebsite,
  122. })))
  123. const dataSourceStore = useDataSourceStore()
  124. const hideWebsitePreview = useCallback(() => {
  125. const { setCurrentWebsite, setPreviewIndex } = dataSourceStore.getState()
  126. setCurrentWebsite(undefined)
  127. setPreviewIndex(-1)
  128. }, [dataSourceStore])
  129. return {
  130. websitePages,
  131. currentWebsite,
  132. hideWebsitePreview,
  133. }
  134. }
  135. export const useOnlineDrive = () => {
  136. const {
  137. fileList,
  138. selectedFileKeys,
  139. } = useDataSourceStoreWithSelector(useShallow(state => ({
  140. fileList: state.fileList,
  141. selectedFileKeys: state.selectedFileKeys,
  142. })))
  143. const selectedOnlineDriveFileList = useMemo(() => {
  144. return selectedFileKeys.map(key => fileList.find(item => item.key === key)!)
  145. }, [fileList, selectedFileKeys])
  146. return {
  147. fileList,
  148. selectedFileKeys,
  149. selectedOnlineDriveFileList,
  150. }
  151. }