fix(web): unify quick server history and favorites

This commit is contained in:
illusia committed 2026-10-09 13:42:56 +08:00
1 parent b6e4d349a2
commit 58559cf839
14 files changed
+398 -85

No files matched your search

+48
View File
@@ -0,0 +1,48 @@
import assert from "node:assert/strict";
import test from "node:test";
import { mergeQuickServers } from "./quick-servers.js";
test("quick servers put favorites first and merge matching recent connection details", () => {
const quickServers = mergeQuickServers(
[
{ id: "z", label: "Friends", address: "voice.example:9987", nickname: "Saved name" },
{ id: "a", label: "Games", address: "games.example:9987" },
],
[
{
id: "recent-z",
address: "VOICE.EXAMPLE:9987",
nickname: "Current name",
lastConnectedAt: 20,
lastChannelHint: { name: "Lounge" },
},
{ id: "recent-b", address: "community.example:9987", lastConnectedAt: 30 },
{ id: "recent-b-old", address: "COMMUNITY.example:9987", lastConnectedAt: 10 },
{ id: "recent-a", address: "games.example:9987", lastConnectedAt: 15 },
],
);
assert.deepEqual(quickServers.map(({ label, isFavorite }) => [label, isFavorite]), [
["Friends", true],
["Games", true],
["community.example:9987", false],
]);
assert.equal(quickServers.length, 3);
assert.equal(quickServers[0].nickname, "Current name");
assert.deepEqual(quickServers[0].lastChannelHint, { name: "Lounge" });
assert.equal(quickServers[2].lastConnectedAt, 30);
});
test("quick server addresses are trimmed, normalized, and empty targets are skipped", () => {
const quickServers = mergeQuickServers(
[{ id: "favorite", label: "Favorite", address: " Voice.Example:9987 " }],
[
{ id: "same", address: "voice.example:9987", lastConnectedAt: 2 },
{ id: "empty", address: " ", lastConnectedAt: 3 },
],
);
assert.equal(quickServers.length, 1);
assert.equal(quickServers[0].id, "voice.example:9987");
assert.equal(quickServers[0].address, "Voice.Example:9987");
});
+69
View File
@@ -0,0 +1,69 @@
import type { FavoriteServer, RecentServer } from "./local-persistence.js";
export interface QuickServer {
id: string;
address: string;
label: string;
nickname?: string;
identityId?: string;
lastChannelHint?: { id?: string; name?: string };
isFavorite: boolean;
lastConnectedAt?: number;
}
/**
* Builds the shared server picker used on the home page and in the voice room.
* Favorites stay first, while a recent entry for a favorite only enriches its
* connection details instead of rendering a duplicate row.
*/
export function mergeQuickServers(
favorites: readonly FavoriteServer[],
recentServers: readonly RecentServer[],
): QuickServer[] {
const recentByAddress = new Map<string, RecentServer>();
for (const recent of [...recentServers].sort((left, right) => right.lastConnectedAt - left.lastConnectedAt)) {
const key = serverKey(recent.address);
if (key && !recentByAddress.has(key)) recentByAddress.set(key, recent);
}
const result: QuickServer[] = [];
const seen = new Set<string>();
for (const favorite of [...favorites].sort((left, right) => left.label.localeCompare(right.label))) {
const key = serverKey(favorite.address);
if (!key || seen.has(key)) continue;
seen.add(key);
const recent = recentByAddress.get(key);
result.push({
id: key,
address: favorite.address.trim(),
label: favorite.label.trim() || favorite.address.trim(),
isFavorite: true,
...(recent?.nickname || favorite.nickname ? { nickname: recent?.nickname || favorite.nickname } : {}),
...(recent?.identityId || favorite.identityId ? { identityId: recent?.identityId || favorite.identityId } : {}),
...(recent?.lastChannelHint || favorite.lastChannelHint ? { lastChannelHint: recent?.lastChannelHint ?? favorite.lastChannelHint } : {}),
...(recent ? { lastConnectedAt: recent.lastConnectedAt } : {}),
});
}
for (const recent of [...recentServers].sort((left, right) => right.lastConnectedAt - left.lastConnectedAt)) {
const key = serverKey(recent.address);
if (!key || seen.has(key)) continue;
seen.add(key);
result.push({
id: key,
address: recent.address.trim(),
label: recent.address.trim(),
isFavorite: false,
...(recent.nickname ? { nickname: recent.nickname } : {}),
...(recent.identityId ? { identityId: recent.identityId } : {}),
...(recent.lastChannelHint ? { lastChannelHint: recent.lastChannelHint } : {}),
lastConnectedAt: recent.lastConnectedAt,
});
}
return result;
}
function serverKey(address: string): string {
return address.trim().toLocaleLowerCase();
}
+4
View File
@@ -154,6 +154,10 @@ test("the ILLUSIA visual-only example imports without replacing WebSpeak's base
assert.match(skin.css, /language\.option/);
assert.match(skin.css, /data-ws-skin-id="community\.illusia-voice"/);
assert.match(skin.css, /data-ws-part="voice\.favorite-servers\.rail"/);
assert.match(skin.css, /data-ws-part="home\.server-history\.item"/);
assert.match(skin.css, /data-ws-part="home\.server-history\.favorite-toggle"\]\[data-ws-state="saved"\]/);
assert.match(skin.css, /data-ws-part="voice\.favorite-servers\.favorite-toggle"\]\[data-ws-state="saved"\]/);
assert.match(skin.css, /data-ws-part="voice\.favorite-servers\.strip\.favorite-toggle"\]\[data-ws-state="saved"\]/);
assert.match(skin.css, /data-ws-part="voice\.favorite-servers\.strip\.server"\]\[data-ws-state="current"\]/);
assert.match(skin.css, /data-ws-part="voice\.favorite-servers\.dialog"\]\s*\{[^}]*background:\s*linear-gradient/s);
assert.match(skin.css, /data-ws-part="voice\.favorite-servers\.dialog\.input"\]:focus-visible\s*\{[^}]*box-shadow:/s);