pdf-preview-detached-root.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { createRoot } from 'react-dom/client'
  2. import PdfPreview from './pdf-preview'
  3. import useWaitForI18n from '../../../shared/hooks/use-wait-for-i18n'
  4. import { ReactContextRoot } from '@/features/ide-react/context/react-context-root'
  5. import { useIsNewEditorEnabled } from '@/features/ide-redesign/utils/new-editor-utils'
  6. import { useDetachCompileContext as useCompileContext } from '@/shared/context/detach-compile-context'
  7. import { useEffect } from 'react'
  8. function PdfPreviewDetachedRoot() {
  9. const { isReady } = useWaitForI18n()
  10. if (!isReady) {
  11. return null
  12. }
  13. return (
  14. <ReactContextRoot>
  15. <PdfPreviewDetachedRootContent />
  16. </ReactContextRoot>
  17. )
  18. }
  19. function PdfPreviewDetachedRootContent() {
  20. const { activeOverallTheme } = useCompileContext()
  21. useEffect(() => {
  22. // NOTE: We cannot use useThemedPage here because we need to read the
  23. // activeOverallTheme value from the compile context
  24. document.body.dataset.theme =
  25. activeOverallTheme === 'dark' ? 'default' : 'light'
  26. }, [activeOverallTheme])
  27. return (
  28. <EditorRedesignWrapper>
  29. <PdfPreview />
  30. </EditorRedesignWrapper>
  31. )
  32. }
  33. function EditorRedesignWrapper({ children }: { children: React.ReactNode }) {
  34. const newEditorEnabled = useIsNewEditorEnabled()
  35. if (!newEditorEnabled) {
  36. return <>{children}</>
  37. }
  38. // TODO ide-redesign-cleanup: this wrapper should not be required
  39. return <div className="ide-redesign-main">{children}</div>
  40. }
  41. export default PdfPreviewDetachedRoot // for testing
  42. const element = document.getElementById('pdf-preview-detached-root')
  43. if (element) {
  44. const root = createRoot(element)
  45. root.render(<PdfPreviewDetachedRoot />)
  46. }