11import { useCallback , useEffect } from "react" ;
22// plane editor
3- import { EditorRefApi } from "@plane/editor" ;
3+ import { EditorRefApi , getBinaryDataFromDocumentEditorHTMLString } from "@plane/editor" ;
44// plane types
55import { TDocumentPayload } from "@plane/types" ;
66// hooks
77import useAutoSave from "@/hooks/use-auto-save" ;
88
99type TArgs = {
1010 editorRef : React . RefObject < EditorRefApi > ;
11- fetchPageDescription : ( ) => Promise < any > ;
11+ fetchPageDescription : ( ) => Promise < ArrayBuffer > ;
1212 hasConnectionFailed : boolean ;
1313 updatePageDescription : ( data : TDocumentPayload ) => Promise < void > ;
1414} ;
@@ -21,28 +21,35 @@ export const usePageFallback = (args: TArgs) => {
2121 const editor = editorRef . current ;
2222 if ( ! editor ) return ;
2323
24- const latestEncodedDescription = await fetchPageDescription ( ) ;
25- const latestDecodedDescription = latestEncodedDescription
26- ? new Uint8Array ( latestEncodedDescription )
27- : new Uint8Array ( ) ;
28-
29- editor . setProviderDocument ( latestDecodedDescription ) ;
30- const { binary, html, json } = editor . getDocument ( ) ;
31- if ( ! binary || ! json ) return ;
32- const encodedBinary = Buffer . from ( binary ) . toString ( "base64" ) ;
33-
34- await updatePageDescription ( {
35- description_binary : encodedBinary ,
36- description_html : html ,
37- description : json ,
38- } ) ;
39- } , [ hasConnectionFailed ] ) ;
24+ try {
25+ const latestEncodedDescription = await fetchPageDescription ( ) ;
26+ let latestDecodedDescription : Uint8Array ;
27+ if ( latestEncodedDescription && latestEncodedDescription . byteLength > 0 ) {
28+ latestDecodedDescription = new Uint8Array ( latestEncodedDescription ) ;
29+ } else {
30+ latestDecodedDescription = getBinaryDataFromDocumentEditorHTMLString ( "<p></p>" ) ;
31+ }
32+
33+ editor . setProviderDocument ( latestDecodedDescription ) ;
34+ const { binary, html, json } = editor . getDocument ( ) ;
35+ if ( ! binary || ! json ) return ;
36+ const encodedBinary = Buffer . from ( binary ) . toString ( "base64" ) ;
37+
38+ await updatePageDescription ( {
39+ description_binary : encodedBinary ,
40+ description_html : html ,
41+ description : json ,
42+ } ) ;
43+ } catch ( error ) {
44+ console . error ( "Error in updating description using fallback logic:" , error ) ;
45+ }
46+ } , [ editorRef , fetchPageDescription , hasConnectionFailed , updatePageDescription ] ) ;
4047
4148 useEffect ( ( ) => {
4249 if ( hasConnectionFailed ) {
4350 handleUpdateDescription ( ) ;
4451 }
45- } , [ hasConnectionFailed ] ) ;
52+ } , [ handleUpdateDescription , hasConnectionFailed ] ) ;
4653
4754 useAutoSave ( handleUpdateDescription ) ;
4855} ;
0 commit comments