Skip to content

Commit

Permalink
chore: removing the syncing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Palanikannan1437 committed Dec 17, 2024
1 parent a7ddf3d commit 96c2f6d
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React from "react";
// components
import { DocumentContentLoader, PageRenderer } from "@/components/editors";
// constants
Expand Down Expand Up @@ -44,59 +44,34 @@ const CollaborativeDocumentEditor = (props: ICollaborativeDocumentEditor) => {
}

// use document editor
const { editor, hasServerConnectionFailed, hasServerSynced, localProvider, hasIndexedDbSynced } =
useCollaborativeEditor({
disabledExtensions,
editable,
editorClassName,
embedHandler,
extensions,
fileHandler,
forwardedRef,
handleEditorReady,
id,
mentionHandler,
onTransaction,
placeholder,
realtimeConfig,
serverHandler,
tabIndex,
user,
});
const { editor, hasServerConnectionFailed, hasServerSynced } = useCollaborativeEditor({
disabledExtensions,
editable,
editorClassName,
embedHandler,
extensions,
fileHandler,
forwardedRef,
handleEditorReady,
id,
mentionHandler,
onTransaction,
placeholder,
realtimeConfig,
serverHandler,
tabIndex,
user,
});

const editorContainerClassNames = getEditorClassNames({
noBorder: true,
borderOnFocus: false,
containerClassName,
});

const [hasIndexedDbEntry, setHasIndexedDbEntry] = useState(null);

useEffect(() => {
async function documentIndexedDbEntry(dbName: string) {
try {
const databases = await indexedDB.databases();
const hasEntry = databases.some((db) => db.name === dbName);
setHasIndexedDbEntry(hasEntry);
} catch (error) {
console.error("Error checking database existence:", error);
return false;
}
}
documentIndexedDbEntry(id);
}, [id, localProvider]);

if (!editor) return null;

if (
hasServerConnectionFailed ||
(!hasIndexedDbEntry && !hasServerSynced) ||
!hasIndexedDbSynced ||
!hasIndexedDbEntry
) {
console.log("syncing indexedDB");
return <DocumentContentLoader />;
}
if (!hasServerSynced && !hasServerConnectionFailed) return <DocumentContentLoader />;

return (
<PageRenderer
Expand Down
12 changes: 1 addition & 11 deletions packages/editor/src/core/hooks/use-collaborative-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const useCollaborativeEditor = (props: TCollaborativeEditorProps) => {
// states
const [hasServerConnectionFailed, setHasServerConnectionFailed] = useState(false);
const [hasServerSynced, setHasServerSynced] = useState(false);
const [hasIndexedDbSynced, setHasIndexedDbSynced] = useState(false);
// initialize Hocuspocus provider
const provider = useMemo(
() =>
Expand All @@ -55,10 +54,7 @@ export const useCollaborativeEditor = (props: TCollaborativeEditorProps) => {
setHasServerConnectionFailed(true);
}
},
onSynced: () => {
serverHandler?.onServerSync?.();
setHasServerSynced(true);
},
onSynced: () => setHasServerSynced(true),
}),
[id, realtimeConfig, serverHandler, user]
);
Expand All @@ -68,10 +64,6 @@ export const useCollaborativeEditor = (props: TCollaborativeEditorProps) => {
[id, provider]
);

localProvider?.on("synced", () => {
setHasIndexedDbSynced(true);
});

