panel.tsx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import useSWR from 'swr'
  4. import { useTranslation } from 'react-i18next'
  5. import { useContext } from 'use-context-selector'
  6. import VarReferencePicker from '../_base/components/variable/var-reference-picker'
  7. import OutputVars, { VarItem } from '../_base/components/output-vars'
  8. import Split from '../_base/components/split'
  9. import { useNodeHelpLink } from '../_base/hooks/use-node-help-link'
  10. import useConfig from './use-config'
  11. import type { DocExtractorNodeType } from './types'
  12. import { fetchSupportFileTypes } from '@/service/datasets'
  13. import Field from '@/app/components/workflow/nodes/_base/components/field'
  14. import { BlockEnum, type NodePanelProps } from '@/app/components/workflow/types'
  15. import I18n from '@/context/i18n'
  16. import { LanguagesSupported } from '@/i18n/language'
  17. const i18nPrefix = 'workflow.nodes.docExtractor'
  18. const Panel: FC<NodePanelProps<DocExtractorNodeType>> = ({
  19. id,
  20. data,
  21. }) => {
  22. const { t } = useTranslation()
  23. const { locale } = useContext(I18n)
  24. const link = useNodeHelpLink(BlockEnum.DocExtractor)
  25. const { data: supportFileTypesResponse } = useSWR({ url: '/files/support-type' }, fetchSupportFileTypes)
  26. const supportTypes = supportFileTypesResponse?.allowed_extensions || []
  27. const supportTypesShowNames = (() => {
  28. const extensionMap: { [key: string]: string } = {
  29. md: 'markdown',
  30. pptx: 'pptx',
  31. htm: 'html',
  32. xlsx: 'xlsx',
  33. docx: 'docx',
  34. }
  35. return [...supportTypes]
  36. .map(item => extensionMap[item] || item) // map to standardized extension
  37. .map(item => item.toLowerCase()) // convert to lower case
  38. .filter((item, index, self) => self.indexOf(item) === index) // remove duplicates
  39. .join(locale !== LanguagesSupported[1] ? ', ' : '、 ')
  40. })()
  41. const {
  42. readOnly,
  43. inputs,
  44. handleVarChanges,
  45. filterVar,
  46. } = useConfig(id, data)
  47. return (
  48. <div className='mt-2'>
  49. <div className='px-4 pb-4 space-y-4'>
  50. <Field
  51. title={t(`${i18nPrefix}.inputVar`)}
  52. >
  53. <>
  54. <VarReferencePicker
  55. readonly={readOnly}
  56. nodeId={id}
  57. isShowNodeName
  58. value={inputs.variable_selector || []}
  59. onChange={handleVarChanges}
  60. filterVar={filterVar}
  61. typePlaceHolder='File | Array[File]'
  62. />
  63. <div className='mt-1 py-0.5 text-text-tertiary body-xs-regular'>
  64. {t(`${i18nPrefix}.supportFileTypes`, { types: supportTypesShowNames })}
  65. <a className='text-text-accent' href={link} target='_blank'>{t(`${i18nPrefix}.learnMore`)}</a>
  66. </div>
  67. </>
  68. </Field>
  69. </div>
  70. <Split />
  71. <div className='px-4 pt-4 pb-2'>
  72. <OutputVars>
  73. <VarItem
  74. name='text'
  75. type={inputs.is_array_file ? 'array[string]' : 'string'}
  76. description={t(`${i18nPrefix}.outputVars.text`)}
  77. />
  78. </OutputVars>
  79. </div>
  80. </div>
  81. )
  82. }
  83. export default React.memo(Panel)