diff --git a/packages/api/src/controllers/cloud.js b/packages/api/src/controllers/cloud.js index c5db7fb37..4888cc826 100644 --- a/packages/api/src/controllers/cloud.js +++ b/packages/api/src/controllers/cloud.js @@ -59,6 +59,7 @@ module.exports = { async putContent({ folid, cntid, content, name, type }) { const resp = await putCloudContent(folid, cntid, content, name, type); socket.emitChanged('cloud-content-changed'); + socket.emit('cloud-content-updated'); return resp; }, @@ -66,6 +67,7 @@ module.exports = { async createFolder({ name }) { const resp = await callCloudApiPost(`folders/create`, { name }); socket.emitChanged('cloud-content-changed'); + socket.emit('cloud-content-updated'); return resp; }, @@ -80,6 +82,7 @@ module.exports = { const resp = await callCloudApiPost(`folders/grant/${mode}`, { invite }); socket.emitChanged('cloud-content-changed'); + socket.emit('cloud-content-updated'); return resp; }, @@ -87,6 +90,7 @@ module.exports = { async renameFolder({ folid, name }) { const resp = await callCloudApiPost(`folders/rename`, { folid, name }); socket.emitChanged('cloud-content-changed'); + socket.emit('cloud-content-updated'); return resp; }, @@ -94,6 +98,7 @@ module.exports = { async deleteFolder({ folid }) { const resp = await callCloudApiPost(`folders/delete`, { folid }); socket.emitChanged('cloud-content-changed'); + socket.emit('cloud-content-updated'); return resp; }, @@ -106,6 +111,7 @@ module.exports = { refreshContent_meta: true, async refreshContent() { socket.emitChanged('cloud-content-changed'); + socket.emit('cloud-content-updated'); return { status: 'ok', }; @@ -156,6 +162,7 @@ module.exports = { removeCloudCachedConnection(folid, resp.cntid); cntid = resp.cntid; socket.emitChanged('cloud-content-changed'); + socket.emit('cloud-content-updated'); return { ...recryptedConn, _id: `cloud://${folid}/${cntid}`, diff --git a/packages/api/src/utility/cloudIntf.js b/packages/api/src/utility/cloudIntf.js index d1dfbdc7d..6e845b212 100644 --- a/packages/api/src/utility/cloudIntf.js +++ b/packages/api/src/utility/cloudIntf.js @@ -320,6 +320,7 @@ async function putCloudContent(folid, cntid, content, name, type) { signinHolder ); socket.emitChanged('cloud-content-changed'); + socket.emit('cloud-content-updated'); return resp; } diff --git a/packages/web/src/App.svelte b/packages/web/src/App.svelte index e5acac607..124be6c23 100644 --- a/packages/web/src/App.svelte +++ b/packages/web/src/App.svelte @@ -28,6 +28,7 @@ import { handleAuthOnStartup } from './clientAuth'; import { initializeAppUpdates } from './utility/appUpdate'; import { _t } from './translations'; + import { installCloudListeners } from './utility/cloudListeners'; export let isAdminPage = false; @@ -58,6 +59,7 @@ installNewVolatileConnectionListener(); installNewCloudTokenListener(); initializeAppUpdates(); + installCloudListeners(); } refreshPublicCloudFiles(); diff --git a/packages/web/src/stores.ts b/packages/web/src/stores.ts index 1de2cdc71..5dfdb4c99 100644 --- a/packages/web/src/stores.ts +++ b/packages/web/src/stores.ts @@ -457,4 +457,10 @@ focusedTreeDbKey.subscribe(value => { }); export const getFocusedTreeDbKey = () => focusedTreeDbKeyValue; +let cloudConnectionsStoreValue = {}; +cloudConnectionsStore.subscribe(value => { + cloudConnectionsStoreValue = value; +}); +export const getCloudConnectionsStore = () => cloudConnectionsStoreValue; + window['__changeCurrentTheme'] = theme => currentTheme.set(theme); diff --git a/packages/web/src/utility/cloudListeners.ts b/packages/web/src/utility/cloudListeners.ts new file mode 100644 index 000000000..501cc2f65 --- /dev/null +++ b/packages/web/src/utility/cloudListeners.ts @@ -0,0 +1,48 @@ +import { derived } from 'svelte/store'; +import { + cloudConnectionsStore, + currentDatabase, + getCloudConnectionsStore, + openedConnections, + openedSingleDatabaseConnections, +} from '../stores'; +import { apiCall, apiOn } from './api'; +import _ from 'lodash'; + +export const possibleCloudConnectionSources = derived( + [currentDatabase, openedSingleDatabaseConnections, openedConnections], + ([$currentDatabase, $openedSingleDatabaseConnections, $openedConnections]) => { + const conids = new Set(); + if ($currentDatabase?.connection?._id) { + conids.add($currentDatabase.connection._id); + } + $openedSingleDatabaseConnections.forEach(x => conids.add(x)); + $openedConnections.forEach(x => conids.add(x)); + return Array.from(conids).filter(x => x?.startsWith('cloud://')); + } +); + +async function loadCloudConnection(conid) { + const conn = await apiCall('connections/get', { conid }); + cloudConnectionsStore.update(store => ({ + ...store, + [conid]: conn, + })); +} + +function ensureCloudConnectionsLoaded(...conids) { + const conns = getCloudConnectionsStore(); + _.uniq(conids).forEach(conid => { + if (!conns[conid]) { + loadCloudConnection(conid); + } + }); +} + +export function installCloudListeners() { + possibleCloudConnectionSources.subscribe(conids => { + ensureCloudConnectionsLoaded(...conids); + }); + + apiOn('cloud-content-updated', () => cloudConnectionsStore.set({})); +} diff --git a/packages/web/src/widgets/PrivateCloudWidget.svelte b/packages/web/src/widgets/PrivateCloudWidget.svelte index 49d2c01e9..5b6cf38c0 100644 --- a/packages/web/src/widgets/PrivateCloudWidget.svelte +++ b/packages/web/src/widgets/PrivateCloudWidget.svelte @@ -67,27 +67,27 @@ await apiCall('cloud/refresh-content'); } - async function loadCloudConnection(conid) { - const conn = await apiCall('connections/get', { conid }); - $cloudConnectionsStore = { - ...$cloudConnectionsStore, - [conid]: conn, - }; - } + // async function loadCloudConnection(conid) { + // const conn = await apiCall('connections/get', { conid }); + // $cloudConnectionsStore = { + // ...$cloudConnectionsStore, + // [conid]: conn, + // }; + // } - function ensureCloudConnectionsLoaded(...conids) { - _.uniq(conids).forEach(conid => { - if (conid?.startsWith('cloud://') && !$cloudConnectionsStore[conid]) { - loadCloudConnection(conid); - } - }); - } + // function ensureCloudConnectionsLoaded(...conids) { + // _.uniq(conids).forEach(conid => { + // if (conid?.startsWith('cloud://') && !$cloudConnectionsStore[conid]) { + // loadCloudConnection(conid); + // } + // }); + // } - $: ensureCloudConnectionsLoaded( - $currentDatabase?.connection?._id, - ...$openedSingleDatabaseConnections, - ...$openedConnections - ); + // $: ensureCloudConnectionsLoaded( + // $currentDatabase?.connection?._id, + // ...$openedSingleDatabaseConnections, + // ...$openedConnections + // ); // onMount(() => { // const currentConid = $currentDatabase?.connection?._id;