// destroy and disconnect all providers connection on unmount
useEffect(
() => () => {
Expand Down Expand Up @@ -119,7 +111,5 @@ export const useCollaborativeEditor = (props: TCollaborativeEditorProps) => {
editor,
hasServerConnectionFailed,
hasServerSynced,
hasIndexedDbSynced,
localProvider,
};
};
1 change: 0 additions & 1 deletion packages/editor/src/core/types/collaboration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
export type TServerHandler = {
onConnect?: () => void;
onServerError?: () => void;
onServerSync?: () => void;
};

type TCollaborativeEditorHookProps = {
Expand Down
18 changes: 0 additions & 18 deletions web/core/components/icons/syncing-component.tsx

This file was deleted.

10 changes: 2 additions & 8 deletions web/core/components/pages/editor/editor-body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ type Props = {
handleEditorReady: Dispatch<SetStateAction<boolean>>;
page: IPage;
sidePeekVisible: boolean;
setSyncing: (value: boolean) => void;
};

export const PageEditorBody: React.FC<Props> = observer((props) => {
const { editorRef, handleConnectionStatus, handleEditorReady, page, sidePeekVisible, setSyncing } = props;
const { editorRef, handleConnectionStatus, handleEditorReady, page, sidePeekVisible } = props;
// router
const { workspaceSlug, projectId } = useParams();
// store hooks
Expand Down Expand Up @@ -107,17 +106,12 @@ export const PageEditorBody: React.FC<Props> = observer((props) => {
handleConnectionStatus(true);
}, [handleConnectionStatus]);

const handleServerSynced = useCallback(() => {
setSyncing(false);
}, [setSyncing]);

const serverHandler: TServerHandler = useMemo(
() => ({
onConnect: handleServerConnect,
onServerError: handleServerError,
onServerSync: handleServerSynced,
}),
[handleServerConnect, handleServerError, handleServerSynced]
[handleServerConnect, handleServerError]
);

const realtimeConfig: TRealtimeConfig | undefined = useMemo(() => {
Expand Down
4 changes: 1 addition & 3 deletions web/core/components/pages/editor/header/extra-options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ type Props = {
editorRef: React.RefObject<EditorRefApi>;
handleDuplicatePage: () => void;
page: IPage;
syncState: boolean | null;
};

export const PageExtraOptions: React.FC<Props> = observer((props) => {
const { editorRef, syncState, handleDuplicatePage, page } = props;
const { editorRef, handleDuplicatePage, page } = props;
// derived values
const {
archived_at,
Expand Down Expand Up @@ -61,7 +60,6 @@ export const PageExtraOptions: React.FC<Props> = observer((props) => {
return (
<div className="flex items-center justify-end gap-3">
{is_locked && <LockedComponent />}
{syncState && <SyncingComponent />}
{archived_at && (
<div className="flex-shrink-0 flex h-7 items-center gap-2 rounded-full bg-blue-500/20 px-3 py-0.5 text-xs font-medium text-blue-500">
<ArchiveIcon className="flex-shrink-0 size-3" />
Expand Down
10 changes: 2 additions & 8 deletions web/core/components/pages/editor/header/mobile-root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ type Props = {
page: IPage;
setSidePeekVisible: (sidePeekState: boolean) => void;
sidePeekVisible: boolean;
syncState: boolean;
};

export const PageEditorMobileHeaderRoot: React.FC<Props> = observer((props) => {
const { editorReady, editorRef, handleDuplicatePage, page, setSidePeekVisible, sidePeekVisible, syncState } = props;
const { editorReady, editorRef, handleDuplicatePage, page, setSidePeekVisible, sidePeekVisible } = props;
// derived values
const { isContentEditable } = page;
// page filters
Expand All @@ -38,12 +37,7 @@ export const PageEditorMobileHeaderRoot: React.FC<Props> = observer((props) => {
setSidePeekVisible={setSidePeekVisible}
/>
</div>
<PageExtraOptions
editorRef={editorRef}
handleDuplicatePage={handleDuplicatePage}
page={page}
syncState={syncState}
/>
<PageExtraOptions editorRef={editorRef} handleDuplicatePage={handleDuplicatePage} page={page} />
</Header>
<Header variant={EHeaderVariant.TERNARY}>
{editorReady && isContentEditable && editorRef.current && <PageToolbar editorRef={editorRef?.current} />}
Expand Down
11 changes: 2 additions & 9 deletions web/core/components/pages/editor/header/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ type Props = {
page: IPage;
setSidePeekVisible: (sidePeekState: boolean) => void;
sidePeekVisible: boolean;
syncState: boolean;
};

export const PageEditorHeaderRoot: React.FC<Props> = observer((props) => {
const { editorReady, editorRef, setSidePeekVisible, sidePeekVisible, handleDuplicatePage, page, syncState } = props;
const { editorReady, editorRef, setSidePeekVisible, sidePeekVisible, handleDuplicatePage, page } = props;
// derived values
const { isContentEditable } = page;
// page filters
Expand Down Expand Up @@ -50,12 +49,7 @@ export const PageEditorHeaderRoot: React.FC<Props> = observer((props) => {
)}
{editorReady && isContentEditable && editorRef.current && <PageToolbar editorRef={editorRef?.current} />}
</Header.LeftItem>
<PageExtraOptions
editorRef={editorRef}
handleDuplicatePage={handleDuplicatePage}
page={page}
syncState={syncState}
/>
<PageExtraOptions editorRef={editorRef} handleDuplicatePage={handleDuplicatePage} page={page} />
</Header>
<div className="md:hidden">
<PageEditorMobileHeaderRoot
Expand All @@ -65,7 +59,6 @@ export const PageEditorHeaderRoot: React.FC<Props> = observer((props) => {
page={page}
sidePeekVisible={sidePeekVisible}
setSidePeekVisible={setSidePeekVisible}
syncState={syncState}
/>
</div>
</>
Expand Down
3 changes: 0 additions & 3 deletions web/core/components/pages/editor/page-root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const PageRoot = observer((props: TPageRootProps) => {
const [editorReady, setEditorReady] = useState(false);
const [hasConnectionFailed, setHasConnectionFailed] = useState(false);
const [sidePeekVisible, setSidePeekVisible] = useState(window.innerWidth >= 768);
const [syncState, setSyncing] = useState<boolean>(true);
const [isVersionsOverlayOpen, setIsVersionsOverlayOpen] = useState(false);
// refs
const editorRef = useRef<EditorRefApi>(null);
Expand Down Expand Up @@ -136,7 +135,6 @@ export const PageRoot = observer((props: TPageRootProps) => {
page={page}
setSidePeekVisible={(state) => setSidePeekVisible(state)}
sidePeekVisible={sidePeekVisible}
syncState={syncState}
/>
<PageEditorBody
editorReady={editorReady}
Expand All @@ -145,7 +143,6 @@ export const PageRoot = observer((props: TPageRootProps) => {
handleEditorReady={setEditorReady}
page={page}
sidePeekVisible={sidePeekVisible}
setSyncing={setSyncing}
/>
</>
);
Expand Down

0 comments on commit 96c2f6d

Please sign in to comment.