index.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use client'
  2. import React, { useEffect, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { XMarkIcon } from '@heroicons/react/20/solid'
  5. import s from './index.module.css'
  6. import cn from '@/utils/classnames'
  7. import type { NotionPage } from '@/models/common'
  8. import NotionIcon from '@/app/components/base/notion-icon'
  9. import { fetchNotionPagePreview } from '@/service/datasets'
  10. type IProps = {
  11. currentPage?: NotionPage
  12. hidePreview: () => void
  13. }
  14. const NotionPagePreview = ({
  15. currentPage,
  16. hidePreview,
  17. }: IProps) => {
  18. const { t } = useTranslation()
  19. const [previewContent, setPreviewContent] = useState('')
  20. const [loading, setLoading] = useState(true)
  21. const getPreviewContent = async () => {
  22. if (!currentPage)
  23. return
  24. try {
  25. const res = await fetchNotionPagePreview({
  26. workspaceID: currentPage.workspace_id,
  27. pageID: currentPage.page_id,
  28. pageType: currentPage.type,
  29. })
  30. setPreviewContent(res.content)
  31. setLoading(false)
  32. }
  33. catch { }
  34. }
  35. useEffect(() => {
  36. if (currentPage) {
  37. setLoading(true)
  38. getPreviewContent()
  39. }
  40. }, [currentPage])
  41. return (
  42. <div className={cn(s.filePreview)}>
  43. <div className={cn(s.previewHeader)}>
  44. <div className={cn(s.title)}>
  45. <span>{t('datasetCreation.stepOne.pagePreview')}</span>
  46. <div className='flex items-center justify-center w-6 h-6 cursor-pointer' onClick={hidePreview}>
  47. <XMarkIcon className='h-4 w-4'></XMarkIcon>
  48. </div>
  49. </div>
  50. <div className={cn(s.fileName)}>
  51. <NotionIcon
  52. className='shrink-0 mr-1'
  53. type='page'
  54. src={currentPage?.page_icon}
  55. />
  56. {currentPage?.page_name}
  57. </div>
  58. </div>
  59. <div className={cn(s.previewContent)}>
  60. {loading && <div className={cn(s.loading)} />}
  61. {!loading && (
  62. <div className={cn(s.fileContent)}>{previewContent}</div>
  63. )}
  64. </div>
  65. </div>
  66. )
  67. }
  68. export default NotionPagePreview