textarea.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import type { FC } from 'react'
  2. import { useContext } from 'use-context-selector'
  3. import { useTranslation } from 'react-i18next'
  4. import cn from 'classnames'
  5. import Button from '../../base/button'
  6. import Tag from '../../base/tag'
  7. import Tooltip from '../../base/tooltip'
  8. import { getIcon } from '../common/retrieval-method-info'
  9. import s from './style.module.css'
  10. import DatasetDetailContext from '@/context/dataset-detail'
  11. import type { HitTestingResponse } from '@/models/datasets'
  12. import { hitTesting } from '@/service/datasets'
  13. import { asyncRunSafe } from '@/utils'
  14. import { RETRIEVE_METHOD, type RetrievalConfig } from '@/types/app'
  15. type Props = {
  16. datasetId: string
  17. onUpdateList: () => void
  18. setHitResult: (res: HitTestingResponse) => void
  19. loading: boolean
  20. setLoading: (v: boolean) => void
  21. text: string
  22. setText: (v: string) => void
  23. onClickRetrievalMethod: () => void
  24. retrievalConfig: RetrievalConfig
  25. isEconomy: boolean
  26. }
  27. const TextAreaWithButton: FC<Props> = ({
  28. datasetId,
  29. onUpdateList,
  30. setHitResult,
  31. setLoading,
  32. loading,
  33. text,
  34. setText,
  35. onClickRetrievalMethod,
  36. retrievalConfig,
  37. isEconomy,
  38. }) => {
  39. const { t } = useTranslation()
  40. const { indexingTechnique } = useContext(DatasetDetailContext)
  41. function handleTextChange(event: any) {
  42. setText(event.target.value)
  43. }
  44. const onSubmit = async () => {
  45. setLoading(true)
  46. const [e, res] = await asyncRunSafe<HitTestingResponse>(
  47. hitTesting({ datasetId, queryText: text, retrieval_model: retrievalConfig }) as Promise<HitTestingResponse>,
  48. )
  49. if (!e) {
  50. setHitResult(res)
  51. onUpdateList?.()
  52. }
  53. setLoading(false)
  54. }
  55. const retrievalMethod = isEconomy ? RETRIEVE_METHOD.invertedIndex : retrievalConfig.search_method
  56. const Icon = getIcon(retrievalMethod)
  57. return (
  58. <>
  59. <div className={s.wrapper}>
  60. <div className='pt-2 rounded-tl-xl rounded-tr-xl bg-[#EEF4FF]'>
  61. <div className="px-4 pb-2 flex justify-between h-8 items-center">
  62. <span className="text-gray-800 font-semibold text-sm">
  63. {t('datasetHitTesting.input.title')}
  64. </span>
  65. <Tooltip
  66. selector={'change-retrieval-method'}
  67. htmlContent={t('dataset.retrieval.changeRetrievalMethod')}
  68. >
  69. <div
  70. onClick={onClickRetrievalMethod}
  71. className='flex px-2 h-7 items-center space-x-1 bg-white hover:bg-[#ECE9FE] rounded-md shadow-sm cursor-pointer text-[#6927DA]'
  72. >
  73. <Icon className='w-3.5 h-3.5'></Icon>
  74. <div className='text-xs font-medium'>{t(`dataset.retrieval.${retrievalMethod}.title`)}</div>
  75. </div>
  76. </Tooltip>
  77. </div>
  78. <div className='h-2 rounded-tl-xl rounded-tr-xl bg-white'></div>
  79. </div>
  80. <div className='px-4 pb-11'>
  81. <textarea
  82. value={text}
  83. onChange={handleTextChange}
  84. placeholder={t('datasetHitTesting.input.placeholder') as string}
  85. className={s.textarea}
  86. />
  87. <div className="absolute inset-x-0 bottom-0 flex items-center justify-between mx-4 mt-2 mb-2">
  88. {text?.length > 200
  89. ? (
  90. <Tooltip
  91. content={t('datasetHitTesting.input.countWarning') as string}
  92. selector="hit-testing-warning"
  93. >
  94. <div>
  95. <Tag color="red" className="!text-red-600">
  96. {text?.length}
  97. <span className="text-red-300 mx-0.5">/</span>
  98. 200
  99. </Tag>
  100. </div>
  101. </Tooltip>
  102. )
  103. : (
  104. <Tag
  105. color="gray"
  106. className={cn('!text-gray-500', text?.length ? '' : 'opacity-50')}
  107. >
  108. {text?.length}
  109. <span className="text-gray-300 mx-0.5">/</span>
  110. 200
  111. </Tag>
  112. )}
  113. <Tooltip
  114. selector="hit-testing-submit"
  115. disabled={indexingTechnique === 'high_quality'}
  116. content={t('datasetHitTesting.input.indexWarning') as string}
  117. >
  118. <div>
  119. <Button
  120. onClick={onSubmit}
  121. type="primary"
  122. loading={loading}
  123. disabled={indexingTechnique !== 'high_quality' ? true : (!text?.length || text?.length > 200)}
  124. >
  125. {t('datasetHitTesting.input.testing')}
  126. </Button>
  127. </div>
  128. </Tooltip>
  129. </div>
  130. </div>
  131. </div>
  132. </>
  133. )
  134. }
  135. export default TextAreaWithButton