move startupService to common

This commit is contained in:
DEV Sam Hayes
2025-02-04 20:13:45 +01:00
parent a652718bc7
commit b21701f677
8 changed files with 37 additions and 25 deletions

View File

@@ -1,7 +1,7 @@
import { Component, inject, OnInit } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { LoggerService } from '@common';
import { StartupService } from './services/startup/startup.service';
import { LoggerService, StartupService } from '@common';
import { getNewStorageServiceConfig } from './common/data/get-new-storage-service-config';
@Component({
selector: 'app-root',
@@ -15,6 +15,7 @@ export class AppComponent implements OnInit {
ngOnInit(): void {
this.#logger.initialize('Gooti Chrome Extension');
this.#startup.startOver();
this.#startup.startOver(getNewStorageServiceConfig());
}
}

View File

@@ -0,0 +1,16 @@
import { StorageServiceConfig } from '@common';
import { ChromeSessionHandler } from './chrome-session-handler';
import { ChromeSyncYesHandler } from './chrome-sync-yes-handler';
import { ChromeSyncNoHandler } from './chrome-sync-no-handler';
import { ChromeMetaHandler } from './chrome-meta-handler';
export const getNewStorageServiceConfig = () => {
const storageConfig: StorageServiceConfig = {
browserSessionHandler: new ChromeSessionHandler(),
browserSyncYesHandler: new ChromeSyncYesHandler(),
browserSyncNoHandler: new ChromeSyncNoHandler(),
gootiMetaHandler: new ChromeMetaHandler(),
};
return storageConfig;
};

View File

@@ -4,9 +4,10 @@ import {
BrowserSyncFlow,
ConfirmComponent,
DateHelper,
StartupService,
StorageService,
} from '@common';
import { StartupService } from '../../../services/startup/startup.service';
import { getNewStorageServiceConfig } from '../../../common/data/get-new-storage-service-config';
@Component({
selector: 'app-settings',
@@ -43,7 +44,7 @@ export class SettingsComponent implements OnInit {
async onDeleteVault() {
try {
await this.#storage.deleteVault();
this.#startup.startOver();
this.#startup.startOver(getNewStorageServiceConfig());
} catch (error) {
console.log(error);
// TODO
@@ -68,7 +69,7 @@ export class SettingsComponent implements OnInit {
await this.#storage.deleteVault(true);
await this.#storage.importVault(vault);
this.#storage.isInitialized = false;
this.#startup.startOver();
this.#startup.startOver(getNewStorageServiceConfig());
} catch (error) {
console.log(error);
// TODO

View File

@@ -1,7 +1,7 @@
import { Component, inject } from '@angular/core';
import { Router } from '@angular/router';
import { BrowserSyncData, StorageService } from '@common';
import { StartupService } from '../../../services/startup/startup.service';
import { BrowserSyncData, StartupService, StorageService } from '@common';
import { getNewStorageServiceConfig } from '../../../common/data/get-new-storage-service-config';
@Component({
selector: 'app-home',
@@ -27,7 +27,7 @@ export class HomeComponent {
console.log(vault);
await this.#storage.importVault(vault);
this.#startup.startOver();
this.#startup.startOver(getNewStorageServiceConfig());
} catch (error) {
console.log(error);
// TODO

View File

@@ -1,8 +1,8 @@
import { Component, inject } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import { ConfirmComponent, StorageService } from '@common';
import { StartupService } from '../../services/startup/startup.service';
import { ConfirmComponent, StartupService, StorageService } from '@common';
import { getNewStorageServiceConfig } from '../../common/data/get-new-storage-service-config';
@Component({
selector: 'app-vault-login',
@@ -46,7 +46,7 @@ export class VaultLoginComponent {
async onClickDeleteVault() {
try {
await this.#storage.deleteVault();
this.#startup.startOver();
this.#startup.startOver(getNewStorageServiceConfig());
} catch (error) {
console.log(error);
// TODO

View File

@@ -1,16 +0,0 @@
import { TestBed } from '@angular/core/testing';
import { StartupService } from './startup.service';
describe('StartupService', () => {
let service: StartupService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(StartupService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@@ -1,88 +0,0 @@
import { inject, Injectable } from '@angular/core';
import { LoggerService, StorageService, StorageServiceConfig } from '@common';
import { ChromeSessionHandler } from '../../common/data/chrome-session-handler';
import { ChromeSyncYesHandler } from '../../common/data/chrome-sync-yes-handler';
import { ChromeSyncNoHandler } from '../../common/data/chrome-sync-no-handler';
import { ChromeMetaHandler } from '../../common/data/chrome-meta-handler';
import { Router } from '@angular/router';
@Injectable({
providedIn: 'root',
})
export class StartupService {
readonly #logger = inject(LoggerService);
readonly #storage = inject(StorageService);
readonly #router = inject(Router);
async startOver() {
const storageConfig: StorageServiceConfig = {
browserSessionHandler: new ChromeSessionHandler(),
browserSyncYesHandler: new ChromeSyncYesHandler(),
browserSyncNoHandler: new ChromeSyncNoHandler(),
gootiMetaHandler: new ChromeMetaHandler(),
};
this.#storage.initialize(storageConfig);
// Step 0:
storageConfig.browserSyncNoHandler.setIgnoreProperties(
storageConfig.gootiMetaHandler.metaProperties
);
// Step 1: Load the gooti's user settings
const gootiMetaData = await this.#storage.loadGootiMetaData();
if (typeof gootiMetaData?.syncFlow === 'undefined') {
// Very first run. The user has not set up Gooti yet.
this.#router.navigateByUrl('/welcome');
return;
}
this.#storage.enableBrowserSyncFlow(gootiMetaData.syncFlow);
// Load the browser session data.
const browserSessionData = await this.#storage.loadBrowserSessionData();
if (!browserSessionData) {
await this.#initializeFlow_A();
} else {
await this.#initializeFlow_B();
}
}
async #initializeFlow_A() {
// Starting with NO browser session data available.
//
// This could be because the browser sync data was
// never loaded before OR it was attempted, but
// there is no browser sync data.
this.#logger.log('No browser session data available.');
// Check if there is NO browser sync data.
const browserSyncData = await this.#storage.loadAndMigrateBrowserSyncData();
if (browserSyncData) {
// There is browser sync data. Route to the VAULT LOGIN to enable the session.
this.#router.navigateByUrl('/vault-login');
} else {
// There is NO browser sync data. Route to the VAULT CREATION to enable the session.
this.#router.navigateByUrl('/vault-create/home');
}
}
async #initializeFlow_B() {
// Stating with browser session data available. The user has already unlocked the vault before.
// Route to VAULT HOME.
this.#logger.log('Browser session data is available.');
// Also load the browser sync data. This is needed, if the user adds or deletes anything.
await this.#storage.loadAndMigrateBrowserSyncData();
const selectedIdentityId =
this.#storage.getBrowserSessionHandler().browserSessionData
?.selectedIdentityId;
this.#router.navigateByUrl(
`/home/${selectedIdentityId ? 'identity' : 'identities'}`
);
}
}