fix: update fallback logic for newly created pages (#6345)
This commit is contained in:
parent
71ebe5ca36
commit
ac14d57f8b
1 changed files with 24 additions and 17 deletions
|
|
@ -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);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue