Merge pull request #1923 from github/charisk/db-item-expansion-tidy

Some small refactorings around db item expansion
This commit is contained in:
Charis Kyriakou
2023-01-04 10:02:33 +00:00
committed by GitHub
6 changed files with 135 additions and 135 deletions

View File

@@ -87,37 +87,6 @@ export interface LocalDatabase {
storagePath: string;
}
export type ExpandedDbItem =
| RootLocalExpandedDbItem
| LocalUserDefinedListExpandedDbItem
| RootRemoteExpandedDbItem
| RemoteUserDefinedListExpandedDbItem;
export enum ExpandedDbItemKind {
RootLocal = "rootLocal",
LocalUserDefinedList = "localUserDefinedList",
RootRemote = "rootRemote",
RemoteUserDefinedList = "remoteUserDefinedList",
}
export interface RootLocalExpandedDbItem {
kind: ExpandedDbItemKind.RootLocal;
}
export interface LocalUserDefinedListExpandedDbItem {
kind: ExpandedDbItemKind.LocalUserDefinedList;
listName: string;
}
export interface RootRemoteExpandedDbItem {
kind: ExpandedDbItemKind.RootRemote;
}
export interface RemoteUserDefinedListExpandedDbItem {
kind: ExpandedDbItemKind.RemoteUserDefinedList;
listName: string;
}
export function cloneDbConfig(config: DbConfig): DbConfig {
return {
databases: {

View File

@@ -1,7 +1,37 @@
import { ExpandedDbItem, ExpandedDbItemKind } from "./config/db-config";
import { DbItem, DbItemKind } from "./db-item";
export function calculateNewExpandedState(
export type ExpandedDbItem =
| RootLocalExpandedDbItem
| LocalUserDefinedListExpandedDbItem
| RootRemoteExpandedDbItem
| RemoteUserDefinedListExpandedDbItem;
export enum ExpandedDbItemKind {
RootLocal = "rootLocal",
LocalUserDefinedList = "localUserDefinedList",
RootRemote = "rootRemote",
RemoteUserDefinedList = "remoteUserDefinedList",
}
export interface RootLocalExpandedDbItem {
kind: ExpandedDbItemKind.RootLocal;
}
export interface LocalUserDefinedListExpandedDbItem {
kind: ExpandedDbItemKind.LocalUserDefinedList;
listName: string;
}
export interface RootRemoteExpandedDbItem {
kind: ExpandedDbItemKind.RootRemote;
}
export interface RemoteUserDefinedListExpandedDbItem {
kind: ExpandedDbItemKind.RemoteUserDefinedList;
listName: string;
}
export function updateItemInExpandedState(
currentExpandedItems: ExpandedDbItem[],
dbItem: DbItem,
itemExpanded: boolean,

View File

@@ -1,10 +1,9 @@
import { App } from "../common/app";
import { AppEvent, AppEventEmitter } from "../common/events";
import { ValueResult } from "../common/value-result";
import { ExpandedDbItem } from "./config/db-config";
import { DbConfigStore } from "./config/db-config-store";
import { DbItem, DbListKind } from "./db-item";
import { calculateNewExpandedState } from "./db-item-expansion";
import { updateItemInExpandedState, ExpandedDbItem } from "./db-item-expansion";
import {
getSelectedDbItem,
mapDbItemToSelectedDbItem,
@@ -45,7 +44,7 @@ export class DbManager {
return ValueResult.fail(configResult.errors);
}
const expandedItems = this.getCurrentExpandedItems();
const expandedItems = this.getExpandedItems();
return ValueResult.ok([
createRemoteTree(configResult.value, expandedItems),
@@ -68,23 +67,15 @@ export class DbManager {
dbItem: DbItem,
itemExpanded: boolean,
): Promise<void> {
const configResult = this.dbConfigStore.getConfig();
if (configResult.isFailure) {
throw Error("Cannot update expanded state if config is not loaded");
}
const currentExpandedItems = this.getExpandedItems();
const currentExpandedItems = this.getCurrentExpandedItems();
const newExpandedItems = calculateNewExpandedState(
const newExpandedItems = updateItemInExpandedState(
currentExpandedItems,
dbItem,
itemExpanded,
);
await this.app.workspaceState.update(
DbManager.DB_EXPANDED_STATE_KEY,
newExpandedItems,
);
await this.setExpandedItems(newExpandedItems);
}
public async addNewRemoteRepo(
@@ -133,11 +124,18 @@ export class DbManager {
return this.dbConfigStore.doesRemoteDbExist(nwo, listName);
}
private getCurrentExpandedItems(): ExpandedDbItem[] {
private getExpandedItems(): ExpandedDbItem[] {
const items = this.app.workspaceState.get<ExpandedDbItem[]>(
DbManager.DB_EXPANDED_STATE_KEY,
);
return items || [];
}
private async setExpandedItems(items: ExpandedDbItem[]): Promise<void> {
await this.app.workspaceState.update(
DbManager.DB_EXPANDED_STATE_KEY,
items,
);
}
}

View File

@@ -1,7 +1,5 @@
import {
DbConfig,
ExpandedDbItem,
ExpandedDbItemKind,
LocalDatabase,
LocalList,
RemoteRepositoryList,
@@ -18,6 +16,7 @@ import {
RootLocalDbItem,
RootRemoteDbItem,
} from "./db-item";
import { ExpandedDbItem, ExpandedDbItemKind } from "./db-item-expansion";
export function createRemoteTree(
dbConfig: DbConfig,

View File

@@ -1,109 +1,111 @@
import {
ExpandedDbItem,
ExpandedDbItemKind,
} from "../../../src/databases/config/db-config";
import {
RemoteUserDefinedListDbItem,
RootRemoteDbItem,
} from "../../../src/databases/db-item";
import { calculateNewExpandedState } from "../../../src/databases/db-item-expansion";
import {
updateItemInExpandedState,
ExpandedDbItem,
ExpandedDbItemKind,
} from "../../../src/databases/db-item-expansion";
import {
createRemoteUserDefinedListDbItem,
createRootRemoteDbItem,
} from "../../factories/db-item-factories";
describe("db item expansion", () => {
it("should add an expanded item to an existing list", () => {
const currentExpandedItems: ExpandedDbItem[] = [
{
kind: ExpandedDbItemKind.RootRemote,
},
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list1",
},
];
describe("updateItemInExpandedState", () => {
it("should add an expanded item to an existing list", () => {
const currentExpandedItems: ExpandedDbItem[] = [
{
kind: ExpandedDbItemKind.RootRemote,
},
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list1",
},
];
const dbItem: RemoteUserDefinedListDbItem =
createRemoteUserDefinedListDbItem({
listName: "list2",
});
const dbItem: RemoteUserDefinedListDbItem =
createRemoteUserDefinedListDbItem({
listName: "list2",
});
const newExpandedItems = calculateNewExpandedState(
currentExpandedItems,
dbItem,
true,
);
const newExpandedItems = updateItemInExpandedState(
currentExpandedItems,
dbItem,
true,
);
expect(newExpandedItems).toEqual([
...currentExpandedItems,
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list2",
},
]);
});
expect(newExpandedItems).toEqual([
...currentExpandedItems,
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list2",
},
]);
});
it("should add an expanded item to an empty list", () => {
const dbItem: RemoteUserDefinedListDbItem =
createRemoteUserDefinedListDbItem({
listName: "list2",
});
it("should add an expanded item to an empty list", () => {
const dbItem: RemoteUserDefinedListDbItem =
createRemoteUserDefinedListDbItem({
listName: "list2",
});
const newExpandedItems = calculateNewExpandedState([], dbItem, true);
const newExpandedItems = updateItemInExpandedState([], dbItem, true);
expect(newExpandedItems).toEqual([
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list2",
},
]);
});
expect(newExpandedItems).toEqual([
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list2",
},
]);
});
it("should remove a collapsed item from a list", () => {
const currentExpandedItems: ExpandedDbItem[] = [
{
kind: ExpandedDbItemKind.RootRemote,
},
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list1",
},
];
it("should remove a collapsed item from a list", () => {
const currentExpandedItems: ExpandedDbItem[] = [
{
kind: ExpandedDbItemKind.RootRemote,
},
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list1",
},
];
const dbItem: RemoteUserDefinedListDbItem =
createRemoteUserDefinedListDbItem({
listName: "list1",
});
const dbItem: RemoteUserDefinedListDbItem =
createRemoteUserDefinedListDbItem({
listName: "list1",
});
const newExpandedItems = calculateNewExpandedState(
currentExpandedItems,
dbItem,
false,
);
const newExpandedItems = updateItemInExpandedState(
currentExpandedItems,
dbItem,
false,
);
expect(newExpandedItems).toEqual([
{
kind: ExpandedDbItemKind.RootRemote,
},
]);
});
expect(newExpandedItems).toEqual([
{
kind: ExpandedDbItemKind.RootRemote,
},
]);
});
it("should remove a collapsed item from a list that becomes empty", () => {
const currentExpandedItems: ExpandedDbItem[] = [
{
kind: ExpandedDbItemKind.RootRemote,
},
];
it("should remove a collapsed item from a list that becomes empty", () => {
const currentExpandedItems: ExpandedDbItem[] = [
{
kind: ExpandedDbItemKind.RootRemote,
},
];
const dbItem: RootRemoteDbItem = createRootRemoteDbItem();
const dbItem: RootRemoteDbItem = createRootRemoteDbItem();
const newExpandedItems = calculateNewExpandedState(
currentExpandedItems,
dbItem,
false,
);
const newExpandedItems = updateItemInExpandedState(
currentExpandedItems,
dbItem,
false,
);
expect(newExpandedItems).toEqual([]);
expect(newExpandedItems).toEqual([]);
});
});
});

View File

@@ -1,7 +1,5 @@
import {
DbConfig,
ExpandedDbItem,
ExpandedDbItemKind,
SelectedDbItemKind,
} from "../../../src/databases/config/db-config";
import {
@@ -10,6 +8,10 @@ import {
isRemoteRepoDbItem,
isRemoteUserDefinedListDbItem,
} from "../../../src/databases/db-item";
import {
ExpandedDbItem,
ExpandedDbItemKind,
} from "../../../src/databases/db-item-expansion";
import {
createLocalTree,
createRemoteTree,