Skip to content

Commit e3452bd

Browse files
committed
Simplify if (…) classList.add(…) else classList.remove(…) anti-pattern.
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 0aab691 commit e3452bd

File tree

6 files changed

+13
-40
lines changed

6 files changed

+13
-40
lines changed

app/renderer/js/components/server-tab.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,8 @@ export default class ServerTab extends Tab {
5959
}
6060

6161
updateBadge(count: number): void {
62-
if (count > 0) {
63-
const formattedCount = count > 999 ? "1K+" : count.toString();
64-
this.$badge.textContent = formattedCount;
65-
this.$badge.classList.add("active");
66-
} else {
67-
this.$badge.classList.remove("active");
68-
}
62+
this.$badge.textContent = count > 999 ? "1K+" : count.toString();
63+
this.$badge.classList.toggle("active", count > 0);
6964
}
7065

7166
generateShortcutText(): string {

app/renderer/js/components/webview.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,7 @@ export default class WebView {
206206
}
207207

208208
// To show or hide the loading indicator in the the active tab
209-
if (this.loading) {
210-
this.$webviewsContainer.remove("loaded");
211-
} else {
212-
this.$webviewsContainer.add("loaded");
213-
}
209+
this.$webviewsContainer.toggle("loaded", !this.loading);
214210

215211
this.$el.classList.add("active");
216212
this.focus();
@@ -293,11 +289,7 @@ export default class WebView {
293289
const $backButton = document.querySelector(
294290
"#actions-container #back-action",
295291
)!;
296-
if (this.getWebContents().canGoBack()) {
297-
$backButton.classList.remove("disable");
298-
} else {
299-
$backButton.classList.add("disable");
300-
}
292+
$backButton.classList.toggle("disable", !this.getWebContents().canGoBack());
301293
}
302294

303295
forward(): void {

app/renderer/js/main.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -699,13 +699,8 @@ export class ServerManagerView {
699699
}
700700

701701
showLoading(loading: boolean): void {
702-
if (!loading) {
703-
this.$reloadButton.removeAttribute("style");
704-
this.$loadingIndicator.style.display = "none";
705-
} else if (loading) {
706-
this.$reloadButton.style.display = "none";
707-
this.$loadingIndicator.removeAttribute("style");
708-
}
702+
this.$reloadButton.classList.toggle("hidden", loading);
703+
this.$loadingIndicator.classList.toggle("hidden", !loading);
709704
}
710705

711706
async destroyTab(name: string, index: number): Promise<void> {
@@ -772,11 +767,7 @@ export class ServerManagerView {
772767
}
773768

774769
toggleSidebar(show: boolean): void {
775-
if (show) {
776-
this.$sidebar.classList.remove("sidebar-hide");
777-
} else {
778-
this.$sidebar.classList.add("sidebar-hide");
779-
}
770+
this.$sidebar.classList.toggle("sidebar-hide", !show);
780771
}
781772

782773
// Toggles the dnd button icon.

app/renderer/js/pages/preference/find-accounts.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ export function initFindAccounts(props: FindAccountsProps): void {
5858
});
5959

6060
$serverUrlField.addEventListener("input", () => {
61-
if ($serverUrlField.value) {
62-
$serverUrlField.classList.remove("invalid-input-value");
63-
} else {
64-
$serverUrlField.classList.add("invalid-input-value");
65-
}
61+
$serverUrlField.classList.toggle(
62+
"invalid-input-value",
63+
$serverUrlField.value === "",
64+
);
6665
});
6766
}

app/renderer/js/pages/preference/network-section.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,7 @@ export function initNetworkSection({$root}: NetworkSectionProps): void {
8383
});
8484

8585
function toggleManualProxySettings(option: boolean): void {
86-
if (option) {
87-
$manualProxyBlock.classList.remove("hidden");
88-
} else {
89-
$manualProxyBlock.classList.add("hidden");
90-
}
86+
$manualProxyBlock.classList.toggle("hidden", !option);
9187
}
9288

9389
function updateProxyOption(): void {

app/renderer/main.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<i class="material-icons md-48">notifications</i>
3131
<span id="dnd-tooltip" style="display: none">Do Not Disturb</span>
3232
</div>
33-
<div class="action-button" id="reload-action">
33+
<div class="action-button hidden" id="reload-action">
3434
<i class="material-icons md-48">refresh</i>
3535
<span id="reload-tooltip" style="display: none">Reload</span>
3636
</div>

0 commit comments

Comments
 (0)