1- import { Loader } from '@openfun/cunningham-react' ;
2- import { useRouter } from 'next/router' ;
3- import { useEffect , useState } from 'react' ;
1+ import { useEffect } from 'react' ;
42import { css } from 'styled-components' ;
5- import * as Y from 'yjs' ;
63
7- import { Box , Loading , Text , TextErrors } from '@/components' ;
8- import { DocHeader , DocVersionHeader } from '@/docs/doc-header/' ;
9- import {
10- Doc ,
11- base64ToBlocknoteXmlFragment ,
12- useProviderStore ,
13- } from '@/docs/doc-management' ;
4+ import { Box , Loading } from '@/components' ;
5+ import { DocHeader } from '@/docs/doc-header/' ;
6+ import { Doc , useProviderStore } from '@/docs/doc-management' ;
147import { TableContent } from '@/docs/doc-table-content/' ;
15- import { Versions , useDocVersion } from '@/docs/doc-versioning/' ;
168import { useSkeletonStore } from '@/features/skeletons' ;
179import { useResponsiveStore } from '@/stores' ;
1810
19- import { BlockNoteEditor , BlockNoteEditorVersion } from './BlockNoteEditor' ;
11+ import { BlockNoteEditor } from './BlockNoteEditor' ;
2012
21- interface DocEditorProps {
22- doc : Doc ;
23- versionId ?: Versions [ 'version_id' ] ;
13+ interface DocEditorContainerProps {
14+ docHeader : React . ReactNode ;
15+ docEditor : React . ReactNode ;
2416}
2517
26- export const DocEditor = ( { doc, versionId } : DocEditorProps ) => {
18+ export const DocEditorContainer = ( {
19+ docHeader,
20+ docEditor,
21+ } : DocEditorContainerProps ) => {
2722 const { isDesktop } = useResponsiveStore ( ) ;
28- const isVersion = ! ! versionId && typeof versionId === 'string' ;
29- const { provider, isReady } = useProviderStore ( ) ;
30- const { setIsSkeletonVisible } = useSkeletonStore ( ) ;
31- const isProviderReady = isReady && provider ;
32-
33- useEffect ( ( ) => {
34- if ( isProviderReady ) {
35- setIsSkeletonVisible ( false ) ;
36- }
37- } , [ isProviderReady , setIsSkeletonVisible ] ) ;
38-
39- if ( ! isProviderReady ) {
40- return < Loading /> ;
41- }
4223
4324 return (
4425 < >
45- { isDesktop && ! isVersion && (
46- < Box
47- $position = "absolute"
48- $css = { css `
49- top : 72px ;
50- right : 20px ;
51- ` }
52- >
53- < TableContent />
54- </ Box >
55- ) }
5626 < Box
5727 $maxWidth = "868px"
5828 $width = "100%"
@@ -63,7 +33,7 @@ export const DocEditor = ({ doc, versionId }: DocEditorProps) => {
6333 $padding = { { horizontal : isDesktop ? '54px' : 'base' } }
6434 className = "--docs--doc-editor-header"
6535 >
66- { isVersion ? < DocVersionHeader /> : < DocHeader doc = { doc } /> }
36+ { docHeader }
6737 </ Box >
6838
6939 < Box
@@ -74,81 +44,51 @@ export const DocEditor = ({ doc, versionId }: DocEditorProps) => {
7444 className = "--docs--doc-editor-content"
7545 >
7646 < Box $css = "flex:1;" $position = "relative" $width = "100%" >
77- { isVersion ? (
78- < DocVersionEditor docId = { doc . id } versionId = { versionId } />
79- ) : (
80- < BlockNoteEditor doc = { doc } provider = { provider } />
81- ) }
47+ { docEditor }
8248 </ Box >
8349 </ Box >
8450 </ Box >
8551 </ >
8652 ) ;
8753} ;
8854
89- interface DocVersionEditorProps {
90- docId : Doc [ 'id' ] ;
91- versionId : Versions [ 'version_id' ] ;
55+ interface DocEditorProps {
56+ doc : Doc ;
9257}
9358
94- export const DocVersionEditor = ( {
95- docId,
96- versionId,
97- } : DocVersionEditorProps ) => {
98- const {
99- data : version ,
100- isLoading,
101- isError,
102- error,
103- } = useDocVersion ( {
104- docId,
105- versionId,
106- } ) ;
107-
108- const { replace } = useRouter ( ) ;
109- const [ initialContent , setInitialContent ] = useState < Y . XmlFragment > ( ) ;
59+ export const DocEditor = ( { doc } : DocEditorProps ) => {
60+ const { isDesktop } = useResponsiveStore ( ) ;
61+ const { provider, isReady } = useProviderStore ( ) ;
62+ const { setIsSkeletonVisible } = useSkeletonStore ( ) ;
63+ const isProviderReady = isReady && provider ;
11064
11165 useEffect ( ( ) => {
112- if ( ! version ?. content ) {
113- return ;
114- }
115-
116- setInitialContent ( base64ToBlocknoteXmlFragment ( version . content ) ) ;
117- } , [ version ?. content ] ) ;
118-
119- if ( isError && error ) {
120- if ( error . status === 404 ) {
121- void replace ( `/404` ) ;
122- return null ;
66+ if ( isProviderReady ) {
67+ setIsSkeletonVisible ( false ) ;
12368 }
69+ } , [ isProviderReady , setIsSkeletonVisible ] ) ;
12470
125- return (
126- < Box $margin = "large" className = "--docs--doc-version-editor-error" >
127- < TextErrors
128- causes = { error . cause }
129- icon = {
130- error . status === 502 ? (
131- < Text
132- className = "material-icons"
133- $theme = "danger"
134- aria-hidden = { true }
135- >
136- wifi_off
137- </ Text >
138- ) : undefined
139- }
140- />
141- </ Box >
142- ) ;
143- }
144-
145- if ( isLoading || ! version || ! initialContent ) {
146- return (
147- < Box $align = "center" $justify = "center" $height = "100%" >
148- < Loader />
149- </ Box >
150- ) ;
71+ if ( ! isProviderReady ) {
72+ return < Loading /> ;
15173 }
15274
153- return < BlockNoteEditorVersion initialContent = { initialContent } /> ;
75+ return (
76+ < >
77+ { isDesktop && (
78+ < Box
79+ $position = "absolute"
80+ $css = { css `
81+ top : 72px ;
82+ right : 20px ;
83+ ` }
84+ >
85+ < TableContent />
86+ </ Box >
87+ ) }
88+ < DocEditorContainer
89+ docHeader = { < DocHeader doc = { doc } /> }
90+ docEditor = { < BlockNoteEditor doc = { doc } provider = { provider } /> }
91+ />
92+ </ >
93+ ) ;
15494} ;
0 commit comments