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.

panel.tsx 4.0KB

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='space-y-4 px-4 pb-4'>
  61. <Field
  62. title={t(`${i18nPrefix}.inputVar`)}
  63. required
  64. >
  65. <>
  66. <VarReferencePicker
  67. readonly={readOnly}
  68. nodeId={id}
  69. isShowNodeName
  70. value={inputs.variable_selector || []}
  71. onChange={handleVarChanges}
  72. filterVar={filterVar}
  73. typePlaceHolder='File | Array[File]'
  74. />
  75. <div className='body-xs-regular mt-1 py-0.5 text-text-tertiary'>
  76. {t(`${i18nPrefix}.supportFileTypes`, { types: supportTypesShowNames })}
  77. <a className='text-text-accent' href={link} target='_blank'>{t(`${i18nPrefix}.learnMore`)}</a>
  78. </div>
  79. </>
  80. </Field>
  81. </div>
  82. <Split />
  83. <div>
  84. <OutputVars>
  85. <VarItem
  86. name='text'
  87. type={inputs.is_array_file ? 'array[string]' : 'string'}
  88. description={t(`${i18nPrefix}.outputVars.text`)}
  89. />
  90. </OutputVars>
  91. </div>
  92. {
  93. isShowSingleRun && (
  94. <BeforeRunForm
  95. nodeName={inputs.title}
  96. onHide={hideSingleRun}
  97. forms={[
  98. {
  99. inputs: [{
  100. label: t(`${i18nPrefix}.inputVar`)!,
  101. variable: 'files',
  102. type: InputVarType.multiFiles,
  103. required: true,
  104. }],
  105. values: { files },
  106. onChange: keyValue => setFiles(keyValue.files),
  107. },
  108. ]}
  109. runningStatus={runningStatus}
  110. onRun={handleRun}
  111. onStop={handleStop}
  112. result={<ResultPanel {...runResult} showSteps={false} />}
  113. />
  114. )
  115. }
  116. </div>
  117. )
  118. }
  119. export default React.memo(Panel)