diff --git a/blocks/edit/edit.js b/blocks/edit/edit.js index fdfb0688..6882d9ec 100644 --- a/blocks/edit/edit.js +++ b/blocks/edit/edit.js @@ -36,7 +36,7 @@ async function setUI(el, utils) { } const { daFetch } = await utils; - const { permissions } = await daFetch(details.sourceUrl, { method: 'HEAD' }); + const { permissions = [] } = await daFetch(details.sourceUrl, { method: 'HEAD' }); daTitle.permissions = permissions; daContent.permissions = permissions; diff --git a/blocks/edit/prose/index.js b/blocks/edit/prose/index.js index b725f444..e4fa2770 100644 --- a/blocks/edit/prose/index.js +++ b/blocks/edit/prose/index.js @@ -32,6 +32,7 @@ import linkConverter from './plugins/linkConverter.js'; import sectionPasteHandler from './plugins/sectionPasteHandler.js'; import base64Uploader from './plugins/base64uploader.js'; import { COLLAB_ORIGIN, DA_ORIGIN } from '../../shared/constants.js'; +import { logOut } from '../../shared/utils.js'; import toggleLibrary from '../da-library/da-library.js'; import { getLocClass } from './loc-utils.js'; import { getSchema } from './schema.js'; @@ -219,6 +220,10 @@ export default function initProse({ path, permissions }) { const canWrite = permissions.some((permission) => permission === 'write'); const wsProvider = new WebsocketProvider(server, roomName, ydoc, opts); + wsProvider.on('connection-error', async () => { + await logOut(); + }); + addSyncedListener(wsProvider, canWrite); createAwarenessStatusWidget(wsProvider, window); diff --git a/blocks/shared/utils.js b/blocks/shared/utils.js index 0c26927b..d34299cb 100644 --- a/blocks/shared/utils.js +++ b/blocks/shared/utils.js @@ -21,6 +21,17 @@ export async function initIms() { } } +export async function logOut() { + imsDetails = null; + try { + const { handleSignOut } = await import(`${getNx()}/utils/ims.js`); + handleSignOut(); + } catch { + // do nothing + } + window.location.reload(); +} + export const daFetch = async (url, opts = {}) => { opts.headers = opts.headers || {}; let accessToken; @@ -50,6 +61,8 @@ export const daFetch = async (url, opts = {}) => { const { loadIms, handleSignIn } = await import(`${getNx()}/utils/ims.js`); await loadIms(); handleSignIn(); + // wait 1 second to let ims do its things + await new Promise((resolve) => { setTimeout(resolve, 1000); }); } }