From c1e03ed5321f2a0add06a7051008e6deffdb038c Mon Sep 17 00:00:00 2001 From: Clem Fern Date: Sat, 22 Jul 2023 21:36:40 +0200 Subject: [PATCH] wip ref(core/profiles.service): move out group managment from settings --- tabby-core/src/api/index.ts | 2 +- tabby-core/src/api/profileProvider.ts | 16 ++++++ tabby-core/src/services/config.service.ts | 32 +++++++++++ tabby-core/src/services/profiles.service.ts | 62 ++++++++++++++++++++- 4 files changed, 110 insertions(+), 2 deletions(-) diff --git a/tabby-core/src/api/index.ts b/tabby-core/src/api/index.ts index 0384396d5c..00e915d50e 100644 --- a/tabby-core/src/api/index.ts +++ b/tabby-core/src/api/index.ts @@ -16,7 +16,7 @@ export { BootstrapData, PluginInfo, BOOTSTRAP_DATA } from './mainProcess' export { HostWindowService } from './hostWindow' export { HostAppService, Platform } from './hostApp' export { FileProvider } from './fileProvider' -export { ProfileProvider, Profile, PartialProfile, ProfileSettingsComponent } from './profileProvider' +export { ProfileProvider, Profile, PartialProfile, ProfileSettingsComponent, ProfileGroup, PartialProfileGroup} from './profileProvider' export { PromptModalComponent } from '../components/promptModal.component' export * from './commands' diff --git a/tabby-core/src/api/profileProvider.ts b/tabby-core/src/api/profileProvider.ts index a6e6bdd6c8..9b53bbc2c6 100644 --- a/tabby-core/src/api/profileProvider.ts +++ b/tabby-core/src/api/profileProvider.ts @@ -31,6 +31,22 @@ export type PartialProfile = Omit[] + defaults: any + editable: boolean + collapsed: boolean +} + +export type PartialProfileGroup = Omit, 'name'> & { + id: string + name: string +} + export interface ProfileSettingsComponent

{ profile: P save?: () => void diff --git a/tabby-core/src/services/config.service.ts b/tabby-core/src/services/config.service.ts index daa0d74627..206b3b46fa 100644 --- a/tabby-core/src/services/config.service.ts +++ b/tabby-core/src/services/config.service.ts @@ -10,6 +10,7 @@ import { PlatformService } from '../api/platform' import { HostAppService } from '../api/hostApp' import { Vault, VaultService } from './vault.service' import { serializeFunction } from '../utils' +import { PartialProfileGroup, ProfileGroup } from '../api/profileProvider' const deepmerge = require('deepmerge') // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types @@ -364,6 +365,37 @@ export class ConfigService { } config.version = 4 } + if (config.version < 5) { + const groups: PartialProfileGroup[] = [] + for (const p of config.profiles ?? []) { + if (!`${p.group}`.trim()) { + continue + } + + let group = groups.find(x => x.name === (p.group)) + if (!group) { + group = { + id: `${uuidv4()}`, + name: `${p.group}`, + } + groups.push(group) + } + p.group = group.id + } + + const profileGroupCollapsed = JSON.parse(window.localStorage.profileGroupCollapsed ?? '{}') + for (const g of groups) { + if (profileGroupCollapsed[g.name]) { + const collapsed = profileGroupCollapsed[g.name] + delete profileGroupCollapsed[g.name] + profileGroupCollapsed[g.id] = collapsed + } + } + window.localStorage.profileGroupCollapsed = JSON.stringify(profileGroupCollapsed) + + config.groups = groups + config.version = 5 + } } private async maybeDecryptConfig (store) { diff --git a/tabby-core/src/services/profiles.service.ts b/tabby-core/src/services/profiles.service.ts index c4bafb2ed3..a5cff178d4 100644 --- a/tabby-core/src/services/profiles.service.ts +++ b/tabby-core/src/services/profiles.service.ts @@ -2,7 +2,7 @@ import { Injectable, Inject } from '@angular/core' import { TranslateService } from '@ngx-translate/core' import { NewTabParameters } from './tabs.service' import { BaseTabComponent } from '../components/baseTab.component' -import { PartialProfile, Profile, ProfileProvider } from '../api/profileProvider' +import { PartialProfile, PartialProfileGroup, Profile, ProfileGroup, ProfileProvider } from '../api/profileProvider' import { SelectorOption } from '../api/selector' import { AppService } from './app.service' import { configMerge, ConfigProxy, ConfigService } from './config.service' @@ -80,6 +80,8 @@ export class ProfilesService { return list } + + providerForProfile (profile: PartialProfile): ProfileProvider|null { const provider = this.profileProviders.find(x => x.id === profile.type) ?? null return provider as unknown as ProfileProvider|null @@ -263,4 +265,62 @@ export class ProfilesService { ] } + /* + * Methods used to interract with ProfileGroup + */ + + /** + * Return an Array of the existing ProfileGroups + * arg: includeProfiles (default: false) -> if false, does not fill up the profiles field of ProfileGroup + * arg: includeNonUserGroup (default: false) -> if false, does not add built-in and ungrouped groups + */ + async getProfileGroups (includeProfiles = false, includeNonUserGroup = false): Promise[]> { + let profiles: PartialProfile[] = [] + if (includeProfiles) { + profiles = await this.getProfiles() + } + + const profileGroupCollapsed = JSON.parse(window.localStorage.profileGroupCollapsed ?? '{}') + let groups: PartialProfileGroup[] = this.config.store.groups ?? [] + groups = groups.map(x => { + x.editable = true + x.collapsed = profileGroupCollapsed[x.id ?? ''] ?? false + + if (includeProfiles) { + x.profiles = profiles.filter(p => p.group === x.id) + profiles = profiles.filter(p => p.group !== x.id) + } + + return x + }) + + if (includeNonUserGroup) { + const builtIn: PartialProfileGroup = { + id: 'built-in', + name: this.translate.instant('Built-in'), + editable: false, + } + builtIn.collapsed = profileGroupCollapsed[builtIn.id] ?? false + + const ungrouped: PartialProfileGroup = { + id: 'ungrouped', + name: this.translate.instant('Ungrouped'), + editable: false, + } + ungrouped.collapsed = profileGroupCollapsed[ungrouped.id] ?? false + + if (includeProfiles) { + builtIn.profiles = profiles.filter(p => p.group === builtIn.id) + profiles = profiles.filter(p => p.group !== builtIn.id) + + ungrouped.profiles = profiles + } + + groups.push(builtIn) + groups.push(ungrouped) + } + + return groups + } + }