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.

data-sources.tsx 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import {
  2. useCallback,
  3. useRef,
  4. } from 'react'
  5. import Link from 'next/link'
  6. import { useTranslation } from 'react-i18next'
  7. import { RiArrowRightUpLine } from '@remixicon/react'
  8. import { BlockEnum } from '../types'
  9. import type {
  10. OnSelectBlock,
  11. ToolWithProvider,
  12. } from '../types'
  13. import type { DataSourceDefaultValue, ToolDefaultValue } from './types'
  14. import Tools from './tools'
  15. import { ViewType } from './view-type-select'
  16. import cn from '@/utils/classnames'
  17. import type { ListRef } from '@/app/components/workflow/block-selector/market-place-plugin/list'
  18. import { getMarketplaceUrl } from '@/utils/var'
  19. import { useGlobalPublicStore } from '@/context/global-public-context'
  20. import { DEFAULT_FILE_EXTENSIONS_IN_LOCAL_FILE_DATA_SOURCE } from './constants'
  21. type AllToolsProps = {
  22. className?: string
  23. toolContentClassName?: string
  24. searchText: string
  25. onSelect: OnSelectBlock
  26. dataSources: ToolWithProvider[]
  27. }
  28. const DataSources = ({
  29. className,
  30. toolContentClassName,
  31. searchText,
  32. onSelect,
  33. dataSources,
  34. }: AllToolsProps) => {
  35. const { t } = useTranslation()
  36. const pluginRef = useRef<ListRef>(null)
  37. const wrapElemRef = useRef<HTMLDivElement>(null)
  38. const handleSelect = useCallback((_: any, toolDefaultValue: ToolDefaultValue) => {
  39. let defaultValue: DataSourceDefaultValue = {
  40. plugin_id: toolDefaultValue?.provider_id,
  41. provider_type: toolDefaultValue?.provider_type,
  42. provider_name: toolDefaultValue?.provider_name,
  43. datasource_name: toolDefaultValue?.tool_name,
  44. datasource_label: toolDefaultValue?.tool_label,
  45. title: toolDefaultValue?.title,
  46. }
  47. // Update defaultValue with fileExtensions if this is the local file data source
  48. if (toolDefaultValue?.provider_id === 'langgenius/file' && toolDefaultValue?.provider_name === 'file') {
  49. defaultValue = {
  50. ...defaultValue,
  51. fileExtensions: DEFAULT_FILE_EXTENSIONS_IN_LOCAL_FILE_DATA_SOURCE,
  52. }
  53. }
  54. onSelect(BlockEnum.DataSource, toolDefaultValue && defaultValue)
  55. }, [onSelect])
  56. const { enable_marketplace } = useGlobalPublicStore(s => s.systemFeatures)
  57. return (
  58. <div className={cn(className)}>
  59. <div
  60. ref={wrapElemRef}
  61. className='max-h-[464px] overflow-y-auto'
  62. onScroll={pluginRef.current?.handleScroll}
  63. >
  64. <Tools
  65. className={toolContentClassName}
  66. tools={dataSources}
  67. onSelect={handleSelect as OnSelectBlock}
  68. viewType={ViewType.flat}
  69. hasSearchText={!!searchText}
  70. canNotSelectMultiple
  71. />
  72. {
  73. enable_marketplace && (
  74. <Link
  75. className='system-sm-medium sticky bottom-0 z-10 flex h-8 cursor-pointer items-center rounded-b-lg border-[0.5px] border-t border-components-panel-border bg-components-panel-bg-blur px-4 py-1 text-text-accent-light-mode-only shadow-lg'
  76. href={getMarketplaceUrl('')}
  77. target='_blank'
  78. >
  79. <span>{t('plugin.findMoreInMarketplace')}</span>
  80. <RiArrowRightUpLine className='ml-0.5 h-3 w-3' />
  81. </Link>
  82. )
  83. }
  84. </div>
  85. </div>
  86. )
  87. }
  88. export default DataSources