index.tsx 7.1 KB

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