n8n/packages/editor-ui/src/init.ts
Milorad FIlipović 8b99384367
fix(editor): Fix cloud plan data loading on instance (#7841)
Moving cloud hooks and store initialization logic after users are
authenticated. This will ensure user local account is available when
their cloud plan data is being fetched.
This PR also adds the following error handling improvements:
- Added error handling to the same initialization logic
- Fixed empty `catch` clauses inside the cloud store which caused it to
silently fail and complicated debugging of this bug
2023-11-29 10:51:15 +01:00

81 lines
2 KiB
TypeScript

import { useCloudPlanStore } from '@/stores/cloudPlan.store';
import { useNodeTypesStore } from '@/stores/nodeTypes.store';
import { useRootStore } from '@/stores/n8nRoot.store';
import { useSettingsStore } from '@/stores/settings.store';
import { useSourceControlStore } from '@/stores/sourceControl.store';
import { useUsersStore } from '@/stores/users.store';
import { initializeCloudHooks } from '@/hooks/register';
let coreInitialized = false;
let authenticatedFeaturesInitialized = false;
/**
* Initializes the core application stores and hooks
* This is called once, when the first route is loaded.
*/
export async function initializeCore() {
if (coreInitialized) {
return;
}
const settingsStore = useSettingsStore();
const usersStore = useUsersStore();
await settingsStore.initialize();
await usersStore.initialize();
if (settingsStore.isCloudDeployment) {
try {
await initializeCloudHooks();
} catch (e) {
console.error('Failed to initialize cloud hooks:', e);
}
}
coreInitialized = true;
}
/**
* Initializes the features of the application that require an authenticated user
*/
export async function initializeAuthenticatedFeatures() {
if (authenticatedFeaturesInitialized) {
return;
}
const usersStore = useUsersStore();
if (!usersStore.currentUser) {
return;
}
const sourceControlStore = useSourceControlStore();
const settingsStore = useSettingsStore();
const rootStore = useRootStore();
const nodeTypesStore = useNodeTypesStore();
const cloudPlanStore = useCloudPlanStore();
if (sourceControlStore.isEnterpriseSourceControlEnabled) {
await sourceControlStore.getPreferences();
}
if (settingsStore.isTemplatesEnabled) {
try {
await settingsStore.testTemplatesEndpoint();
} catch (e) {}
}
if (rootStore.defaultLocale !== 'en') {
await nodeTypesStore.getNodeTranslationHeaders();
}
if (settingsStore.isCloudDeployment) {
try {
await cloudPlanStore.initialize();
} catch (e) {
console.error('Failed to initialize cloud plan store:', e);
}
}
authenticatedFeaturesInitialized = true;
}