From 67020b0e72864bf7336b6a60a6b1522ac0563b9c Mon Sep 17 00:00:00 2001 From: Guillermo Puente Date: Tue, 3 Sep 2024 18:26:58 -0400 Subject: [PATCH] feat: remove default drives info from connect + add default drives to reactor config --- package-lock.json | 63 ++++---- package.json | 2 +- src/app/document-drive.ts | 2 + src/components/modal/modals/SettingsModal.tsx | 11 -- src/hooks/useFeatureFlags/default-config.ts | 12 -- src/hooks/useLoadDefaultDrives.ts | 139 ------------------ src/hooks/useLoadInitialData.tsx | 3 - src/hooks/useUiNodes.ts | 20 ++- src/utils/browser-document-drive.ts | 2 + src/utils/reactor.ts | 48 ++++++ 10 files changed, 100 insertions(+), 202 deletions(-) delete mode 100644 src/hooks/useLoadDefaultDrives.ts create mode 100644 src/utils/reactor.ts diff --git a/package-lock.json b/package-lock.json index 6dec1fe7..57fa1168 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,7 @@ "@sentry/vite-plugin": "^2.22.2", "@tanstack/react-virtual": "^3.8.1", "did-key-creator": "^1.2.0", - "document-drive": "^1.0.0-alpha.88", + "document-drive": "1.0.0-alpha.89", "document-model": "1.7.0", "document-model-libs": "^1.83.0", "electron-is-dev": "^3.0.1", @@ -6849,9 +6849,9 @@ } }, "node_modules/@prisma/client": { - "version": "5.17.0", - "resolved": "https://registry.npmjs.org/@prisma/client/-/client-5.17.0.tgz", - "integrity": "sha512-N2tnyKayT0Zf7mHjwEyE8iG7FwTmXDHFZ1GnNhQp0pJUObsuel4ZZ1XwfuAYkq5mRIiC/Kot0kt0tGCfLJ70Jw==", + "version": "5.19.1", + "resolved": "https://registry.npmjs.org/@prisma/client/-/client-5.19.1.tgz", + "integrity": "sha512-x30GFguInsgt+4z5I4WbkZP2CGpotJMUXy+Gl/aaUjHn2o1DnLYNTA+q9XdYmAQZM8fIIkvUiA2NpgosM3fneg==", "hasInstallScript": true, "optional": true, "engines": { @@ -8837,9 +8837,9 @@ } }, "node_modules/@redis/client": { - "version": "1.5.17", - "resolved": "https://registry.npmjs.org/@redis/client/-/client-1.5.17.tgz", - "integrity": "sha512-IPvU9A31qRCZ7lds/x+ksuK/UMndd0EASveAvCvEtFFKIZjZ+m/a4a0L7S28KEWoR5ka8526hlSghDo4Hrc2Hg==", + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@redis/client/-/client-1.6.0.tgz", + "integrity": "sha512-aR0uffYI700OEEH4gYnitAnv3vzVGXCFvYfdpu/CJKvk4pHfLPEy/JSZyrpQ+15WhXe1yJRXLtfQ84s4mEXnPg==", "optional": true, "dependencies": { "cluster-key-slot": "1.1.2", @@ -8866,27 +8866,27 @@ } }, "node_modules/@redis/json": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/@redis/json/-/json-1.0.6.tgz", - "integrity": "sha512-rcZO3bfQbm2zPRpqo82XbW8zg4G/w4W3tI7X8Mqleq9goQjAGLL7q/1n1ZX4dXEAmORVZ4s1+uKLaUOg7LrUhw==", + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/@redis/json/-/json-1.0.7.tgz", + "integrity": "sha512-6UyXfjVaTBTJtKNG4/9Z8PSpKE6XgSyEb8iwaqDcy+uKrd/DGYHTWkUdnQDyzm727V7p21WUMhsqz5oy65kPcQ==", "optional": true, "peerDependencies": { "@redis/client": "^1.0.0" } }, "node_modules/@redis/search": { - "version": "1.1.6", - "resolved": "https://registry.npmjs.org/@redis/search/-/search-1.1.6.tgz", - "integrity": "sha512-mZXCxbTYKBQ3M2lZnEddwEAks0Kc7nauire8q20oA0oA/LoA+E/b5Y5KZn232ztPb1FkIGqo12vh3Lf+Vw5iTw==", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@redis/search/-/search-1.2.0.tgz", + "integrity": "sha512-tYoDBbtqOVigEDMAcTGsRlMycIIjwMCgD8eR2t0NANeQmgK/lvxNAvYyb6bZDD4frHRhIHkJu2TBRvB0ERkOmw==", "optional": true, "peerDependencies": { "@redis/client": "^1.0.0" } }, "node_modules/@redis/time-series": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/@redis/time-series/-/time-series-1.0.5.tgz", - "integrity": "sha512-IFjIgTusQym2B5IZJG3XKr5llka7ey84fw/NOYqESP5WUfQs9zz1ww/9+qoz4ka/S6KcGBodzlCeZ5UImKbscg==", + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@redis/time-series/-/time-series-1.1.0.tgz", + "integrity": "sha512-c1Q99M5ljsIuc4YdaCwfUEXsofakb9c8+Zse2qxTadu8TalLXuAESzLvFAvNVbkmSlvlzIQOLpBCmWI9wTOt+g==", "optional": true, "peerDependencies": { "@redis/client": "^1.0.0" @@ -11515,9 +11515,9 @@ "dev": true }, "node_modules/@types/validator": { - "version": "13.12.0", - "resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.12.0.tgz", - "integrity": "sha512-nH45Lk7oPIJ1RVOF6JgFI6Dy0QpHEzq4QecZhvguxYPDwT8c93prCMqAtiIttm39voZ+DDR+qkNnMpJmMBRqag==", + "version": "13.12.1", + "resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.12.1.tgz", + "integrity": "sha512-w0URwf7BQb0rD/EuiG12KP0bailHKHP5YVviJG9zw3ykAokL0TuxU2TUqMB7EwZ59bDHYdeTIvjI5m0S7qHfOA==", "optional": true }, "node_modules/@types/wicg-file-system-access": { @@ -15874,9 +15874,9 @@ } }, "node_modules/document-drive": { - "version": "1.0.0-alpha.88", - "resolved": "https://registry.npmjs.org/document-drive/-/document-drive-1.0.0-alpha.88.tgz", - "integrity": "sha512-HwLtyr9wo5Q4c6aMKOK8rFqXO0NZwB07gVljCeCqrromcDekHeQREvYUQaqXlo4RKhQP3dyyzcI6itzS0g5qjQ==", + "version": "1.0.0-alpha.89", + "resolved": "https://registry.npmjs.org/document-drive/-/document-drive-1.0.0-alpha.89.tgz", + "integrity": "sha512-icOYZ0FSkE5iGhnPOPRrH8zbYIxeDnj8mDrVwZ7K25YOxXJd0XO7hgqwfNFjeO2fGcxsAG5fl1r2512F7gilGQ==", "dependencies": { "exponential-backoff": "^3.1.1", "graphql": "^16.9.0", @@ -15887,7 +15887,7 @@ "uuid": "^9.0.1" }, "optionalDependencies": { - "@prisma/client": "5.17.0", + "@prisma/client": "^5.18.0", "localforage": "^1.10.0", "redis": "^4.6.15", "sequelize": "^6.37.3", @@ -29141,20 +29141,17 @@ } }, "node_modules/redis": { - "version": "4.6.15", - "resolved": "https://registry.npmjs.org/redis/-/redis-4.6.15.tgz", - "integrity": "sha512-2NtuOpMW3tnYzBw6S8mbXSX7RPzvVFCA2wFJq9oErushO2UeBkxObk+uvo7gv7n0rhWeOj/IzrHO8TjcFlRSOg==", + "version": "4.7.0", + "resolved": "https://registry.npmjs.org/redis/-/redis-4.7.0.tgz", + "integrity": "sha512-zvmkHEAdGMn+hMRXuMBtu4Vo5P6rHQjLoHftu+lBqq8ZTA3RCVC/WzD790bkKKiNFp7d5/9PcSD19fJyyRvOdQ==", "optional": true, - "workspaces": [ - "./packages/*" - ], "dependencies": { "@redis/bloom": "1.2.0", - "@redis/client": "1.5.17", + "@redis/client": "1.6.0", "@redis/graph": "1.1.1", - "@redis/json": "1.0.6", - "@redis/search": "1.1.6", - "@redis/time-series": "1.0.5" + "@redis/json": "1.0.7", + "@redis/search": "1.2.0", + "@redis/time-series": "1.1.0" } }, "node_modules/reflect.getprototypeof": { diff --git a/package.json b/package.json index 4062126e..e7fb0047 100644 --- a/package.json +++ b/package.json @@ -96,7 +96,7 @@ "@sentry/vite-plugin": "^2.22.2", "@tanstack/react-virtual": "^3.8.1", "did-key-creator": "^1.2.0", - "document-drive": "^1.0.0-alpha.88", + "document-drive": "1.0.0-alpha.89", "document-model": "1.7.0", "document-model-libs": "^1.83.0", "electron-is-dev": "^3.0.1", diff --git a/src/app/document-drive.ts b/src/app/document-drive.ts index 516810aa..6f3ccfc2 100644 --- a/src/app/document-drive.ts +++ b/src/app/document-drive.ts @@ -17,6 +17,7 @@ import { import { IpcMain, webContents } from 'electron'; import { join } from 'path'; import { logger } from 'src/services/logger'; +import { getReactorDefaultDrivesConfig } from 'src/utils/reactor'; export default ( documentModels: DocumentModel[], @@ -28,6 +29,7 @@ export default ( new FilesystemStorage(join(path, 'Document Drives')), new InMemoryCache(), new BaseQueueManager(1, 10), + { ...getReactorDefaultDrivesConfig() }, ); const promise = documentDrive diff --git a/src/components/modal/modals/SettingsModal.tsx b/src/components/modal/modals/SettingsModal.tsx index 43d61492..8ba18173 100644 --- a/src/components/modal/modals/SettingsModal.tsx +++ b/src/components/modal/modals/SettingsModal.tsx @@ -71,17 +71,6 @@ export const SettingsModal: React.FC = props => { onContinue: () => { clearStorage() .then(() => { - // resets the default drive to unloaded if it is defined - setConfig(conf => ({ - ...conf, - defaultDrives: conf.defaultDrives - ? conf.defaultDrives.map(drive => ({ - ...drive, - loaded: false, - })) - : undefined, - })); - // refreshes the page to reload default drive onRefresh(); }) diff --git a/src/hooks/useFeatureFlags/default-config.ts b/src/hooks/useFeatureFlags/default-config.ts index 0aaf847f..7d75cf77 100644 --- a/src/hooks/useFeatureFlags/default-config.ts +++ b/src/hooks/useFeatureFlags/default-config.ts @@ -5,15 +5,9 @@ const enabledEditors = ENABLED_EDITORS?.split(','); const DISABLED_EDITORS = import.meta.env.PH_CONNECT_DISABLED_EDITORS || undefined; -const DEFAULT_DRIVES_URL = - import.meta.env.PH_CONNECT_DEFAULT_DRIVES_URL || undefined; const disabledEditors = DISABLED_EDITORS?.split(','); export interface FeatureFlag { - defaultDrives?: { - url: string; - loaded: boolean; - }[]; editors: { enabledEditors?: '*' | string[]; disabledEditors?: '*' | string[]; @@ -21,12 +15,6 @@ export interface FeatureFlag { } const defaultConfig: FeatureFlag = { - defaultDrives: DEFAULT_DRIVES_URL - ? DEFAULT_DRIVES_URL.split(',').map(url => ({ - url, - loaded: false, - })) - : undefined, editors: { enabledEditors: ENABLED_EDITORS === '*' ? '*' : enabledEditors, disabledEditors: DISABLED_EDITORS === '*' ? '*' : disabledEditors, diff --git a/src/hooks/useLoadDefaultDrives.ts b/src/hooks/useLoadDefaultDrives.ts deleted file mode 100644 index daed457f..00000000 --- a/src/hooks/useLoadDefaultDrives.ts +++ /dev/null @@ -1,139 +0,0 @@ -import { useEffect, useRef } from 'react'; -import { logger } from 'src/services/logger'; -import { useDocumentDriveServer } from './useDocumentDriveServer'; -import { useFeatureFlag } from './useFeatureFlags'; -import defaultConfig from './useFeatureFlags/default-config'; - -type DefaultDrive = { - url: string; - loaded: boolean; -}; - -const areLoadedDrivesUpToDate = ( - defaultDrivesConfig: DefaultDrive[], - loadedDrives: DefaultDrive[], -) => { - for (const defaultDrive of defaultDrivesConfig) { - const loadedDrive = loadedDrives.find( - loadedDrive => loadedDrive.url === defaultDrive.url, - ); - - if (!loadedDrive) { - return false; - } - } - - return true; -}; - -export const useLoadDefaultDrives = () => { - const loadingDrives = useRef([]); - const { - addRemoteDrive, - documentDrives, - documentDrivesStatus, - clearStorage, - } = useDocumentDriveServer(); - const { - setConfig, - config: { defaultDrives }, - } = useFeatureFlag(); - - async function resetDefaultDrive() { - await clearStorage(); - setConfig(defaultConfig); - location.reload(); - loadingDrives.current = []; - } - - useEffect(() => { - if (!defaultDrives) return; - - // reset default drives if config has been updated - if ( - loadingDrives.current.length <= 0 && - defaultDrives.every(drive => drive.loaded) && - defaultConfig.defaultDrives && - defaultConfig.defaultDrives.length > 0 && - !areLoadedDrivesUpToDate(defaultConfig.defaultDrives, defaultDrives) - ) { - void resetDefaultDrive(); - return; - } - - for (const defaultDrive of defaultDrives) { - if ( - documentDrivesStatus === 'LOADED' && - !defaultDrive.loaded && - !loadingDrives.current.includes(defaultDrive.url) - ) { - const isDriveAlreadyAdded = documentDrives.some(drive => { - return drive.state.local.triggers.some( - trigger => trigger.data?.url === defaultDrive.url, - ); - }); - - if (isDriveAlreadyAdded) { - setConfig(conf => ({ - ...conf, - defaultDrives: [ - ...(conf.defaultDrives || []).filter( - drive => drive.url !== defaultDrive.url, - ), - { ...defaultDrive, loaded: true }, - ], - })); - - return; - } - - loadingDrives.current.push(defaultDrive.url); - - addRemoteDrive(defaultDrive.url, { - sharingType: 'PUBLIC', - availableOffline: true, - listeners: [ - { - block: true, - callInfo: { - data: defaultDrive.url, - name: 'switchboard-push', - transmitterType: 'SwitchboardPush', - }, - filter: { - branch: ['main'], - documentId: ['*'], - documentType: ['*'], - scope: ['global'], - }, - label: 'Switchboard Sync', - listenerId: '1', - system: true, - }, - ], - triggers: [], - pullInterval: 3000, - }) - .then(() => - setConfig(conf => ({ - ...conf, - defaultDrives: [ - ...(conf.defaultDrives || []).filter( - drive => drive.url !== defaultDrive.url, - ), - { ...defaultDrive, loaded: true }, - ], - })), - ) - .catch(logger.error) - .finally(() => { - loadingDrives.current = loadingDrives.current.filter( - url => url !== defaultDrive.url, - ); - }); - } - } - }, [documentDrives, defaultDrives, documentDrivesStatus]); - - return loadingDrives.current.length > 0; -}; diff --git a/src/hooks/useLoadInitialData.tsx b/src/hooks/useLoadInitialData.tsx index 320a4754..f0849f56 100644 --- a/src/hooks/useLoadInitialData.tsx +++ b/src/hooks/useLoadInitialData.tsx @@ -15,7 +15,6 @@ import { useUiNodes } from 'src/hooks/useUiNodes'; import { DefaultDocumentDriveServer as server } from 'src/utils/document-drive-server'; import { useClientErrorHandler } from './useClientErrorHandler'; import { useDocumentDrives } from './useDocumentDrives'; -import { useLoadDefaultDrives } from './useLoadDefaultDrives'; import { isLatestVersion } from './utils'; export const useLoadInitialData = () => { @@ -32,8 +31,6 @@ export const useLoadInitialData = () => { const [, , serverSubscribeUpdates] = useDocumentDrives(server); const clientErrorHandler = useClientErrorHandler(); - useLoadDefaultDrives(); - async function checkLatestVersion() { const result = await isLatestVersion(); if (result === null) return; diff --git a/src/hooks/useUiNodes.ts b/src/hooks/useUiNodes.ts index a0863d2c..01b8a7c4 100644 --- a/src/hooks/useUiNodes.ts +++ b/src/hooks/useUiNodes.ts @@ -84,6 +84,12 @@ export function useUiNodes() { ) as SharingType; const driveSyncStatus = await getSyncStatus(id, sharingType); + // TODO: rempve this after integration in design-system + const normalizedDriveSyncStatus = + driveSyncStatus === 'INITIAL_SYNC' + ? 'SYNCING' + : driveSyncStatus; + const driveNode: UiDriveNode = { id, name, @@ -92,7 +98,7 @@ export function useUiNodes() { children: [], nodeMap: {}, sharingType, - syncStatus: driveSyncStatus, + syncStatus: normalizedDriveSyncStatus, availableOffline, icon, parentFolder: null, @@ -106,7 +112,7 @@ export function useUiNodes() { driveId: id, parentFolder: n.parentFolder || id, kind: n.kind.toUpperCase(), - syncStatus: driveSyncStatus, + syncStatus: normalizedDriveSyncStatus, sharingType, }; @@ -130,10 +136,18 @@ export function useUiNodes() { for await (const node of nodes) { if (node.kind === FILE) { - node.syncStatus = await getSyncStatus( + const fileSyncStatus = await getSyncStatus( node.synchronizationUnits[0].syncId, sharingType, ); + + // TODO: rempve this after integration in design-system + const normalizedFileSyncStatus = + fileSyncStatus === 'INITIAL_SYNC' + ? 'SYNCING' + : fileSyncStatus; + + node.syncStatus = normalizedFileSyncStatus; } if (node.parentFolder === id) { diff --git a/src/utils/browser-document-drive.ts b/src/utils/browser-document-drive.ts index b1cc1769..679c7471 100644 --- a/src/utils/browser-document-drive.ts +++ b/src/utils/browser-document-drive.ts @@ -6,12 +6,14 @@ import { BrowserStorage } from 'document-drive/storage/browser'; import { utils } from 'document-model/document'; import { logger } from 'src/services/logger'; import { documentModels } from 'src/store/document-model'; +import { getReactorDefaultDrivesConfig } from './reactor'; export const BrowserDocumentDriveServer = new DocumentDriveServer( documentModels, new BrowserStorage(connectConfig.routerBasename), new InMemoryCache(), new BaseQueueManager(1, 10), + { ...getReactorDefaultDrivesConfig() }, ); async function init() { diff --git a/src/utils/reactor.ts b/src/utils/reactor.ts new file mode 100644 index 00000000..dcccf8fc --- /dev/null +++ b/src/utils/reactor.ts @@ -0,0 +1,48 @@ +import { DocumentDriveServerOptions } from 'document-drive/server'; + +const DEFAULT_DRIVES_URL = + import.meta.env.PH_CONNECT_DEFAULT_DRIVES_URL || undefined; +const defaultDrivesUrl = DEFAULT_DRIVES_URL?.split(','); + +export const getReactorDefaultDrivesConfig = (): Pick< + DocumentDriveServerOptions, + 'defaultRemoteDrives' | 'removeOldRemoteDrives' +> => { + const defaultDrives: DocumentDriveServerOptions['defaultRemoteDrives'] = + defaultDrivesUrl?.map(driveUrl => ({ + url: driveUrl, + options: { + sharingType: 'PUBLIC', + availableOffline: true, + listeners: [ + { + block: true, + callInfo: { + data: driveUrl, + name: 'switchboard-push', + transmitterType: 'SwitchboardPush', + }, + filter: { + branch: ['main'], + documentId: ['*'], + documentType: ['*'], + scope: ['global'], + }, + label: 'Switchboard Sync', + listenerId: '1', + system: true, + }, + ], + triggers: [], + pullInterval: 3000, + }, + })); + + return { + defaultRemoteDrives: defaultDrives, + removeOldRemoteDrives: { + strategy: 'preserve-by-url', + urls: defaultDrivesUrl || [], + }, + }; +};