Skip to content

Commit 33bd5ef

Browse files
committed
⚡️(frontend) remove flickering when connecting to collab
The blocking edition modal could be flickring, because the connection to the collaborative server can take a bit of time. We set a timeout to ensure the loading state is cleared after a reasonable time.
1 parent 7abe1c9 commit 33bd5ef

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/frontend/apps/impress/src/features/docs/doc-management/hooks/useIsCollaborativeEditable.tsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react';
1+
import { useEffect, useRef, useState } from 'react';
22

33
import { useConfig } from '@/core';
44
import { useIsOffline } from '@/features/service-worker';
@@ -20,7 +20,7 @@ export const useIsCollaborativeEditable = (doc: Doc) => {
2020
const _isEditable = isUserReader || isConnected || !isShared || isOffline;
2121
const [isEditable, setIsEditable] = useState(true);
2222
const [isLoading, setIsLoading] = useState(!_isEditable);
23-
23+
const timeout = useRef<NodeJS.Timeout | null>(null);
2424
const {
2525
data: { can_edit } = { can_edit: _isEditable },
2626
isLoading: isLoadingCanEdit,
@@ -35,10 +35,32 @@ export const useIsCollaborativeEditable = (doc: Doc) => {
3535
return;
3636
}
3737

38-
setIsEditable(can_edit);
39-
setIsLoading(false);
38+
// Connection to the WebSocket can take some time, so we set a timeout to ensure the loading state is cleared after a reasonable time.
39+
timeout.current = setTimeout(() => {
40+
setIsEditable(can_edit);
41+
setIsLoading(false);
42+
}, 1500);
43+
44+
return () => {
45+
if (timeout.current) {
46+
clearTimeout(timeout.current);
47+
}
48+
};
4049
}, [can_edit, isLoadingCanEdit]);
4150

51+
useEffect(() => {
52+
if (!_isEditable) {
53+
return;
54+
}
55+
56+
if (timeout.current) {
57+
clearTimeout(timeout.current);
58+
}
59+
60+
setIsEditable(true);
61+
setIsLoading(false);
62+
}, [_isEditable, isLoading]);
63+
4264
if (!conf?.COLLABORATION_WS_NOT_CONNECTED_READY_ONLY) {
4365
return {
4466
isEditable: true,

0 commit comments

Comments
 (0)