Skip to content

Commit

Permalink
wip ref(core): ProfileGroup avoid direct config interraction outside …
Browse files Browse the repository at this point in the history
…of profiles.services
  • Loading branch information
Clem-Fern committed Jul 23, 2023
1 parent 1903ec5 commit 8e9156e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
50 changes: 46 additions & 4 deletions tabby-core/src/services/profiles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AppService } from './app.service'
import { configMerge, ConfigProxy, ConfigService } from './config.service'
import { NotificationsService } from './notifications.service'
import { SelectorService } from './selector.service'
import deepClone from 'clone-deep'

@Injectable({ providedIn: 'root' })
export class ProfilesService {
Expand Down Expand Up @@ -74,7 +75,7 @@ export class ProfilesService {
...this.config.store.profiles ?? [],
...list,
]
const sortKey = p => `${p.group ?? ''} / ${p.name}`
const sortKey = p => `${this.resolveProfileGroupName(p.group ?? '')} / ${p.name}`
list.sort((a, b) => sortKey(a).localeCompare(sortKey(b)))
list.sort((a, b) => (a.isBuiltin ? 1 : 0) - (b.isBuiltin ? 1 : 0))
return list
Expand All @@ -96,8 +97,7 @@ export class ProfilesService {
const freeInputEquivalent = provider?.intoQuickConnectString(fullProfile) ?? undefined
return {
...profile,
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
group: profile.group || '',
group: this.resolveProfileGroupName(profile.group ?? ''),
freeInputEquivalent,
description: provider?.getDescription(fullProfile),
}
Expand Down Expand Up @@ -280,7 +280,7 @@ export class ProfilesService {
}

const profileGroupCollapsed = JSON.parse(window.localStorage.profileGroupCollapsed ?? '{}')
let groups: PartialProfileGroup<ProfileGroup>[] = this.config.store.groups ?? []
let groups: PartialProfileGroup<ProfileGroup>[] = deepClone(this.config.store.groups ?? [])
groups = groups.map(x => {
x.editable = true
x.collapsed = profileGroupCollapsed[x.id] ?? false
Expand Down Expand Up @@ -322,6 +322,48 @@ export class ProfilesService {
return groups
}

/**
* Write a ProfileGroup in config
* arg: saveConfig (default: true) -> invoke after the ProfileGroup was updated
*/
async writeProfileGroup (group: PartialProfileGroup<ProfileGroup>, saveConfig = true): Promise<void> {
this.deleteProfileGroup(group, false)

delete group.profiles
delete group.editable
delete group.collapsed

this.config.store.groups.push(group)
if (saveConfig) {
return this.config.save()
}
}

/**
* Delete a ProfileGroup from config
* arg: saveConfig (default: true) -> invoke after the ProfileGroup was deleted
*/
async deleteProfileGroup (group: PartialProfileGroup<ProfileGroup>, saveConfig = true, deleteProfiles = true): Promise<void> {
this.config.store.groups = this.config.store.groups.filter(g => g.id !== group.id)
if (deleteProfiles) {
this.config.store.profiles = this.config.store.profiles.filter(x => x.group !== group.id)
} else {
for (const profile of this.config.store.profiles.filter(x => x.group === group.id)) {
delete profile.group
}
}
if (saveConfig) {
return this.config.save()
}
}

/**
* Resolve and return ProfileGroup from ProfileGroup ID
*/
resolveProfileGroupName (groupId: string): string {
return this.config.store.groups.find(g => g.id === groupId)?.name ?? ''
}

/**
* Save ProfileGroup collapse state in localStorage
*/
Expand Down
6 changes: 3 additions & 3 deletions tabby-settings/src/components/editProfileModal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class EditProfileModalComponent<P extends Profile> {
private modalInstance: NgbActiveModal,
) {
if (!this.defaultsMode) {
this.profilesService.getProfileGroups().then(groups => {
this.profilesService.getProfileGroups().then(groups => {
this.groups = groups
this.profileGroup = groups.find(g => g.id === this.profile.group)
})
Expand Down Expand Up @@ -97,9 +97,9 @@ export class EditProfileModalComponent<P extends Profile> {
if (typeof this.profileGroup === 'string') {
const newGroup: PartialProfileGroup<ProfileGroup> = {
id: uuidv4(),
name: this.profileGroup
name: this.profileGroup,
}
this.groups.push(newGroup)
this.profilesService.writeProfileGroup(newGroup, false)
this.profileGroup = newGroup
}
this.profile.group = this.profileGroup.id
Expand Down
16 changes: 7 additions & 9 deletions tabby-settings/src/components/profilesSettingsTab.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class ProfilesSettingsTabComponent extends BaseComponent {
const result = await modal.result
if (result) {
group.name = result.value
await this.config.save()
await this.profilesService.writeProfileGroup(group)
}
}

Expand All @@ -184,7 +184,8 @@ export class ProfilesSettingsTabComponent extends BaseComponent {
cancelId: 1,
},
)).response === 0) {
if ((await this.platform.showMessageBox(
let deleteProfiles = false
if ((group.profiles?.length ?? 0) > 0 && (await this.platform.showMessageBox(
{
type: 'warning',
message: this.translate.instant('Delete the group\'s profiles?'),
Expand All @@ -195,14 +196,11 @@ export class ProfilesSettingsTabComponent extends BaseComponent {
defaultId: 0,
cancelId: 0,
},
)).response === 0) {
for (const profile of this.profiles.filter(x => x.group === group.id)) {
delete profile.group
}
} else {
this.config.store.profiles = this.config.store.profiles.filter(x => x.group !== group.id)
)).response !== 0) {
deleteProfiles = true
}
await this.config.save()

await this.profilesService.deleteProfileGroup(group, true, deleteProfiles)
}
}

Expand Down

0 comments on commit 8e9156e

Please sign in to comment.