diff --git a/.github/workflows/build-and-deploy-arbitrum-prod.yaml b/.github/workflows/build-and-deploy-arbitrum-prod.yaml index e9ed2b41..4b94879e 100644 --- a/.github/workflows/build-and-deploy-arbitrum-prod.yaml +++ b/.github/workflows/build-and-deploy-arbitrum-prod.yaml @@ -36,7 +36,7 @@ jobs: --build-arg VITE_DISABLE_DELETE_CLOUD_DRIVES=true --build-arg VITE_DISABLE_DELETE_LOCAL_DRIVES=true --build-arg VITE_LOCAL_DRIVES_ENABLED=false - --build-arg VITE_DEFAULT_DRIVES_URL=https://apps.powerhouse.io/arbitrum/switchboard/d/arbitrum-ltipp-grantees + --build-arg VITE_DEFAULT_DRIVES_URL=https://apps.powerhouse.io/arbitrum/switchboard/d/arbitrum-ltipp-grantees,https://apps.powerhouse.io/arbitrum/switchboard/d/arbitrum-stip-grantees --build-arg VITE_RENOWN_CHAIN_ID=42161 --build-arg VITE_HIDE_DOCUMENT_MODEL_SELECTION_SETTINGS=true process_type: web \ No newline at end of file diff --git a/.github/workflows/build-and-deploy-arbitrum-staging.yaml b/.github/workflows/build-and-deploy-arbitrum-staging.yaml index 62908104..144951f1 100644 --- a/.github/workflows/build-and-deploy-arbitrum-staging.yaml +++ b/.github/workflows/build-and-deploy-arbitrum-staging.yaml @@ -34,7 +34,7 @@ jobs: --build-arg VITE_DISABLE_DELETE_CLOUD_DRIVES=true --build-arg VITE_DISABLE_DELETE_LOCAL_DRIVES=true --build-arg VITE_LOCAL_DRIVES_ENABLED=false - --build-arg VITE_DEFAULT_DRIVES_URL=https://apps.powerhouse.io/alpha/arbitrum/switchboard/d/arbitrum-ltipp-grantees,https://apps.powerhouse.io/alpha/arbitrum/switchboard/d/arbitrum-stip-grantees + --build-arg VITE_DEFAULT_DRIVES_URL=https://apps.powerhouse.io/alpha/arbitrum/switchboard/d/arbitrum-ltipp-grantees,https://apps.powerhouse.io/alpha/arbitrum/switchboard/d/arbitrum-stip --build-arg VITE_RENOWN_CHAIN_ID=42161 --build-arg VITE_SENTRY_DSN=${{ secrets.SENTRY_DSN }} --build-arg VITE_SENTRY_ENV=${{ secrets.SENTRY_ENV }} diff --git a/src/hooks/useLoadDefaultDrives.ts b/src/hooks/useLoadDefaultDrives.ts index 116985f5..0edf210d 100644 --- a/src/hooks/useLoadDefaultDrives.ts +++ b/src/hooks/useLoadDefaultDrives.ts @@ -8,20 +8,24 @@ type DefaultDrive = { loaded: boolean; }; +const isLoadedDrivesUpToDate = ( + defaultDrive: DefaultDrive, + loadedDrives: DefaultDrive[], +) => { + return !!loadedDrives.find( + loadedDrive => loadedDrive.url === defaultDrive.url, + ); +}; + const areLoadedDrivesUpToDate = ( defaultDrivesConfig: DefaultDrive[], loadedDrives: DefaultDrive[], ) => { for (const defaultDrive of defaultDrivesConfig) { - const loadedDrive = loadedDrives.find( - loadedDrive => loadedDrive.url === defaultDrive.url, - ); - - if (!loadedDrive) { + if (!isLoadedDrivesUpToDate(defaultDrive, loadedDrives)) { return false; } } - return true; }; @@ -31,107 +35,169 @@ export const useLoadDefaultDrives = () => { addRemoteDrive, documentDrives, documentDrivesStatus, - clearStorage, + deleteDrive, } = 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; + // checks if there are default drives missing + // DEFAULT_DRIVES_URL might have changed + const missingDefaultDrives = + defaultConfig.defaultDrives + ?.filter( + driveToLoad => + !defaultDrives.find( + loadedDrive => loadedDrive.url === driveToLoad.url, + ), + ) + .map(driveToLoad => ({ + url: driveToLoad.url, + loaded: false, + })) ?? []; + + // adds missing drives to config with loaded = false + if (missingDefaultDrives.length > 0) { + setConfig(conf => ({ + ...conf, + defaultDrives: [ + ...(conf.defaultDrives ?? []), + ...missingDefaultDrives, + ], + })); } - 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, + // deletes drives that are not in the default drives list + // (specific for ArbGrants) + async function ArbGrantsDeleteOldDrives() { + const drivesToDelete: { id: string; url?: string }[] = []; + for (const loadedDrive of documentDrives) { + const isEnvDefault = loadedDrive.state.local.triggers.find( + trigger => + defaultConfig.defaultDrives?.find( + defaultDrive => + trigger.data?.url === defaultDrive.url, + ), + ); + + if (!isEnvDefault) { + const trigger = loadedDrive.state.local.triggers.find( + trigger => trigger.type === 'PullResponder', + ); + + drivesToDelete.push({ + id: loadedDrive.state.global.id, + url: trigger?.data?.url, + }); + } + } + if (drivesToDelete.length > 0) { + const deletedUrls = drivesToDelete + .filter(drive => drive.url) + .map(drive => drive.url); + try { + await Promise.all( + drivesToDelete.map(drive => deleteDrive(drive.id)), ); - }); - if (isDriveAlreadyAdded) { setConfig(conf => ({ ...conf, - defaultDrives: [ - ...(conf.defaultDrives || []).filter( - drive => drive.url !== defaultDrive.url, - ), - { ...defaultDrive, loaded: true }, - ], + defaultDrives: (conf.defaultDrives || []).filter( + drive => !deletedUrls.includes(drive.url), + ), })); - - return; + location.reload(); + } catch (e) { + console.error(e); } + } + } - 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'], + ArbGrantsDeleteOldDrives() + .then(() => { + 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, + ); }, - label: 'Switchboard Sync', - listenerId: '1', - system: true, - }, - ], - triggers: [], - pullInterval: 10000, - }) - .then(() => - setConfig(conf => ({ - ...conf, - defaultDrives: [ - ...(conf.defaultDrives || []).filter( - drive => drive.url !== defaultDrive.url, - ), - { ...defaultDrive, loaded: true }, - ], - })), - ) - .catch(console.error) - .finally(() => { - loadingDrives.current = loadingDrives.current.filter( - url => 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(console.error) + .finally(() => { + loadingDrives.current = + loadingDrives.current.filter( + url => url !== defaultDrive.url, + ); + }); + } + } + }) + .catch(console.error); }, [documentDrives, defaultDrives, documentDrivesStatus]); return loadingDrives.current.length > 0;