fix: update fallback logic for newly created pages (#6345)

This commit is contained in:
Aaryan Khandelwal 2025-01-08 13:30:06 +05:30 committed by GitHub
parent 71ebe5ca36
commit ac14d57f8b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,6 @@
import { useCallback, useEffect } from "react"; import { useCallback, useEffect } from "react";
// plane editor // plane editor
import { EditorRefApi } from "@plane/editor"; import { EditorRefApi, getBinaryDataFromDocumentEditorHTMLString } from "@plane/editor";
// plane types // plane types
import { TDocumentPayload } from "@plane/types"; import { TDocumentPayload } from "@plane/types";
// hooks // hooks
@ -8,7 +8,7 @@ import useAutoSave from "@/hooks/use-auto-save";
type TArgs = { type TArgs = {
editorRef: React.RefObject<EditorRefApi>; editorRef: React.RefObject<EditorRefApi>;
fetchPageDescription: () => Promise<any>; fetchPageDescription: () => Promise<ArrayBuffer>;
hasConnectionFailed: boolean; hasConnectionFailed: boolean;
updatePageDescription: (data: TDocumentPayload) => Promise<void>; updatePageDescription: (data: TDocumentPayload) => Promise<void>;
}; };
@ -21,28 +21,35 @@ export const usePageFallback = (args: TArgs) => {
const editor = editorRef.current; const editor = editorRef.current;
if (!editor) return; if (!editor) return;
const latestEncodedDescription = await fetchPageDescription(); try {
const latestDecodedDescription = latestEncodedDescription const latestEncodedDescription = await fetchPageDescription();
? new Uint8Array(latestEncodedDescription) let latestDecodedDescription: Uint8Array;
: new Uint8Array(); if (latestEncodedDescription && latestEncodedDescription.byteLength > 0) {
latestDecodedDescription = new Uint8Array(latestEncodedDescription);
} else {
latestDecodedDescription = getBinaryDataFromDocumentEditorHTMLString("<p></p>");
}
editor.setProviderDocument(latestDecodedDescription); editor.setProviderDocument(latestDecodedDescription);
const { binary, html, json } = editor.getDocument(); const { binary, html, json } = editor.getDocument();
if (!binary || !json) return; if (!binary || !json) return;
const encodedBinary = Buffer.from(binary).toString("base64"); const encodedBinary = Buffer.from(binary).toString("base64");
await updatePageDescription({ await updatePageDescription({
description_binary: encodedBinary, description_binary: encodedBinary,
description_html: html, description_html: html,
description: json, description: json,
}); });
}, [hasConnectionFailed]); } catch (error) {
console.error("Error in updating description using fallback logic:", error);
}
}, [editorRef, fetchPageDescription, hasConnectionFailed, updatePageDescription]);
useEffect(() => { useEffect(() => {
if (hasConnectionFailed) { if (hasConnectionFailed) {
handleUpdateDescription(); handleUpdateDescription();
} }
}, [hasConnectionFailed]); }, [handleUpdateDescription, hasConnectionFailed]);
useAutoSave(handleUpdateDescription); useAutoSave(handleUpdateDescription);
}; };