Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort cluster by name is the settings #83

Merged
merged 1 commit into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ All notable changes to Kafka extension will be documented in this file.
- Newly created topic or cluster is automatically selected in the Kafka Explorer. See [#61](https://github.com/jlandersen/vscode-kafka/issues/61).
- Internal topics are now hidden by default. See [#29](https://github.com/jlandersen/vscode-kafka/issues/29) and [#74](https://github.com/jlandersen/vscode-kafka/pull/74).
- Elements are now sorted alphabetically in the Kafka explorer. See [#63](https://github.com/jlandersen/vscode-kafka/issues/63).
- Clusters are now sorted in the cluster selection wizard. See [#83](https://github.com/jlandersen/vscode-kafka/issues/83).

## [0.10.0] - 2021-01-02
### Added
Expand Down
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();