From 1c06a510bd14f73b0331053f12dcca38b48cdc57 Mon Sep 17 00:00:00 2001 From: Clem Fern Date: Sun, 23 Jul 2023 22:23:57 +0200 Subject: [PATCH] wip ref(core): Profile & ProfileGroup add creation methods in ProfileService --- tabby-core/src/services/profiles.service.ts | 46 +++++++++++++++++++ .../components/editProfileModal.component.ts | 2 +- .../profilesSettingsTab.component.ts | 12 ++--- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/tabby-core/src/services/profiles.service.ts b/tabby-core/src/services/profiles.service.ts index 7781bdf0..8355e29d 100644 --- a/tabby-core/src/services/profiles.service.ts +++ b/tabby-core/src/services/profiles.service.ts @@ -9,6 +9,8 @@ import { configMerge, ConfigProxy, ConfigService } from './config.service' import { NotificationsService } from './notifications.service' import { SelectorService } from './selector.service' import deepClone from 'clone-deep' +import { v4 as uuidv4 } from 'uuid' +import slugify from 'slugify' @Injectable({ providedIn: 'root' }) export class ProfilesService { @@ -89,6 +91,28 @@ export class ProfilesService { return clone ? deepClone(list) : list } + /** + * Insert a new Profile in config + * arg: saveConfig (default: true) -> invoke after the Profile was updated + * arg: genId (default: true) -> generate uuid in before pushing Profile into config + */ + async newProfile (profile: PartialProfile, saveConfig = true, genId = true): Promise { + if (genId) { + profile.id = `${profile.type}:custom:${slugify(profile.name)}:${uuidv4()}` + } + + const cProfile = this.config.store.profiles.find(p => p.id === profile.id) + if (cProfile) { + throw new Error(`Cannot insert new Profile, duplicated Id: ${profile.id}`) + } + + this.config.store.profiles.push(profile) + + if (saveConfig) { + return this.config.save() + } + } + /** * Write a Profile in config * arg: saveConfig (default: true) -> invoke after the Profile was updated @@ -417,6 +441,28 @@ export class ProfilesService { return groups } + /** + * Insert a new ProfileGroup in config + * arg: saveConfig (default: true) -> invoke after the Profile was updated + * arg: genId (default: true) -> generate uuid in before pushing Profile into config + */ + async newProfileGroup (group: PartialProfileGroup, saveConfig = true, genId = true): Promise { + if (genId) { + group.id = `${uuidv4()}` + } + + const cProfileGroup = this.config.store.groups.find(p => p.id === group.id) + if (cProfileGroup) { + throw new Error(`Cannot insert new ProfileGroup, duplicated Id: ${group.id}`) + } + + this.config.store.groups.push(group) + + if (saveConfig) { + return this.config.save() + } + } + /** * Write a ProfileGroup in config * arg: saveConfig (default: true) -> invoke after the ProfileGroup was updated diff --git a/tabby-settings/src/components/editProfileModal.component.ts b/tabby-settings/src/components/editProfileModal.component.ts index 3a17ce0f..f1b64f6a 100644 --- a/tabby-settings/src/components/editProfileModal.component.ts +++ b/tabby-settings/src/components/editProfileModal.component.ts @@ -99,7 +99,7 @@ export class EditProfileModalComponent

{ id: uuidv4(), name: this.profileGroup, } - this.profilesService.writeProfileGroup(newGroup, false) + this.profilesService.newProfileGroup(newGroup, false, false) this.profileGroup = newGroup } this.profile.group = this.profileGroup.id diff --git a/tabby-settings/src/components/profilesSettingsTab.component.ts b/tabby-settings/src/components/profilesSettingsTab.component.ts index b8b060cb..ea71c54f 100644 --- a/tabby-settings/src/components/profilesSettingsTab.component.ts +++ b/tabby-settings/src/components/profilesSettingsTab.component.ts @@ -1,6 +1,4 @@ import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker' -import { v4 as uuidv4 } from 'uuid' -import slugify from 'slugify' import deepClone from 'clone-deep' import { Component, Inject } from '@angular/core' import { NgbModal } from '@ng-bootstrap/ng-bootstrap' @@ -16,7 +14,6 @@ _('Ungrouped') styleUrls: ['./profilesSettingsTab.component.scss'], }) export class ProfilesSettingsTabComponent extends BaseComponent { - profiles: PartialProfile[] = [] builtinProfiles: PartialProfile[] = [] templateProfiles: PartialProfile[] = [] profileGroups: PartialProfileGroup[] @@ -39,7 +36,7 @@ export class ProfilesSettingsTabComponent extends BaseComponent { async ngOnInit (): Promise { this.refresh() - this.builtinProfiles = (await this.profilesService.getProfiles()).filter(x => x.isBuiltin) + this.builtinProfiles = (await this.profilesService.getProfiles(true, false)).filter(x => x.isBuiltin) this.templateProfiles = this.builtinProfiles.filter(x => x.isTemplate) this.builtinProfiles = this.builtinProfiles.filter(x => !x.isTemplate) this.refresh() @@ -52,7 +49,7 @@ export class ProfilesSettingsTabComponent extends BaseComponent { async newProfile (base?: PartialProfile): Promise { if (!base) { - let profiles = [...this.templateProfiles, ...this.builtinProfiles, ...this.profiles] + let profiles = await this.profilesService.getProfiles() profiles = profiles.filter(x => !this.isProfileBlacklisted(x)) profiles.sort((a, b) => (a.weight ?? 0) - (b.weight ?? 0)) base = await this.selector.show( @@ -83,9 +80,7 @@ export class ProfilesSettingsTabComponent extends BaseComponent { const cfgProxy = this.profilesService.getConfigProxyForProfile(profile) profile.name = this.profilesService.providerForProfile(profile)?.getSuggestedName(cfgProxy) ?? this.translate.instant('{name} copy', base) } - profile.id = `${profile.type}:custom:${slugify(profile.name)}:${uuidv4()}` - this.config.store.profiles = [profile, ...this.config.store.profiles] - await this.config.save() + this.profilesService.newProfile(profile) } async editProfile (profile: PartialProfile): Promise { @@ -142,7 +137,6 @@ export class ProfilesSettingsTabComponent extends BaseComponent { } async refresh (): Promise { - this.profiles = this.config.store.profiles const groups = await this.profilesService.getProfileGroups(true, true) groups.sort((a, b) => a.name.localeCompare(b.name)) groups.sort((a, b) => (a.id === 'built-in' ? 1 : 0) - (b.id === 'built-in' ? 1 : 0))