index.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import useSWR from 'swr'
  6. import { omit } from 'lodash-es'
  7. import { useBoolean } from 'ahooks'
  8. import { useContext } from 'use-context-selector'
  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 ModifyRetrievalModal from './modify-retrieval-modal'
  15. import cn from '@/utils/classnames'
  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 onClickCard = (detail: HitTestingType) => {
  57. setCurrParagraph({ paraInfo: detail, showModal: true })
  58. }
  59. const { dataset: currentDataset } = useContext(DatasetDetailContext)
  60. const [retrievalConfig, setRetrievalConfig] = useState(currentDataset?.retrieval_model_dict as RetrievalConfig)
  61. const [isShowModifyRetrievalModal, setIsShowModifyRetrievalModal] = useState(false)
  62. const [isShowRightPanel, { setTrue: showRightPanel, setFalse: hideRightPanel, set: setShowRightPanel }] = useBoolean(!isMobile)
  63. useEffect(() => {
  64. setShowRightPanel(!isMobile)
  65. }, [isMobile, setShowRightPanel])
  66. return (
  67. <div className={s.container}>
  68. <div className={s.leftDiv}>
  69. <div className={s.titleWrapper}>
  70. <h1 className={s.title}>{t('datasetHitTesting.title')}</h1>
  71. <p className={s.desc}>{t('datasetHitTesting.desc')}</p>
  72. </div>
  73. <Textarea
  74. datasetId={datasetId}
  75. setHitResult={setHitResult}
  76. onSubmit={showRightPanel}
  77. onUpdateList={recordsMutate}
  78. loading={submitLoading}
  79. setLoading={setSubmitLoading}
  80. setText={setText}
  81. text={text}
  82. onClickRetrievalMethod={() => setIsShowModifyRetrievalModal(true)}
  83. retrievalConfig={retrievalConfig}
  84. isEconomy={currentDataset?.indexing_technique === 'economy'}
  85. />
  86. <div className={cn(s.title, 'mt-8 mb-2')}>{t('datasetHitTesting.recents')}</div>
  87. {(!recordsRes && !error)
  88. ? (
  89. <div className='flex-1'><Loading type='app' /></div>
  90. )
  91. : recordsRes?.data?.length
  92. ? (
  93. <>
  94. <div className='grow overflow-y-auto'>
  95. <table className={`w-full border-collapse border-0 mt-3 ${s.table}`}>
  96. <thead className="sticky top-0 h-8 bg-white leading-8 border-b border-gray-200 text-gray-500 font-bold">
  97. <tr>
  98. <td className='w-28'>{t('datasetHitTesting.table.header.source')}</td>
  99. <td>{t('datasetHitTesting.table.header.text')}</td>
  100. <td className='w-48'>{t('datasetHitTesting.table.header.time')}</td>
  101. </tr>
  102. </thead>
  103. <tbody className="text-gray-500">
  104. {recordsRes?.data?.map((record) => {
  105. return <tr
  106. key={record.id}
  107. className='group border-b border-gray-200 h-8 hover:bg-gray-50 cursor-pointer'
  108. onClick={() => setText(record.content)}
  109. >
  110. <td className='w-24'>
  111. <div className='flex items-center'>
  112. <div className={cn(s[`${record.source}_icon`], s.commonIcon, 'mr-1')} />
  113. <span className='capitalize'>{record.source.replace('_', ' ')}</span>
  114. </div>
  115. </td>
  116. <td className='max-w-xs group-hover:text-primary-600'>{record.content}</td>
  117. <td className='w-36'>
  118. {formatTime(record.created_at, t('datasetHitTesting.dateTimeFormat') as string)}
  119. </td>
  120. </tr>
  121. })}
  122. </tbody>
  123. </table>
  124. </div>
  125. {(total && total > limit)
  126. ? <Pagination current={currPage} onChange={setCurrPage} total={total} limit={limit} />
  127. : null}
  128. </>
  129. )
  130. : (
  131. <RecordsEmpty />
  132. )}
  133. </div>
  134. <FloatRightContainer panelClassname='!justify-start !overflow-y-auto' showClose isMobile={isMobile} isOpen={isShowRightPanel} onClose={hideRightPanel} footer={null}>
  135. <div className={cn(s.rightDiv, 'p-0 sm:px-8 sm:pt-[42px] sm:pb-[26px]')}>
  136. {submitLoading
  137. ? <div className={s.cardWrapper}>
  138. <SegmentCard
  139. loading={true}
  140. scene='hitTesting'
  141. className='h-[216px]'
  142. />
  143. <SegmentCard
  144. loading={true}
  145. scene='hitTesting'
  146. className='h-[216px]'
  147. />
  148. </div>
  149. : !hitResult?.records.length
  150. ? (
  151. <div className='h-full flex flex-col justify-center items-center'>
  152. <div className={cn(docStyle.commonIcon, docStyle.targetIcon, '!bg-gray-200 !h-14 !w-14')} />
  153. <div className='text-gray-300 text-[13px] mt-3'>
  154. {t('datasetHitTesting.hit.emptyTip')}
  155. </div>
  156. </div>
  157. )
  158. : (
  159. <>
  160. <div className='text-gray-600 font-semibold mb-4'>{t('datasetHitTesting.hit.title')}</div>
  161. <div className='overflow-auto flex-1'>
  162. <div className={s.cardWrapper}>
  163. {hitResult?.records.map((record, idx) => {
  164. return <SegmentCard
  165. key={idx}
  166. loading={false}
  167. detail={record.segment as any}
  168. score={record.score}
  169. scene='hitTesting'
  170. className='h-[216px] mb-4'
  171. onClick={() => onClickCard(record as any)}
  172. />
  173. })}
  174. </div>
  175. </div>
  176. </>
  177. )
  178. }
  179. </div>
  180. </FloatRightContainer>
  181. <Modal
  182. className='w-[520px] p-0'
  183. closable
  184. onClose={() => setCurrParagraph({ showModal: false })}
  185. isShow={currParagraph.showModal}
  186. >
  187. {currParagraph.showModal && <HitDetail
  188. segInfo={currParagraph.paraInfo?.segment}
  189. />}
  190. </Modal>
  191. <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'>
  192. <ModifyRetrievalModal
  193. indexMethod={currentDataset?.indexing_technique || ''}
  194. value={retrievalConfig}
  195. isShow={isShowModifyRetrievalModal}
  196. onHide={() => setIsShowModifyRetrievalModal(false)}
  197. onSave={(value) => {
  198. setRetrievalConfig(value)
  199. setIsShowModifyRetrievalModal(false)
  200. }}
  201. />
  202. </Drawer>
  203. </div>
  204. )
  205. }
  206. export default HitTesting