panel.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, InputVarType, type NodePanelProps } from '@/app/components/workflow/types'
  15. import I18n from '@/context/i18n'
  16. import { LanguagesSupported } from '@/i18n/language'
  17. import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form'
  18. import ResultPanel from '@/app/components/workflow/run/result-panel'
  19. const i18nPrefix = 'workflow.nodes.docExtractor'
  20. const Panel: FC<NodePanelProps<DocExtractorNodeType>> = ({
  21. id,
  22. data,
  23. }) => {
  24. const { t } = useTranslation()
  25. const { locale } = useContext(I18n)
  26. const link = useNodeHelpLink(BlockEnum.DocExtractor)
  27. const { data: supportFileTypesResponse } = useSWR({ url: '/files/support-type' }, fetchSupportFileTypes)
  28. const supportTypes = supportFileTypesResponse?.allowed_extensions || []
  29. const supportTypesShowNames = (() => {
  30. const extensionMap: { [key: string]: string } = {
  31. md: 'markdown',
  32. pptx: 'pptx',
  33. htm: 'html',
  34. xlsx: 'xlsx',
  35. docx: 'docx',
  36. }
  37. return [...supportTypes]
  38. .map(item => extensionMap[item] || item) // map to standardized extension
  39. .map(item => item.toLowerCase()) // convert to lower case
  40. .filter((item, index, self) => self.indexOf(item) === index) // remove duplicates
  41. .join(locale !== LanguagesSupported[1] ? ', ' : '、 ')
  42. })()
  43. const {
  44. readOnly,
  45. inputs,
  46. handleVarChanges,
  47. filterVar,
  48. // single run
  49. isShowSingleRun,
  50. hideSingleRun,
  51. runningStatus,
  52. handleRun,
  53. handleStop,
  54. runResult,
  55. files,
  56. setFiles,
  57. } = useConfig(id, data)
  58. return (
  59. <div className='mt-2'>
  60. <div className='px-4 pb-4 space-y-4'>
  61. <Field
  62. title={t(`${i18nPrefix}.inputVar`)}
  63. >
  64. <>
  65. <VarReferencePicker
  66. readonly={readOnly}
  67. nodeId={id}
  68. isShowNodeName
  69. value={inputs.variable_selector || []}
  70. onChange={handleVarChanges}
  71. filterVar={filterVar}
  72. typePlaceHolder='File | Array[File]'
  73. />
  74. <div className='mt-1 py-0.5 text-text-tertiary body-xs-regular'>
  75. {t(`${i18nPrefix}.supportFileTypes`, { types: supportTypesShowNames })}
  76. <a className='text-text-accent' href={link} target='_blank'>{t(`${i18nPrefix}.learnMore`)}</a>
  77. </div>
  78. </>
  79. </Field>
  80. </div>
  81. <Split />
  82. <div>
  83. <OutputVars>
  84. <VarItem
  85. name='text'
  86. type={inputs.is_array_file ? 'array[string]' : 'string'}
  87. description={t(`${i18nPrefix}.outputVars.text`)}
  88. />
  89. </OutputVars>
  90. </div>
  91. {
  92. isShowSingleRun && (
  93. <BeforeRunForm
  94. nodeName={inputs.title}
  95. onHide={hideSingleRun}
  96. forms={[
  97. {
  98. inputs: [{
  99. label: t(`${i18nPrefix}.inputVar`)!,
  100. variable: 'files',
  101. type: InputVarType.multiFiles,
  102. required: true,
  103. }],
  104. values: { files },
  105. onChange: keyValue => setFiles((keyValue as any).files),
  106. },
  107. ]}
  108. runningStatus={runningStatus}
  109. onRun={handleRun}
  110. onStop={handleStop}
  111. result={<ResultPanel {...runResult} showSteps={false} />}
  112. />
  113. )
  114. }
  115. </div>
  116. )
  117. }
  118. export default React.memo(Panel)