Create method to expose selected DbItem

This commit is contained in:
Nora
2022-12-02 15:22:14 +01:00
parent 994ebaacd0
commit ee056ce2b3
2 changed files with 43 additions and 1 deletions

View File

@@ -124,3 +124,35 @@ const SelectableDbItemKinds = [
DbItemKind.RemoteOwner,
DbItemKind.RemoteRepo,
];
export function getSelectedDbItem(dbItems: DbItem[]): DbItem | undefined {
for (const dbItem of dbItems) {
if (
dbItem.kind === DbItemKind.RootRemote ||
dbItem.kind === DbItemKind.RootLocal
) {
for (const child of dbItem.children) {
switch (child.kind) {
case DbItemKind.LocalList:
for (const database of child.databases) {
if (database.selected) {
return database;
}
}
break;
case DbItemKind.RemoteUserDefinedList:
for (const repo of child.repos) {
if (repo.selected) {
return repo;
}
}
break;
default:
if (child.selected) {
return child;
}
}
}
}
}
}

View File

@@ -2,7 +2,7 @@ import { App } from "../common/app";
import { AppEvent, AppEventEmitter } from "../common/events";
import { ValueResult } from "../common/value-result";
import { DbConfigStore } from "./config/db-config-store";
import { DbItem } from "./db-item";
import { DbItem, getSelectedDbItem } from "./db-item";
import { createLocalTree, createRemoteTree } from "./db-tree-creator";
export class DbManager {
@@ -18,6 +18,16 @@ export class DbManager {
});
}
public selectedDbItem(): DbItem | undefined {
const dbItems = this.getDbItems();
if (dbItems.isFailure) {
return undefined;
}
return getSelectedDbItem(dbItems.value);
}
public getDbItems(): ValueResult<DbItem[]> {
const configResult = this.dbConfigStore.getConfig();
if (configResult.isFailure) {