index.tsx 9.1 KB

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