index.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useState } from 'react'
  4. import useSWR from 'swr'
  5. import { ArrowLeftIcon } from '@heroicons/react/24/solid'
  6. import { createContext } from 'use-context-selector'
  7. import { useTranslation } from 'react-i18next'
  8. import { useRouter } from 'next/navigation'
  9. import { omit } from 'lodash-es'
  10. import cn from 'classnames'
  11. import Divider from '@/app/components/base/divider'
  12. import Loading from '@/app/components/base/loading'
  13. import { fetchDocumentDetail, MetadataType } from '@/service/datasets'
  14. import { OperationAction, StatusItem } from '../list'
  15. import Completed from './completed'
  16. import Embedding from './embedding'
  17. import Metadata from './metadata'
  18. import s from '../style.module.css'
  19. import style from './style.module.css'
  20. export const BackCircleBtn: FC<{ onClick: () => void }> = ({ onClick }) => {
  21. return (
  22. <div onClick={onClick} className={'rounded-full w-8 h-8 flex justify-center items-center border-gray-100 cursor-pointer border hover:border-gray-300 shadow-[0px_12px_16px_-4px_rgba(16,24,40,0.08),0px_4px_6px_-2px_rgba(16,24,40,0.03)]'}>
  23. <ArrowLeftIcon className='text-primary-600 fill-current stroke-current h-4 w-4' />
  24. </div>
  25. )
  26. }
  27. export const DocumentContext = createContext<{ datasetId?: string; documentId?: string }>({})
  28. type DocumentTitleProps = {
  29. extension?: string;
  30. name?: string;
  31. iconCls?: string;
  32. textCls?: string;
  33. wrapperCls?: string;
  34. }
  35. export const DocumentTitle: FC<DocumentTitleProps> = ({ extension, name, iconCls, textCls, wrapperCls }) => {
  36. const localExtension = extension?.toLowerCase() || name?.split('.')?.pop()?.toLowerCase()
  37. return <div className={cn('flex items-center justify-start flex-1', wrapperCls)}>
  38. <div className={cn(s[`${localExtension || 'txt'}Icon`], style.titleIcon, iconCls)}></div>
  39. <span className={cn('font-semibold text-lg text-gray-900 ml-1', textCls)}> {name || '--'}</span>
  40. </div>
  41. }
  42. type Props = {
  43. datasetId: string
  44. documentId: string
  45. }
  46. const DocumentDetail: FC<Props> = ({ datasetId, documentId }) => {
  47. const { t } = useTranslation()
  48. const router = useRouter()
  49. const [showMetadata, setShowMetadata] = useState(true)
  50. const { data: documentDetail, error, mutate: detailMutate } = useSWR({
  51. action: 'fetchDocumentDetail',
  52. datasetId,
  53. documentId,
  54. params: { metadata: 'without' as MetadataType }
  55. }, apiParams => fetchDocumentDetail(omit(apiParams, 'action')))
  56. const { data: documentMetadata, error: metadataErr, mutate: metadataMutate } = useSWR({
  57. action: 'fetchDocumentDetail',
  58. datasetId,
  59. documentId,
  60. params: { metadata: 'only' as MetadataType }
  61. }, apiParams => fetchDocumentDetail(omit(apiParams, 'action')))
  62. const backToPrev = () => {
  63. router.push(`/datasets/${datasetId}/documents`)
  64. }
  65. const isDetailLoading = !documentDetail && !error
  66. const isMetadataLoading = !documentMetadata && !metadataErr
  67. const embedding = ['queuing', 'indexing', 'paused'].includes((documentDetail?.display_status || '').toLowerCase())
  68. return (
  69. <DocumentContext.Provider value={{ datasetId, documentId }}>
  70. <div className='flex flex-col h-full'>
  71. <div className='flex h-16 border-b-gray-100 border-b items-center p-4'>
  72. <BackCircleBtn onClick={backToPrev} />
  73. <Divider className='!h-4' type='vertical' />
  74. <DocumentTitle extension={documentDetail?.data_source_info?.upload_file?.extension} name={documentDetail?.name} />
  75. <StatusItem status={documentDetail?.display_status || 'available'} scene='detail' />
  76. <OperationAction
  77. scene='detail'
  78. detail={{
  79. enabled: documentDetail?.enabled || false,
  80. archived: documentDetail?.archived || false,
  81. id: documentId
  82. }}
  83. datasetId={datasetId}
  84. onUpdate={detailMutate}
  85. className='!w-[216px]'
  86. />
  87. <button
  88. className={cn(style.layoutRightIcon, showMetadata ? style.iconShow : style.iconClose)}
  89. onClick={() => setShowMetadata(!showMetadata)}
  90. />
  91. </div>
  92. <div className='flex flex-row flex-1' style={{ height: 'calc(100% - 4rem)' }}>
  93. {isDetailLoading ? <Loading type='app' /> :
  94. <div className={`box-border h-full w-full overflow-y-scroll ${embedding ? 'py-12 px-16' : 'pb-[30px] pt-3 px-6'}`}>
  95. {embedding ? <Embedding detail={documentDetail} /> : <Completed />}
  96. </div>
  97. }
  98. {showMetadata && <Metadata
  99. docDetail={{ ...documentDetail, ...documentMetadata } as any}
  100. loading={isMetadataLoading}
  101. onUpdate={metadataMutate}
  102. />}
  103. </div>
  104. </div>
  105. </DocumentContext.Provider>
  106. )
  107. }
  108. export default DocumentDetail