Skip to content

Commit d4f1ffa

Browse files
committed
refactor: standardize variable names and improve null checks across user and app structures
1 parent a5ee36d commit d4f1ffa

File tree

7 files changed

+48
-43
lines changed

7 files changed

+48
-43
lines changed

src/@types/api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface RESTGetApiVscode extends RESTApiBaseResult {
77

88
export interface ApiVscodeUser {
99
apps: string[]
10+
avatar: string | null
1011
appsStatus: ApiVscodeApp[]
1112
appsTeam: string[]
1213
customdomains: string[]
@@ -16,7 +17,7 @@ export interface ApiVscodeUser {
1617
subdomains: string[]
1718
totalRamMb: number
1819
userID: string
19-
userName: string
20+
username: string | null
2021
}
2122

2223
export interface ApiVscodeApp extends BaseApiApp {

src/services/discloud/socket/actions/upload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export async function socketUpload(task: TaskData, buffer: Buffer, dConfig: Disc
5454
ramKilled: false,
5555
syncGit: null,
5656
...data.app,
57-
} as any;
57+
};
5858

5959
extension.appTree.addRawApp(app); // TODO: fix ApiUploadApp
6060
}

src/structures/AppTreeItem.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ export default class AppTreeItem extends BaseTreeItem<AppChildTreeItem> {
5353

5454
super._patch(data);
5555

56-
if (data.avatarURL !== undefined)
56+
if (data.avatarURL)
5757
this.data.avatarURL = data.avatarURL = data.avatarURL.replace(/\s+/g, "");
5858

59-
if (data.name !== undefined)
59+
if (data.name)
6060
this.label = this.type === AppType.bot ? `${data.name} (${this.appId})` : this.appId;
6161

6262
this.iconName = getIconName(this.data) ?? "off";
@@ -68,14 +68,14 @@ export default class AppTreeItem extends BaseTreeItem<AppChildTreeItem> {
6868

6969
switch (showAvatar) {
7070
case "always": {
71-
if (this.data.avatarURL !== undefined)
71+
if (this.data.avatarURL)
7272
this.iconPath = Uri.parse(this.data.avatarURL);
7373

7474
break;
7575
}
7676

7777
case "when.online": {
78-
if (this.online && this.data.avatarURL !== undefined)
78+
if (this.online && this.data.avatarURL)
7979
this.iconPath = Uri.parse(this.data.avatarURL);
8080

8181
break;
@@ -84,12 +84,12 @@ export default class AppTreeItem extends BaseTreeItem<AppChildTreeItem> {
8484

8585
this.tooltip = t(`app.status.${this.iconName}`) + " - " + this.label;
8686

87-
if (data.memory !== undefined) {
87+
if (data.memory) {
8888
const matched = data.memory.match(/[\d.]+/g) ?? [];
8989
data.memoryUsage = calculatePercentage(matched[0]!, matched[1]);
9090
}
9191

92-
if (data.startedAt !== undefined)
92+
if (data.startedAt)
9393
data.startedAtTimestamp = new Date(data.startedAt).valueOf();
9494

9595
this._addChild("status", {
@@ -98,35 +98,35 @@ export default class AppTreeItem extends BaseTreeItem<AppChildTreeItem> {
9898
iconName: "container",
9999
});
100100

101-
if (data.memory !== undefined)
101+
if (data.memory)
102102
this._addChild("memory", {
103103
label: data.memory,
104104
description: t("label.ram"),
105105
iconName: "ram",
106106
});
107107

108-
if (data.cpu !== undefined)
108+
if (data.cpu)
109109
this._addChild("cpu", {
110110
label: data.cpu,
111111
description: t("label.cpu"),
112112
iconName: "cpu",
113113
});
114114

115-
if (data.ssd !== undefined)
115+
if (data.ssd)
116116
this._addChild("ssd", {
117117
label: data.ssd,
118118
description: t("label.ssd"),
119119
iconName: "ssd",
120120
});
121121

122-
if (data.netIO !== undefined)
122+
if (data.netIO)
123123
this._addChild("netIO", {
124124
label: `⬇${data.netIO.down}${data.netIO.up}`,
125125
description: t("network"),
126126
iconName: "network",
127127
});
128128

129-
if (data.last_restart !== undefined)
129+
if (data.last_restart)
130130
this._addChild("last_restart", {
131131
label: data.last_restart,
132132
description: t("last.restart"),

src/structures/BaseStatusBarItem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default abstract class BaseStatusBarItem implements StatusBarItem {
88
constructor(readonly context: ExtensionContext, data: Partial<StatusBarItemOptions>) {
99
context.subscriptions.push(this);
1010

11-
const options = data.id !== undefined ? [data.id, data.alignment, data.priority] : [data.alignment, data.priority];
11+
const options = data.id ? [data.id, data.alignment, data.priority] : [data.alignment, data.priority];
1212
this.data = window.createStatusBarItem(...options as CreateStatusBarItemOptions);
1313
this.originalData = Object.assign(Object.create(this.data), this.data);
1414
this.set(data);

src/structures/TeamAppTreeItem.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,20 @@ export default class TeamAppTreeItem extends BaseTreeItem<TeamAppChildTreeItem>
5757

5858
super._patch(data);
5959

60-
if (data.name !== undefined)
60+
if (data.name)
6161
this.label = this.type === AppType.bot ? `${data.name} (${this.appId})` : this.appId;
6262

6363
this.iconName = getIconName(this.data) ?? "off";
6464
this.iconPath = getIconPath(this.iconName);
6565

6666
this.tooltip = t(`app.status.${this.iconName}`) + " - " + this.label;
6767

68-
if (data.memory !== undefined) {
68+
if (data.memory) {
6969
const matched = data.memory.match(/[\d.]+/g) ?? [];
7070
data.memoryUsage = calculatePercentage(matched[0]!, matched[1]);
7171
}
7272

73-
if (data.startedAt !== undefined)
73+
if (data.startedAt)
7474
data.startedAtTimestamp = new Date(data.startedAt).valueOf();
7575

7676
if (typeof this.online === "boolean")
@@ -81,41 +81,41 @@ export default class TeamAppTreeItem extends BaseTreeItem<TeamAppChildTreeItem>
8181
appId: this.appId,
8282
});
8383

84-
if (data.memory !== undefined)
84+
if (data.memory)
8585
this._addChild("memory", {
86-
label: data.memory!,
86+
label: data.memory,
8787
description: t("label.ram"),
8888
iconName: "ram",
8989
appId: this.appId,
9090
});
9191

92-
if (data.cpu !== undefined)
92+
if (data.cpu)
9393
this._addChild("cpu", {
94-
label: data.cpu!,
94+
label: data.cpu,
9595
description: t("label.cpu"),
9696
iconName: "cpu",
9797
appId: this.appId,
9898
});
9999

100-
if (data.ssd !== undefined)
100+
if (data.ssd)
101101
this._addChild("ssd", {
102-
label: data.ssd!,
102+
label: data.ssd,
103103
description: t("label.ssd"),
104104
iconName: "ssd",
105105
appId: this.appId,
106106
});
107107

108-
if (data.netIO !== undefined)
108+
if (data.netIO)
109109
this._addChild("netIO", {
110110
label: `⬇${data.netIO.down}${data.netIO.up}`,
111111
description: t("network"),
112112
iconName: "network",
113113
appId: this.appId,
114114
});
115115

116-
if (data.last_restart !== undefined)
116+
if (data.last_restart)
117117
this._addChild("last_restart", {
118-
label: data.last_restart!,
118+
label: data.last_restart,
119119
description: t("last.restart"),
120120
iconName: "uptime",
121121
appId: this.appId,

src/structures/UserTreeItem.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { t } from "@vscode/l10n";
2-
import { TreeItemCollapsibleState } from "vscode";
2+
import { TreeItemCollapsibleState, Uri } from "vscode";
33
import { type ApiVscodeUser, type UserTreeItemData } from "../@types";
44
import BaseTreeItem from "./BaseTreeItem";
55
import UserChildTreeItem from "./UserChildTreeItem";
@@ -21,27 +21,30 @@ export default class UserTreeItem extends BaseTreeItem<UserChildTreeItem> {
2121
protected _patch(data: Partial<UserTreeItemData & ApiVscodeUser>): this {
2222
if (!data) return this;
2323

24+
if (data.avatar)
25+
this.iconPath = Uri.parse(data.avatar);
26+
2427
super._patch(data);
2528

26-
if (data.userName !== undefined)
27-
this.label = data.userName + ` (${this.userID})`;
29+
if (data.username)
30+
this.label = data.username + ` (${this.userID})`;
2831

2932
if (data.children instanceof Map) {
3033
for (const [id, child] of data.children) {
3134
this.children.set(id, child);
3235
}
3336
}
3437

35-
if (data.ramUsedMb !== undefined && data.totalRamMb !== undefined)
38+
if (typeof data.ramUsedMb === "number" && typeof data.totalRamMb === "number")
3639
this._addChild("ram", {
3740
label: `${data.ramUsedMb}/${data.totalRamMb}`,
3841
description: t("label.available.ram"),
3942
userID: this.userID,
4043
});
4144

42-
if (data.plan !== undefined)
45+
if (data.plan)
4346
this._addChild("plan", {
44-
label: data.plan!,
47+
label: data.plan,
4548
description: t("plan"),
4649
userID: this.userID,
4750
});
@@ -55,37 +58,37 @@ export default class UserTreeItem extends BaseTreeItem<UserChildTreeItem> {
5558
userID: this.userID,
5659
});
5760

58-
if (data.locale !== undefined)
61+
if (data.locale)
5962
this._addChild("locale", {
60-
label: data.locale!,
63+
label: data.locale,
6164
description: t("locale"),
6265
userID: this.userID,
6366
});
6467

65-
if (data.apps !== undefined)
68+
if (data.apps)
6669
this._addChild("apps", {
67-
label: `${data.apps?.length ?? 0}`,
70+
label: `${data.apps.length}`,
6871
description: t("label.apps.amount"),
6972
userID: this.userID,
7073
});
7174

72-
if (data.appsTeam !== undefined)
75+
if (data.appsTeam)
7376
this._addChild("team", {
74-
label: `${data.appsTeam?.length ?? 0}`,
77+
label: `${data.appsTeam.length}`,
7578
description: t("label.team.apps.amount"),
7679
userID: this.userID,
7780
});
7881

79-
if (data.customdomains !== undefined)
82+
if (data.customdomains)
8083
this._addChild("domains", {
81-
label: `${data.customdomains?.length ?? 0}`,
84+
label: `${data.customdomains.length}`,
8285
description: t("label.domains.amount"),
8386
userID: this.userID,
8487
});
8588

86-
if (data.subdomains !== undefined)
89+
if (data.subdomains)
8790
this._addChild("subdomains", {
88-
label: `${data.subdomains?.length ?? 0}`,
91+
label: `${data.subdomains.length}`,
8992
description: t("label.subdomains.amount"),
9093
userID: this.userID,
9194
});

src/structures/VSUser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ export default class VSUser implements ApiVscodeUser {
88
readonly appsTeam: string[] = [];
99
readonly customdomains: string[] = [];
1010
readonly subdomains: string[] = [];
11+
declare avatar: string | null;
1112
declare locale: string;
1213
declare readonly plan: string;
1314
declare readonly planDataEnd: string;
1415
declare readonly ramUsedMb: number;
1516
declare readonly totalRamMb: number;
1617
declare readonly userID: string;
17-
declare readonly userName: string;
18+
declare readonly username: string;
1819

1920
async fetch(isVS?: boolean) {
2021
const method = isVS ? "queueGet" : "get";

0 commit comments

Comments
 (0)