mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-02 19:42:51 +08:00
61 lines
2.4 KiB
TypeScript
61 lines
2.4 KiB
TypeScript
import type { QuickServer } from "./quick-servers.js";
|
|
import { splitTeamSpeakTarget } from "./teamspeak-target.js";
|
|
|
|
export interface SkinPluginQuickServer {
|
|
id: string;
|
|
label: string;
|
|
monogram: string;
|
|
current: boolean;
|
|
kind: "favorite" | "recent";
|
|
favorite: boolean;
|
|
}
|
|
|
|
function randomOpaqueId(): string {
|
|
return globalThis.crypto?.randomUUID?.() ?? `quick-server-${Math.random().toString(36).slice(2)}`;
|
|
}
|
|
|
|
function containsConnectionTarget(label: string, address: string): boolean {
|
|
const candidates = new Set([address.trim()]);
|
|
try { candidates.add(splitTeamSpeakTarget(address).address.trim()); } catch { /* Persisted targets are validated on write. */ }
|
|
const normalizedLabel = label.toLocaleLowerCase();
|
|
return [...candidates].some((candidate) => candidate.length >= 3 && normalizedLabel.includes(candidate.toLocaleLowerCase()));
|
|
}
|
|
|
|
/** Projects local favorite/recent entries without exposing their connection targets. */
|
|
export function createSkinPluginQuickServerProjection(createId: () => string = randomOpaqueId) {
|
|
const opaqueIdByServerId = new Map<string, string>();
|
|
const targetByOpaqueId = new Map<string, QuickServer>();
|
|
|
|
return {
|
|
project(servers: readonly QuickServer[], isCurrent: (server: QuickServer) => boolean): SkinPluginQuickServer[] {
|
|
const activeServerIds = new Set(servers.map((server) => server.id));
|
|
for (const id of opaqueIdByServerId.keys()) if (!activeServerIds.has(id)) opaqueIdByServerId.delete(id);
|
|
targetByOpaqueId.clear();
|
|
|
|
return servers.map((server, index) => {
|
|
let opaqueId = opaqueIdByServerId.get(server.id);
|
|
if (!opaqueId) {
|
|
opaqueId = createId();
|
|
opaqueIdByServerId.set(server.id, opaqueId);
|
|
}
|
|
targetByOpaqueId.set(opaqueId, server);
|
|
const label = server.label.trim();
|
|
const safeLabel = !label || containsConnectionTarget(label, server.address)
|
|
? `${server.isFavorite ? "Favorite" : "Recent"} ${index + 1}`
|
|
: label;
|
|
return {
|
|
id: opaqueId,
|
|
label: safeLabel,
|
|
monogram: Array.from(safeLabel)[0] ?? "?",
|
|
current: isCurrent(server),
|
|
kind: server.isFavorite ? "favorite" : "recent",
|
|
favorite: server.isFavorite,
|
|
};
|
|
});
|
|
},
|
|
resolve(opaqueId: string): QuickServer | undefined {
|
|
return targetByOpaqueId.get(opaqueId);
|
|
},
|
|
};
|
|
}
|