index.tsx 2.1 KB

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