textarea.tsx 4.6 KB

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