From 5b3f56b9120d61e9066b58f23e67207a88a1b5be Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal Date: Tue, 7 Jan 2025 19:34:06 +0530 Subject: [PATCH] fix: update fallback logic for newly created pages --- web/core/hooks/use-page-fallback.ts | 45 +++++++++++++++++------------ 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/web/core/hooks/use-page-fallback.ts b/web/core/hooks/use-page-fallback.ts index 9f5ef348293..52b54fe5018 100644 --- a/web/core/hooks/use-page-fallback.ts +++ b/web/core/hooks/use-page-fallback.ts @@ -1,6 +1,6 @@ import { useCallback, useEffect } from "react"; // plane editor -import { EditorRefApi } from "@plane/editor"; +import { EditorRefApi, getBinaryDataFromDocumentEditorHTMLString } from "@plane/editor"; // plane types import { TDocumentPayload } from "@plane/types"; // hooks @@ -8,7 +8,7 @@ import useAutoSave from "@/hooks/use-auto-save"; type TArgs = { editorRef: React.RefObject; - fetchPageDescription: () => Promise; + fetchPageDescription: () => Promise; hasConnectionFailed: boolean; updatePageDescription: (data: TDocumentPayload) => Promise; }; @@ -21,28 +21,35 @@ export const usePageFallback = (args: TArgs) => { const editor = editorRef.current; if (!editor) return; - const latestEncodedDescription = await fetchPageDescription(); - const latestDecodedDescription = latestEncodedDescription - ? new Uint8Array(latestEncodedDescription) - : new Uint8Array(); - - editor.setProviderDocument(latestDecodedDescription); - const { binary, html, json } = editor.getDocument(); - if (!binary || !json) return; - const encodedBinary = Buffer.from(binary).toString("base64"); - - await updatePageDescription({ - description_binary: encodedBinary, - description_html: html, - description: json, - }); - }, [hasConnectionFailed]); + try { + const latestEncodedDescription = await fetchPageDescription(); + let latestDecodedDescription: Uint8Array; + if (latestEncodedDescription && latestEncodedDescription.byteLength > 0) { + latestDecodedDescription = new Uint8Array(latestEncodedDescription); + } else { + latestDecodedDescription = getBinaryDataFromDocumentEditorHTMLString("

"); + } + + editor.setProviderDocument(latestDecodedDescription); + const { binary, html, json } = editor.getDocument(); + if (!binary || !json) return; + const encodedBinary = Buffer.from(binary).toString("base64"); + + await updatePageDescription({ + description_binary: encodedBinary, + description_html: html, + description: json, + }); + } catch (error) { + console.error("Error in updating description using fallback logic:", error); + } + }, [editorRef, fetchPageDescription, hasConnectionFailed, updatePageDescription]); useEffect(() => { if (hasConnectionFailed) { handleUpdateDescription(); } - }, [hasConnectionFailed]); + }, [handleUpdateDescription, hasConnectionFailed]); useAutoSave(handleUpdateDescription); };