Skip to content

Commit

Permalink
feat: merge branch 'arb-ltip' into deployments/arbitrum
Browse files Browse the repository at this point in the history
  • Loading branch information
acaldas committed Jul 19, 2024
2 parents a3b184a + 1a7e916 commit 56800e6
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-deploy-arbitrum-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion .github/workflows/build-and-deploy-arbitrum-staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
240 changes: 153 additions & 87 deletions src/hooks/useLoadDefaultDrives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -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;
Expand Down

0 comments on commit 56800e6

Please sign in to comment.