From 2fded2ad5500dad2c63b9dd21c24272dddcb77aa Mon Sep 17 00:00:00 2001 From: Philipinho <16838612+Philipinho@users.noreply.github.com> Date: Sun, 13 Jul 2025 23:16:09 -0700 Subject: [PATCH] fix: seamlessly update editor collab token on expiration Handle token refresh without disrupting editor state by disconnecting, updating token, and reconnecting the HocuspocusProvider. This prevents scroll position resets and maintains user context during authentication token renewal. --- .../src/features/editor/page-editor.tsx | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/apps/client/src/features/editor/page-editor.tsx b/apps/client/src/features/editor/page-editor.tsx index c8ab04a4..9de17de2 100644 --- a/apps/client/src/features/editor/page-editor.tsx +++ b/apps/client/src/features/editor/page-editor.tsx @@ -126,7 +126,15 @@ export default function PageEditor({ const now = Date.now().valueOf() / 1000; const isTokenExpired = now >= payload.exp; if (isTokenExpired) { - refetchCollabToken(); + refetchCollabToken().then((result) => { + if (result.data?.token) { + remote.disconnect(); + setTimeout(() => { + remote.configuration.token = result.data.token; + remote.connect(); + }, 100); + } + }); } }, onStatus: (status) => { @@ -152,6 +160,21 @@ export default function PageEditor({ }; }, [pageId]); + /* + useEffect(() => { + // Handle token updates by reconnecting with new token + if (providersRef.current?.remote && collabQuery?.token) { + const currentToken = providersRef.current.remote.configuration.token; + if (currentToken !== collabQuery.token) { + // Token has changed, need to reconnect with new token + providersRef.current.remote.disconnect(); + providersRef.current.remote.configuration.token = collabQuery.token; + providersRef.current.remote.connect(); + } + } + }, [collabQuery?.token]); + */ + // Only connect/disconnect on tab/idle, not destroy useEffect(() => { if (!providersReady || !providersRef.current) return;