index.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback, useEffect, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import s from './index.module.css'
  6. import NoData from './no-data'
  7. import Firecrawl from './firecrawl'
  8. import JinaReader from './jina-reader'
  9. import cn from '@/utils/classnames'
  10. import { useModalContext } from '@/context/modal-context'
  11. import type { CrawlOptions, CrawlResultItem } from '@/models/datasets'
  12. import { fetchDataSources } from '@/service/datasets'
  13. import { type DataSourceItem, DataSourceProvider } from '@/models/common'
  14. type Props = {
  15. onPreview: (payload: CrawlResultItem) => void
  16. checkedCrawlResult: CrawlResultItem[]
  17. onCheckedCrawlResultChange: (payload: CrawlResultItem[]) => void
  18. onCrawlProviderChange: (provider: DataSourceProvider) => void
  19. onJobIdChange: (jobId: string) => void
  20. crawlOptions: CrawlOptions
  21. onCrawlOptionsChange: (payload: CrawlOptions) => void
  22. }
  23. const Website: FC<Props> = ({
  24. onPreview,
  25. checkedCrawlResult,
  26. onCheckedCrawlResultChange,
  27. onCrawlProviderChange,
  28. onJobIdChange,
  29. crawlOptions,
  30. onCrawlOptionsChange,
  31. }) => {
  32. const { t } = useTranslation()
  33. const { setShowAccountSettingModal } = useModalContext()
  34. const [isLoaded, setIsLoaded] = useState(false)
  35. const [selectedProvider, setSelectedProvider] = useState<DataSourceProvider>(DataSourceProvider.jinaReader)
  36. const [sources, setSources] = useState<DataSourceItem[]>([])
  37. useEffect(() => {
  38. onCrawlProviderChange(selectedProvider)
  39. }, [selectedProvider, onCrawlProviderChange])
  40. const checkSetApiKey = useCallback(async () => {
  41. const res = await fetchDataSources() as any
  42. setSources(res.sources)
  43. // If users have configured one of the providers, select it.
  44. const availableProviders = res.sources.filter((item: DataSourceItem) =>
  45. [DataSourceProvider.jinaReader, DataSourceProvider.fireCrawl].includes(item.provider),
  46. )
  47. if (availableProviders.length > 0)
  48. setSelectedProvider(availableProviders[0].provider)
  49. }, [])
  50. useEffect(() => {
  51. checkSetApiKey().then(() => {
  52. setIsLoaded(true)
  53. })
  54. // eslint-disable-next-line react-hooks/exhaustive-deps
  55. }, [])
  56. const handleOnConfig = useCallback(() => {
  57. setShowAccountSettingModal({
  58. payload: 'data-source',
  59. onCancelCallback: checkSetApiKey,
  60. })
  61. }, [checkSetApiKey, setShowAccountSettingModal])
  62. if (!isLoaded)
  63. return null
  64. return (
  65. <div>
  66. <div className="mb-4">
  67. <div className="font-medium text-gray-700 mb-2 h-6">
  68. {t('datasetCreation.stepOne.website.chooseProvider')}
  69. </div>
  70. <div className="flex space-x-2">
  71. <button
  72. className={`px-4 py-2 text-sm font-medium rounded-md flex items-center justify-center ${
  73. selectedProvider === DataSourceProvider.jinaReader
  74. ? 'bg-primary-50 text-primary-600'
  75. : 'bg-gray-100 text-gray-600 hover:bg-gray-200'
  76. }`}
  77. onClick={() => setSelectedProvider(DataSourceProvider.jinaReader)}
  78. >
  79. <span className={cn(s.jinaLogo, 'mr-2')} />
  80. <span>Jina Reader</span>
  81. </button>
  82. <button
  83. className={`px-4 py-2 text-sm font-medium rounded-md ${
  84. selectedProvider === DataSourceProvider.fireCrawl
  85. ? 'bg-primary-50 text-primary-600'
  86. : 'bg-gray-100 text-gray-600 hover:bg-gray-200'
  87. }`}
  88. onClick={() => setSelectedProvider(DataSourceProvider.fireCrawl)}
  89. >
  90. 🔥 Firecrawl
  91. </button>
  92. </div>
  93. </div>
  94. {
  95. selectedProvider === DataSourceProvider.fireCrawl
  96. ? sources.find(source => source.provider === DataSourceProvider.fireCrawl)
  97. ? (
  98. <Firecrawl
  99. onPreview={onPreview}
  100. checkedCrawlResult={checkedCrawlResult}
  101. onCheckedCrawlResultChange={onCheckedCrawlResultChange}
  102. onJobIdChange={onJobIdChange}
  103. crawlOptions={crawlOptions}
  104. onCrawlOptionsChange={onCrawlOptionsChange}
  105. />
  106. )
  107. : (
  108. <NoData onConfig={handleOnConfig} provider={selectedProvider} />
  109. )
  110. : sources.find(source => source.provider === DataSourceProvider.jinaReader)
  111. ? (
  112. <JinaReader
  113. onPreview={onPreview}
  114. checkedCrawlResult={checkedCrawlResult}
  115. onCheckedCrawlResultChange={onCheckedCrawlResultChange}
  116. onJobIdChange={onJobIdChange}
  117. crawlOptions={crawlOptions}
  118. onCrawlOptionsChange={onCrawlOptionsChange}
  119. />
  120. )
  121. : (
  122. <NoData onConfig={handleOnConfig} provider={selectedProvider} />
  123. )
  124. }
  125. </div>
  126. )
  127. }
  128. export default React.memo(Website)