Skip to content

Commit

Permalink
left sidebar: added ordering feature for server tabs, fixes zulip#548
Browse files Browse the repository at this point in the history
  • Loading branch information
Aitchessbee committed Mar 14, 2024
1 parent af7272a commit aedab17
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
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;
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);

// 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"));

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

// Update each domain in the database with its new order
for (const [index, domain] of domains.entries()) {
updateDomain(index, 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"
},
"devDependencies": {
"@electron/remote": "^2.0.8",
Expand Down

0 comments on commit aedab17

Please sign in to comment.