index.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useMemo, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import useSWR from 'swr'
  6. import { omit } from 'lodash-es'
  7. import cn from 'classnames'
  8. import dayjs from 'dayjs'
  9. import { useContext } from 'use-context-selector'
  10. import SegmentCard from '../documents/detail/completed/SegmentCard'
  11. import docStyle from '../documents/detail/completed/style.module.css'
  12. import Textarea from './textarea'
  13. import s from './style.module.css'
  14. import HitDetail from './hit-detail'
  15. import ModifyRetrievalModal from './modify-retrieval-modal'
  16. import type { HitTestingResponse, HitTesting as HitTestingType } from '@/models/datasets'
  17. import Loading from '@/app/components/base/loading'
  18. import Modal from '@/app/components/base/modal'
  19. import Pagination from '@/app/components/base/pagination'
  20. import { fetchTestingRecords } from '@/service/datasets'
  21. import DatasetDetailContext from '@/context/dataset-detail'
  22. import type { RetrievalConfig } from '@/types/app'
  23. const limit = 10
  24. type Props = {
  25. datasetId: string
  26. }
  27. const RecordsEmpty: FC = () => {
  28. const { t } = useTranslation()
  29. return <div className='bg-gray-50 rounded-2xl p-5'>
  30. <div className={s.clockWrapper}>
  31. <div className={cn(s.clockIcon, 'w-5 h-5')}></div>
  32. </div>
  33. <div className='my-2 text-gray-500 text-sm'>{t('datasetHitTesting.noRecentTip')}</div>
  34. </div>
  35. }
  36. const HitTesting: FC<Props> = ({ datasetId }: Props) => {
  37. const { t } = useTranslation()
  38. const [hitResult, setHitResult] = useState<HitTestingResponse | undefined>() // 初始化记录为空数组
  39. const [submitLoading, setSubmitLoading] = useState(false)
  40. const [currParagraph, setCurrParagraph] = useState<{ paraInfo?: HitTestingType; showModal: boolean }>({ showModal: false })
  41. const [text, setText] = useState('')
  42. const [currPage, setCurrPage] = React.useState<number>(0)
  43. const { data: recordsRes, error, mutate: recordsMutate } = useSWR({
  44. action: 'fetchTestingRecords',
  45. datasetId,
  46. params: { limit, page: currPage + 1 },
  47. }, apiParams => fetchTestingRecords(omit(apiParams, 'action')))
  48. const total = recordsRes?.total || 0
  49. const points = useMemo(() => (hitResult?.records.map(v => [v.tsne_position.x, v.tsne_position.y]) || []), [hitResult?.records])
  50. const onClickCard = (detail: HitTestingType) => {
  51. setCurrParagraph({ paraInfo: detail, showModal: true })
  52. }
  53. const { dataset: currentDataset } = useContext(DatasetDetailContext)
  54. const [retrievalConfig, setRetrievalConfig] = useState(currentDataset?.retrieval_model_dict as RetrievalConfig)
  55. const [isShowModifyRetrievalModal, setIsShowModifyRetrievalModal] = useState(false)
  56. return (
  57. <div className={s.container}>
  58. <div className={s.leftDiv}>
  59. <div className={s.titleWrapper}>
  60. <h1 className={s.title}>{t('datasetHitTesting.title')}</h1>
  61. <p className={s.desc}>{t('datasetHitTesting.desc')}</p>
  62. </div>
  63. <Textarea
  64. datasetId={datasetId}
  65. setHitResult={setHitResult}
  66. onUpdateList={recordsMutate}
  67. loading={submitLoading}
  68. setLoading={setSubmitLoading}
  69. setText={setText}
  70. text={text}
  71. onClickRetrievalMethod={() => setIsShowModifyRetrievalModal(true)}
  72. retrievalConfig={retrievalConfig}
  73. isEconomy={currentDataset?.indexing_technique === 'economy'}
  74. />
  75. <div className={cn(s.title, 'mt-8 mb-2')}>{t('datasetHitTesting.recents')}</div>
  76. {(!recordsRes && !error)
  77. ? (
  78. <div className='flex-1'><Loading type='app' /></div>
  79. )
  80. : recordsRes?.data?.length
  81. ? (
  82. <>
  83. <div className='grow overflow-y-auto'>
  84. <table className={`w-full border-collapse border-0 mt-3 ${s.table}`}>
  85. <thead className="sticky top-0 h-8 bg-white leading-8 border-b border-gray-200 text-gray-500 font-bold">
  86. <tr>
  87. <td className='w-28'>{t('datasetHitTesting.table.header.source')}</td>
  88. <td>{t('datasetHitTesting.table.header.text')}</td>
  89. <td className='w-48'>{t('datasetHitTesting.table.header.time')}</td>
  90. </tr>
  91. </thead>
  92. <tbody className="text-gray-500">
  93. {recordsRes?.data?.map((record) => {
  94. return <tr
  95. key={record.id}
  96. className='group border-b border-gray-200 h-8 hover:bg-gray-50 cursor-pointer'
  97. onClick={() => setText(record.content)}
  98. >
  99. <td className='w-24'>
  100. <div className='flex items-center'>
  101. <div className={cn(s[`${record.source}_icon`], s.commonIcon, 'mr-1')} />
  102. <span className='capitalize'>{record.source.replace('_', ' ')}</span>
  103. </div>
  104. </td>
  105. <td className='max-w-xs group-hover:text-primary-600'>{record.content}</td>
  106. <td className='w-36'>
  107. {dayjs.unix(record.created_at).format(t('datasetHitTesting.dateTimeFormat') as string)}
  108. </td>
  109. </tr>
  110. })}
  111. </tbody>
  112. </table>
  113. </div>
  114. {(total && total > limit)
  115. ? <Pagination current={currPage} onChange={setCurrPage} total={total} limit={limit} />
  116. : null}
  117. </>
  118. )
  119. : (
  120. <RecordsEmpty />
  121. )}
  122. </div>
  123. <div className={s.rightDiv}>
  124. {submitLoading
  125. ? <div className={s.cardWrapper}>
  126. <SegmentCard
  127. loading={true}
  128. scene='hitTesting'
  129. className='h-[216px]'
  130. />
  131. <SegmentCard
  132. loading={true}
  133. scene='hitTesting'
  134. className='h-[216px]'
  135. />
  136. </div>
  137. : !hitResult?.records.length
  138. ? (
  139. <div className='h-full flex flex-col justify-center items-center'>
  140. <div className={cn(docStyle.commonIcon, docStyle.targetIcon, '!bg-gray-200 !h-14 !w-14')} />
  141. <div className='text-gray-300 text-[13px] mt-3'>
  142. {t('datasetHitTesting.hit.emptyTip')}
  143. </div>
  144. </div>
  145. )
  146. : (
  147. <>
  148. <div className='text-gray-600 font-semibold mb-4'>{t('datasetHitTesting.hit.title')}</div>
  149. <div className='overflow-auto flex-1'>
  150. <div className={s.cardWrapper}>
  151. {hitResult?.records.map((record, idx) => {
  152. return <SegmentCard
  153. key={idx}
  154. loading={false}
  155. detail={record.segment as any}
  156. score={record.score}
  157. scene='hitTesting'
  158. className='h-[216px] mb-4'
  159. onClick={() => onClickCard(record as any)}
  160. />
  161. })}
  162. </div>
  163. </div>
  164. </>
  165. )
  166. }
  167. </div>
  168. <Modal
  169. className='!max-w-[960px] !p-0'
  170. closable
  171. onClose={() => setCurrParagraph({ showModal: false })}
  172. isShow={currParagraph.showModal}
  173. >
  174. {currParagraph.showModal && <HitDetail
  175. segInfo={currParagraph.paraInfo?.segment}
  176. vectorInfo={{
  177. curr: [[currParagraph.paraInfo?.tsne_position?.x || 0, currParagraph.paraInfo?.tsne_position.y || 0]],
  178. points,
  179. }}
  180. />}
  181. </Modal>
  182. {isShowModifyRetrievalModal && (
  183. <ModifyRetrievalModal
  184. indexMethod={currentDataset?.indexing_technique || ''}
  185. value={retrievalConfig}
  186. isShow={isShowModifyRetrievalModal}
  187. onHide={() => setIsShowModifyRetrievalModal(false)}
  188. onSave={(value) => {
  189. setRetrievalConfig(value)
  190. setIsShowModifyRetrievalModal(false)
  191. }}
  192. />
  193. )}
  194. </div>
  195. )
  196. }
  197. export default HitTesting