Files
zulip-desktop/app/renderer/js/components/server-tab.ts
Anders Kaseorg 26b226c7ae Use .ts extensions for imports.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2025-06-20 16:35:18 -07:00

94 lines
2.5 KiB
TypeScript

import process from "node:process";
import {type Html, html} from "../../../common/html.ts";
import {ipcRenderer} from "../typed-ipc-renderer.ts";
import {generateNodeFromHtml} from "./base.ts";
import Tab, {type TabProperties} from "./tab.ts";
import type WebView from "./webview.ts";
export type ServerTabProperties = {
webview: Promise<WebView>;
} & TabProperties;
export default class ServerTab extends Tab {
webview: Promise<WebView>;
$el: Element;
$name: Element;
$icon: HTMLImageElement;
$badge: Element;
constructor({webview, ...properties}: ServerTabProperties) {
super(properties);
this.webview = webview;
this.$el = generateNodeFromHtml(this.templateHtml());
this.properties.$root.append(this.$el);
this.registerListeners();
this.$name = this.$el.querySelector(".server-tooltip")!;
this.$icon = this.$el.querySelector(".server-icons")!;
this.$badge = this.$el.querySelector(".server-tab-badge")!;
}
override async activate(): Promise<void> {
await super.activate();
(await this.webview).load();
}
override async deactivate(): Promise<void> {
await super.deactivate();
(await this.webview).hide();
}
override async destroy(): Promise<void> {
await super.destroy();
(await this.webview).destroy();
}
templateHtml(): Html {
return html`
<div class="tab" data-tab-id="${this.properties.tabIndex}">
<div class="server-tooltip" style="display:none">
${this.properties.label}
</div>
<div class="server-tab-badge"></div>
<div class="server-tab">
<img class="server-icons" src="${this.properties.icon}" />
</div>
<div class="server-tab-shortcut">${this.generateShortcutText()}</div>
</div>
`;
}
setLabel(label: string): void {
this.properties.label = label;
this.$name.textContent = label;
}
setIcon(icon: string): void {
this.properties.icon = icon;
this.$icon.src = icon;
}
updateBadge(count: number): void {
this.$badge.textContent = count > 999 ? "1K+" : count.toString();
this.$badge.classList.toggle("active", count > 0);
}
generateShortcutText(): string {
// Only provide shortcuts for server [0..9]
if (this.properties.index >= 9) {
return "";
}
const shownIndex = this.properties.index + 1;
// Array index == Shown index - 1
ipcRenderer.send("switch-server-tab", shownIndex - 1);
return process.platform === "darwin"
? `${shownIndex}`
: `Ctrl+${shownIndex}`;
}
}