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

Left sidebar: Added ordering feature for server tabs #1359

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions app/renderer/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ webview.focus {
left: -5px;
}

.sortable-chosen .server-tooltip {
display: none;
}

#collapse-button {
bottom: 30px;
left: 20px;
Expand Down
19 changes: 18 additions & 1 deletion app/renderer/js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import url from "node:url";
import {Menu, app, dialog, session} from "@electron/remote";
import * as remote from "@electron/remote";
import * as Sentry from "@sentry/electron/renderer";
import SortableJS from "sortablejs";

import type {Config} from "../../common/config-util.js";
import * as ConfigUtil from "../../common/config-util.js";
Expand Down Expand Up @@ -57,7 +58,7 @@ const dingSound = new Audio(

export class ServerManagerView {
$addServerButton: HTMLButtonElement;
$tabsContainer: Element;
$tabsContainer: HTMLElement;
$reloadButton: HTMLButtonElement;
$loadingIndicator: HTMLButtonElement;
$settingsButton: HTMLButtonElement;
Expand All @@ -81,6 +82,7 @@ export class ServerManagerView {
tabIndex: number;
presetOrgs: string[];
preferenceView?: PreferenceView;
sortableSidebar: SortableJS | null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sortableSidebar: SortableJS | null;
sortableSidebar?: SortableJS;

to avoid explicitly assigning null.

constructor() {
this.$addServerButton = document.querySelector("#add-tab")!;
this.$tabsContainer = document.querySelector("#tabs-container")!;
Expand Down Expand Up @@ -123,6 +125,7 @@ export class ServerManagerView {
this.presetOrgs = [];
this.functionalTabs = new Map();
this.tabIndex = 0;
this.sortableSidebar = null;
}

async init(): Promise<void> {
Expand Down Expand Up @@ -235,6 +238,20 @@ export class ServerManagerView {
initSidebar(): void {
const showSidebar = ConfigUtil.getConfigItem("showSidebar", true);
this.toggleSidebar(showSidebar);
this.sortableSidebar = new SortableJS(this.$tabsContainer, {
animation: 150,
onEnd: (event: SortableJS.SortableEvent) => {
// Update the domain order in the database
DomainUtil.updateDomainOrder(event.oldIndex ?? 0, event.newIndex ?? 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected data should be rejected and ignored, not silently replaced with fake values like 0.


// Update the current active tab index
this.activeTabIndex = event.newIndex ?? 0;
ConfigUtil.setConfigItem("lastActiveTab", event.newIndex ?? 0);

// Reload the app to give the tabs their new indexes
ipcRenderer.send("reload-full-app");
},
});
}

// Remove the stale UA string from the disk if the app is not freshly
Expand Down
14 changes: 14 additions & 0 deletions app/renderer/js/utils/domain-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ export function updateDomain(index: number, server: ServerConf): void {
db.push(`/domains[${index}]`, server, true);
}

export function updateDomainOrder(oldIndex: number, newIndex: number) {
const domains = serverConfSchema
.array()
.parse(db.getObject<unknown>("/domains"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use getDomains()?


const [movedDomain] = domains.splice(oldIndex, 1);
domains.splice(newIndex, 0, movedDomain);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the database is out of sync (e.g. it has been manually edited), it’s possible for oldIndex and newIndex to be out of range. We should check for that.


// Update each domain in the database with its new order
for (const [index, domain] of domains.entries()) {
updateDomain(index, domain);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do this in a single db.push call, not one call per domain.

}

export async function addDomain(server: {
url: string;
alias: string;
Expand Down
14 changes: 13 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@
"InstantMessaging"
],
"dependencies": {
"gatemaker": "https://github.com/andersk/gatemaker/archive/d31890ae1cb293faabcb1e4e465c673458f6eed2.tar.gz"
"@types/sortablejs": "^1.15.8",
"gatemaker": "https://github.com/andersk/gatemaker/archive/d31890ae1cb293faabcb1e4e465c673458f6eed2.tar.gz",
"sortablejs": "^1.15.2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sortablejs and @types/sortablejs should go in devDependencies as they are bundled by Vite. (gatemaker is a weird exception due to the native library it uses on macOS.)

},
"devDependencies": {
"@electron/remote": "^2.0.8",
Expand Down