Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions web/core/hooks/use-page-fallback.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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
import useAutoSave from "@/hooks/use-auto-save";

type TArgs = {
editorRef: React.RefObject<EditorRefApi>;
fetchPageDescription: () => Promise<any>;
fetchPageDescription: () => Promise<ArrayBuffer>;
hasConnectionFailed: boolean;
updatePageDescription: (data: TDocumentPayload) => Promise<void>;
};
Expand All @@ -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("<p></p>");
}

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);
};
Loading