mirror of
https://github.com/gosticks/plane.git
synced 2025-10-16 12:45:33 +00:00
* refactor: remove barrel exports from some compoennt modules * refactor: remove barrel exports from issue components * refactor: remove barrel exports from page components * chore: update type improts * refactor: remove barrel exports from cycle components * refactor: remove barrel exports from dropdown components * refactor: remove barrel exports from ce components * refactor: remove barrel exports from some more components * refactor: remove barrel exports from profile and sidebar components * chore: update type imports * refactor: remove barrel exports from store hooks * chore: dynamically load sticky editor * fix: lint * chore: revert sticky dynamic import * refactor: remove barrel exports from ce issue components * refactor: remove barrel exports from ce issue components * refactor: remove barrel exports from ce issue components --------- Co-authored-by: sriramveeraghanta <veeraghanta.sriram@gmail.com>
56 lines
1.9 KiB
TypeScript
56 lines
1.9 KiB
TypeScript
import { useCallback, useEffect } from "react";
|
|
// plane editor
|
|
import { type 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<ArrayBuffer>;
|
|
hasConnectionFailed: boolean;
|
|
updatePageDescription: (data: TDocumentPayload) => Promise<void>;
|
|
};
|
|
|
|
export const usePageFallback = (args: TArgs) => {
|
|
const { editorRef, fetchPageDescription, hasConnectionFailed, updatePageDescription } = args;
|
|
|
|
const handleUpdateDescription = useCallback(async () => {
|
|
if (!hasConnectionFailed) return;
|
|
const editor = editorRef.current;
|
|
if (!editor) return;
|
|
|
|
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();
|
|
}
|
|
}, [handleUpdateDescription, hasConnectionFailed]);
|
|
|
|
useAutoSave(handleUpdateDescription);
|
|
};
|