Skip to content

Commit

Permalink
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 13, 2024
1 parent af7272a commit 7879f70
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 @@ -145,6 +145,10 @@ body {
color: rgb(108 133 146 / 100%);
}

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

.tab {
position: relative;
margin: 2px 0;
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 @@ -28,6 +28,7 @@ import {initializeTray} from "./tray.js";
import {ipcRenderer} from "./typed-ipc-renderer.js";
import * as DomainUtil from "./utils/domain-util.js";
import ReconnectUtil from "./utils/reconnect-util.js";
import Sortable from "sortablejs";

Sentry.init({});

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: Sortable | 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 Sortable(this.$tabsContainer, {
animation: 150,
onEnd: (event: Sortable.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) {
let 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
domains.forEach((domain, index) => {
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 7879f70

Please sign in to comment.