Skip to content

Commit

Permalink
Sort cluster by name is the settings
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jan 18, 2021
1 parent a0257d0 commit b0dd97d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
11 changes: 2 additions & 9 deletions src/explorer/models/kafka.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Disposable, TreeItemCollapsibleState } from "vscode";
import { ClientAccessor, Cluster } from "../../client";
import { ClientAccessor } from "../../client";
import { ClusterSettings } from "../../settings";
import { ClusterItem, NoClusterItem } from "./cluster";
import { NodeBase } from "./nodeBase";
Expand All @@ -16,8 +16,7 @@ export class KafkaModel extends NodeBase implements Disposable {
}

public async computeChildren(): Promise<NodeBase[]> {
const clusters = this.clusterSettings.getAll()
.sort(this.sortByNameAscending);
const clusters = this.clusterSettings.getAll();
if (clusters.length === 0) {
return [new NoClusterItem(this)];
}
Expand All @@ -26,12 +25,6 @@ export class KafkaModel extends NodeBase implements Disposable {
});
}

private sortByNameAscending(a: Cluster, b: Cluster): -1 | 0 | 1 {
if (a.name.toLowerCase() < b.name.toLowerCase()) { return -1; }
if (a.name.toLowerCase() > b.name.toLowerCase()) { return 1; }
return 0;
}

public dispose(): void {
this.children?.forEach(child => (<ClusterItem>child).dispose());
}
Expand Down
15 changes: 11 additions & 4 deletions src/settings/clusters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface ClusterSettings {
onDidChangeSelected: vscode.Event<SelectedClusterChangedEvent>;

/**
* Gets the full cluster collection..
* Gets the full cluster collection sorted by cluster name.
*/
getAll(): Cluster[];

Expand Down Expand Up @@ -78,7 +78,14 @@ class MementoClusterSettings implements ClusterSettings {

getAll(): Cluster[] {
const state = this.storage.get<ClusterStoreType>(this.clusterCollectionStorageKey, {});
return Object.values(state);
return Object.values(state)
.sort(this.sortByNameAscending);
}

private sortByNameAscending(a: Cluster, b: Cluster): -1 | 0 | 1 {
if (a.name.toLowerCase() < b.name.toLowerCase()) { return -1; }
if (a.name.toLowerCase() > b.name.toLowerCase()) { return 1; }
return 0;
}

get(id: string): Cluster | undefined {
Expand Down Expand Up @@ -112,7 +119,7 @@ class MementoClusterSettings implements ClusterSettings {
}

private setSelectedClusterIfNeeded(): void {
if (this.selected !== undefined){
if (this.selected !== undefined) {
return;
}
const all = this.getAll();
Expand All @@ -122,4 +129,4 @@ class MementoClusterSettings implements ClusterSettings {
}
}

export const getClusterSettings = (): ClusterSettings => MementoClusterSettings.getInstance();
export const getClusterSettings = (): ClusterSettings => MementoClusterSettings.getInstance();

0 comments on commit b0dd97d

Please sign in to comment.