Skip to content

Commit

Permalink
wip ref(core/profiles.service): move out group managment from settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Clem-Fern committed Jul 22, 2023
1 parent 8a85fca commit c1e03ed
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tabby-core/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
16 changes: 16 additions & 0 deletions tabby-core/src/api/profileProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ export type PartialProfile<T extends Profile> = Omit<Omit<Omit<{
}
}

export interface ProfileGroup {
id: string
name: string
profiles: PartialProfile<Profile>[]
defaults: any
editable: boolean
collapsed: boolean
}

export type PartialProfileGroup<T extends ProfileGroup> = Omit<Omit<{
[K in keyof T]?: T[K]
}, 'id'>, 'name'> & {
id: string
name: string
}

export interface ProfileSettingsComponent<P extends Profile> {
profile: P
save?: () => void
Expand Down
32 changes: 32 additions & 0 deletions tabby-core/src/services/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -364,6 +365,37 @@ export class ConfigService {
}
config.version = 4
}
if (config.version < 5) {
const groups: PartialProfileGroup<ProfileGroup>[] = []
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) {
Expand Down
62 changes: 61 additions & 1 deletion tabby-core/src/services/profiles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -80,6 +80,8 @@ export class ProfilesService {
return list
}



providerForProfile <T extends Profile> (profile: PartialProfile<T>): ProfileProvider<T>|null {
const provider = this.profileProviders.find(x => x.id === profile.type) ?? null
return provider as unknown as ProfileProvider<T>|null
Expand Down Expand Up @@ -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<PartialProfileGroup<ProfileGroup>[]> {
let profiles: PartialProfile<Profile>[] = []
if (includeProfiles) {
profiles = await this.getProfiles()
}

const profileGroupCollapsed = JSON.parse(window.localStorage.profileGroupCollapsed ?? '{}')
let groups: PartialProfileGroup<ProfileGroup>[] = 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<ProfileGroup> = {
id: 'built-in',
name: this.translate.instant('Built-in'),
editable: false,
}
builtIn.collapsed = profileGroupCollapsed[builtIn.id] ?? false

const ungrouped: PartialProfileGroup<ProfileGroup> = {
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
}

}

0 comments on commit c1e03ed

Please sign in to comment.