index.tsx 9.4 KB

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