M005: add local identity and preference persistence

This commit is contained in:
illusia committed 2026-09-01 17:19:15 +08:00
1 parent 3397e90749
commit 2f8301d188
9 files changed
+556 -17

No files matched your search

+60 -7
View File
@@ -1,4 +1,5 @@
import { reactive, ref } from "vue";
import { loadLocalPreferences, saveLocalPreferences } from "../services/local-persistence.js";
export interface VoiceState {
connected: boolean;
@@ -13,6 +14,7 @@ export interface VoiceState {
export interface ChannelMember {
id: number;
nickname: string;
uid?: string;
isSelf?: boolean;
}
@@ -27,7 +29,7 @@ export interface ChannelInfo {
parentID: string;
name: string;
description?: string;
members?: { id: number; nickname: string }[];
members?: { id: number; nickname: string; uid?: string }[];
}
export interface ChatMessage {
@@ -45,7 +47,9 @@ export function useVoiceWebSocket() {
const channels = reactive<ChannelInfo[]>([]);
const chatMessages = reactive<ChatMessage[]>([]);
let connectionSequence = 0;
let lastConnection: { target: string; channel: string; nickname: string; serverPassword: string } | null = null;
let lastConnection: { target: string; channel: string; nickname: string; serverPassword: string; identity?: string; rememberIdentity: boolean } | null = null;
const identityMaterial = ref("");
const storedVolumesByUid = reactive<Record<string, number>>({});
let microphoneStartPromise: Promise<void> | null = null;
// Audio capture. The ScriptProcessor path remains the most compatible option
@@ -83,6 +87,35 @@ export function useVoiceWebSocket() {
const speakingTimers = new Map<number, ReturnType<typeof setTimeout>>();
const SPEAKING_HOLD_MS = 360;
async function saveAudioPreferences(): Promise<void> {
await saveLocalPreferences({
schemaVersion: 1,
preferredInputDeviceId: selectedInputDeviceId.value,
inputDeviceId: selectedInputDeviceId.value,
voiceMode: micMode.value,
inputGain: inputVolume.value,
outputVolume: outputVolume.value,
volumesByUid: { ...storedVolumesByUid },
});
}
function syncKnownMemberVolumes(): void {
for (const member of members) {
if (!member.uid) continue;
const saved = storedVolumesByUid[member.uid];
if (saved !== undefined) volumes[member.id] = Math.max(0, Math.min(4, saved));
}
}
void loadLocalPreferences().then((preferences) => {
if (!selectedInputDeviceId.value) selectedInputDeviceId.value = preferences.preferredInputDeviceId ?? preferences.inputDeviceId ?? "";
if (preferences.voiceMode === "vox" || preferences.voiceMode === "ptt") micMode.value = preferences.voiceMode;
if (typeof preferences.inputGain === "number") inputVolume.value = Math.max(0, Math.min(1, preferences.inputGain));
if (typeof preferences.outputVolume === "number") outputVolume.value = Math.max(0, Math.min(1, preferences.outputVolume));
Object.assign(storedVolumesByUid, preferences.volumesByUid ?? {});
syncKnownMemberVolumes();
});
function markSpeaking(clientId: number): void {
if (!clientId) return;
speakingIds.add(clientId);
@@ -146,6 +179,7 @@ export function useVoiceWebSocket() {
if (selectedInputDeviceId.value && !microphones.some((device) => device.deviceId === selectedInputDeviceId.value)) {
selectedInputDeviceId.value = microphones[0]?.deviceId ?? "";
localStorage.setItem("webspeak:input-device", selectedInputDeviceId.value);
void saveAudioPreferences();
}
}
@@ -259,6 +293,7 @@ export function useVoiceWebSocket() {
const previousDeviceId = selectedInputDeviceId.value;
selectedInputDeviceId.value = deviceId;
localStorage.setItem("webspeak:input-device", deviceId);
void saveAudioPreferences();
try {
if (micStream) await startMicrophone();
await refreshInputDevices();
@@ -327,9 +362,10 @@ export function useVoiceWebSocket() {
}
}
function connect(target: string, channel: string, nickname: string, serverPassword = ""): void {
function connect(target: string, channel: string, nickname: string, serverPassword = "", identity = "", rememberIdentity = false): void {
disconnect(true);
lastConnection = { target, channel, nickname, serverPassword };
lastConnection = { target, channel, nickname, serverPassword, ...(identity ? { identity } : {}), rememberIdentity };
identityMaterial.value = identity;
const sequence = ++connectionSequence;
state.error = "";
state.connecting = true;
@@ -344,7 +380,7 @@ export function useVoiceWebSocket() {
const response = await fetch("/api/join-ticket", {
method: "POST",
headers: { "content-type": "application/json", accept: "application/json" },
body: JSON.stringify({ target, nickname, channel, serverPassword }),
body: JSON.stringify({ target, nickname, channel, serverPassword, ...(lastConnection?.rememberIdentity && lastConnection.identity ? { identity: lastConnection.identity } : {}), ...(lastConnection?.rememberIdentity ? { rememberIdentity: true } : {}) }),
});
const result = await response.json().catch(() => ({})) as { ticket?: unknown; code?: unknown };
if (!response.ok || typeof result.ticket !== "string") {
@@ -425,6 +461,7 @@ export function useVoiceWebSocket() {
state.reconnectAttempt = 0;
state.reconnectFailed = false;
state.tsClientId = 0;
identityMaterial.value = "";
members.length = 0;
channels.length = 0;
chatMessages.length = 0;
@@ -454,6 +491,11 @@ export function useVoiceWebSocket() {
for (const member of msg.members) {
members.push({ ...member, isSelf: Number(member.id) === state.tsClientId });
}
syncKnownMemberVolumes();
}
if (typeof msg.identity === "string" && msg.identity.length <= 8192) {
identityMaterial.value = msg.identity;
if (lastConnection) lastConnection.identity = msg.identity;
}
if (wasReconnecting) {
ensureMicrophone().catch((error: unknown) => {
@@ -463,7 +505,8 @@ export function useVoiceWebSocket() {
break;
case "memberEnter":
if (!members.some((member) => member.id === msg.id)) {
members.push({ id: msg.id, nickname: msg.nickname, isSelf: Boolean(msg.isSelf) });
members.push({ id: msg.id, nickname: msg.nickname, uid: typeof msg.uid === "string" ? msg.uid : undefined, isSelf: Boolean(msg.isSelf) });
syncKnownMemberVolumes();
}
break;
case "memberLeave": {
@@ -477,6 +520,7 @@ export function useVoiceWebSocket() {
if (Array.isArray(msg.channels)) {
for (const channel of msg.channels) channels.push(channel);
}
syncKnownMemberVolumes();
break;
case "chatMessage":
if (Number(msg.invokerId) === state.tsClientId) break;
@@ -553,12 +597,13 @@ export function useVoiceWebSocket() {
function reconnectNow(): void {
if (!lastConnection || state.connecting) return;
connect(lastConnection.target, lastConnection.channel, lastConnection.nickname, lastConnection.serverPassword);
connect(lastConnection.target, lastConnection.channel, lastConnection.nickname, lastConnection.serverPassword, lastConnection.rememberIdentity ? identityMaterial.value || lastConnection.identity : "", lastConnection.rememberIdentity);
}
function setMicMode(mode: "vox" | "ptt"): void {
micMode.value = mode;
if (mode === "vox") pttPressed = false;
void saveAudioPreferences();
}
function setPTT(pressed: boolean): void {
@@ -588,6 +633,11 @@ export function useVoiceWebSocket() {
function setVolume(clientId: number, volume: number): void {
const normalized = Math.max(0, Math.min(4, volume));
volumes[clientId] = normalized;
const member = members.find((candidate) => candidate.id === clientId);
if (member?.uid) {
storedVolumesByUid[member.uid] = normalized;
void saveAudioPreferences();
}
const gain = remoteGains.get(clientId);
if (gain) gain.gain.value = normalized * outputVolume.value;
}
@@ -595,11 +645,13 @@ export function useVoiceWebSocket() {
function setInputVolume(volume: number): void {
inputVolume.value = Math.max(0, Math.min(1, volume));
if (micGain) micGain.gain.value = inputVolume.value;
void saveAudioPreferences();
}
function setOutputVolume(volume: number): void {
outputVolume.value = Math.max(0, Math.min(1, volume));
for (const [clientId, gain] of remoteGains) gain.gain.value = (volumes[clientId] ?? 1) * outputVolume.value;
void saveAudioPreferences();
}
return {
@@ -613,6 +665,7 @@ export function useVoiceWebSocket() {
outputVolume,
inputDevices,
selectedInputDeviceId,
identityMaterial,
micLevel,
microphoneTestActive,
speakingIds,
+243
View File
@@ -0,0 +1,243 @@
const DB_NAME = "webspeak-local";
const DB_VERSION = 1;
const CURRENT_IDENTITY_KEY = "current";
const PREFERENCES_KEY = "singleton";
export interface StoredIdentity {
id: string;
label?: string;
privateMaterial: string;
createdAt: number;
lastUsedAt: number;
}
export interface FavoriteServer {
id: string;
label: string;
address: string;
nickname?: string;
identityId?: string;
lastChannelHint?: { id?: string; name?: string };
}
export interface RecentServer {
id: string;
address: string;
nickname?: string;
identityId?: string;
lastConnectedAt: number;
lastChannelHint?: { id?: string; name?: string };
}
export interface LocalPreferences {
schemaVersion: 1;
locale?: "auto" | "zh-CN" | "en";
theme?: "system" | "light" | "dark";
voiceMode?: "vox" | "ptt";
voxThreshold?: number;
language?: "zh" | "en";
preferredInputDeviceId?: string;
preferredOutputDeviceId?: string;
inputGain?: number;
outputVolume?: number;
notificationVolume?: number;
lastNickname?: string;
inputDeviceId?: string;
volumesByUid?: Record<string, number>;
}
let databasePromise: Promise<IDBDatabase> | null = null;
export function isLocalPersistenceAvailable(): boolean {
return typeof indexedDB !== "undefined";
}
function openDatabase(): Promise<IDBDatabase> {
if (!isLocalPersistenceAvailable()) return Promise.reject(new Error("IndexedDB unavailable"));
if (databasePromise) return databasePromise;
databasePromise = new Promise<IDBDatabase>((resolve, reject) => {
const request = indexedDB.open(DB_NAME, DB_VERSION);
request.onerror = () => reject(request.error ?? new Error("Could not open local storage"));
request.onupgradeneeded = () => {
const database = request.result;
if (!database.objectStoreNames.contains("identities")) database.createObjectStore("identities", { keyPath: "id" });
if (!database.objectStoreNames.contains("preferences")) database.createObjectStore("preferences", { keyPath: "id" });
if (!database.objectStoreNames.contains("favorites")) database.createObjectStore("favorites", { keyPath: "id" });
if (!database.objectStoreNames.contains("recent")) database.createObjectStore("recent", { keyPath: "id" });
};
request.onsuccess = () => {
const database = request.result;
database.onversionchange = () => database.close();
resolve(database);
};
}).catch((error) => {
databasePromise = null;
throw error;
});
return databasePromise!;
}
async function request<T>(storeName: string, mode: IDBTransactionMode, action: (store: IDBObjectStore, resolve: (value: T) => void, reject: (reason?: unknown) => void) => void): Promise<T> {
const database = await openDatabase();
return new Promise<T>((resolve, reject) => {
const transaction = database.transaction(storeName, mode);
action(transaction.objectStore(storeName), resolve, reject);
transaction.onerror = () => reject(transaction.error ?? new Error("Local storage transaction failed"));
});
}
export async function loadStoredIdentity(): Promise<StoredIdentity | null> {
try {
return await request<StoredIdentity | null>("identities", "readonly", (store, resolve, reject) => {
const get = store.get(CURRENT_IDENTITY_KEY);
get.onsuccess = () => resolve((get.result as StoredIdentity | undefined) ?? null);
get.onerror = () => reject(get.error);
});
} catch {
return null;
}
}
export async function saveStoredIdentity(privateMaterial: string, label = "此设备"): Promise<boolean> {
try {
const existing = await loadStoredIdentity();
await request("identities", "readwrite", (store, resolve, reject) => {
const put = store.put({
id: CURRENT_IDENTITY_KEY,
label,
privateMaterial,
createdAt: existing?.createdAt ?? Date.now(),
lastUsedAt: Date.now(),
} satisfies StoredIdentity);
put.onsuccess = () => resolve(undefined);
put.onerror = () => reject(put.error);
});
return true;
} catch {
return false;
}
}
export async function loadLocalPreferences(): Promise<LocalPreferences> {
const defaults: LocalPreferences = { schemaVersion: 1, volumesByUid: {} };
try {
const value = await request<(LocalPreferences & { id: string }) | null>("preferences", "readonly", (store, resolve, reject) => {
const get = store.get(PREFERENCES_KEY);
get.onsuccess = () => resolve((get.result as (LocalPreferences & { id: string }) | undefined) ?? null);
get.onerror = () => reject(get.error);
});
if (!value || value.schemaVersion !== 1) return defaults;
return { ...defaults, ...value, volumesByUid: { ...defaults.volumesByUid, ...value.volumesByUid } };
} catch {
return defaults;
}
}
export async function saveLocalPreferences(preferences: LocalPreferences): Promise<void> {
try {
const existing = await loadLocalPreferences();
await request("preferences", "readwrite", (store, resolve, reject) => {
const put = store.put({
...existing,
...preferences,
volumesByUid: { ...existing.volumesByUid, ...preferences.volumesByUid },
id: PREFERENCES_KEY,
});
put.onsuccess = () => resolve(undefined);
put.onerror = () => reject(put.error);
});
} catch {
// Local preference persistence is optional and never blocks joining.
}
}
export async function listFavorites(): Promise<FavoriteServer[]> {
try {
return await request<FavoriteServer[]>("favorites", "readonly", (store, resolve, reject) => {
const get = store.getAll();
get.onsuccess = () => resolve((get.result as FavoriteServer[]).sort((a, b) => a.label.localeCompare(b.label)));
get.onerror = () => reject(get.error);
});
} catch {
return [];
}
}
export async function saveFavorite(favorite: FavoriteServer): Promise<void> {
try {
await request("favorites", "readwrite", (store, resolve, reject) => {
const put = store.put(favorite);
put.onsuccess = () => resolve(undefined);
put.onerror = () => reject(put.error);
});
} catch {
// Favorites are an optional convenience.
}
}
export async function removeFavorite(id: string): Promise<void> {
try {
await request("favorites", "readwrite", (store, resolve, reject) => {
const remove = store.delete(id);
remove.onsuccess = () => resolve(undefined);
remove.onerror = () => reject(remove.error);
});
} catch {
// Favorites are an optional convenience.
}
}
export async function listRecentServers(): Promise<RecentServer[]> {
try {
const recent = await request<RecentServer[]>("recent", "readonly", (store, resolve, reject) => {
const get = store.getAll();
get.onsuccess = () => resolve(get.result as RecentServer[]);
get.onerror = () => reject(get.error);
});
return recent.sort((a, b) => b.lastConnectedAt - a.lastConnectedAt).slice(0, 10);
} catch {
return [];
}
}
export async function recordRecentServer(server: RecentServer): Promise<void> {
try {
await request("recent", "readwrite", (store, resolve, reject) => {
const put = store.put(server);
put.onsuccess = () => resolve(undefined);
put.onerror = () => reject(put.error);
});
const all = await request<RecentServer[]>("recent", "readonly", (store, resolve, reject) => {
const get = store.getAll();
get.onsuccess = () => resolve(get.result as RecentServer[]);
get.onerror = () => reject(get.error);
});
all.sort((a, b) => b.lastConnectedAt - a.lastConnectedAt);
for (const old of all.slice(10)) await removeRecentServer(old.id);
} catch {
// Recent servers are an optional convenience.
}
}
async function removeRecentServer(id: string): Promise<void> {
await request("recent", "readwrite", (store, resolve, reject) => {
const remove = store.delete(id);
remove.onsuccess = () => resolve(undefined);
remove.onerror = () => reject(remove.error);
});
}
export async function clearLocalData(): Promise<void> {
try {
const database = await openDatabase();
await Promise.all(["identities", "preferences", "favorites", "recent"].map((storeName) => new Promise<void>((resolve, reject) => {
const transaction = database.transaction(storeName, "readwrite");
transaction.objectStore(storeName).clear();
transaction.oncomplete = () => resolve();
transaction.onerror = () => reject(transaction.error ?? new Error("Local storage clear failed"));
transaction.onabort = () => reject(transaction.error ?? new Error("Local storage clear aborted"));
})));
} catch {
// Clearing local data is best effort when storage is unavailable.
}
}
+182 -2
View File
@@ -33,6 +33,7 @@
<div v-if="voiceState.error" class="notice error-notice"><span class="notice-symbol">!</span><span>{{ localizedMessage(voiceState.error) }}</span></div>
<div v-if="browserError" class="notice warning-notice"><span class="notice-symbol">i</span><span>{{ localizedMessage(browserError) }}</span></div>
<div v-if="!serverConfigLoading && !initialized" class="notice warning-notice"><span class="notice-symbol">i</span><span>{{ t('notConfigured') }} <a href="/admin">{{ t('configureNow') }}</a></span></div>
<div v-if="!localPersistenceAvailable" class="notice warning-notice"><span class="notice-symbol">i</span><span>{{ t('localPersistenceUnavailable') }}</span></div>
<form v-if="initialized" class="join-form" @submit.prevent="doConnect">
<div v-if="accessMode === 'open'">
@@ -40,6 +41,11 @@
<div class="field-wrap"><Icon name="server" :size="17" /><input id="server-address" v-model="serverAddress" autocomplete="url" :placeholder="t('serverAddressPlaceholder')" /></div>
</div>
<p v-if="accessMode === 'open'" class="field-hint">{{ t('serverAddressHint') }}</p>
<div v-if="accessMode === 'open' && (favoriteServers.length || recentServers.length)" class="local-servers">
<div v-if="favoriteServers.length" class="local-server-group"><span>{{ t('favoriteServers') }}</span><button v-for="favorite in favoriteServers" :key="favorite.id" type="button" @click="selectLocalServer(favorite.address, favorite.nickname)">{{ favorite.label }}</button></div>
<div v-if="recentServers.length" class="local-server-group"><span>{{ t('recentServers') }}</span><button v-for="recent in recentServers" :key="recent.id" type="button" @click="selectLocalServer(recent.address, recent.nickname)">{{ recent.address }}</button></div>
</div>
<button v-if="accessMode === 'open' && serverAddress.trim()" type="button" class="favorite-toggle" @click="toggleFavorite">{{ isFavorite ? t('removeFavorite') : t('saveFavorite') }}</button>
<template v-if="accessMode === 'open'">
<label class="field-label" for="server-password">{{ t('serverPassword') }} <span>{{ t('optional') }}</span></label>
@@ -58,6 +64,13 @@
<input id="channel" v-model="channel" :placeholder="t('emptyDefault')" @keyup.enter="doConnect" />
</div>
<label class="remember-identity"><input v-model="rememberIdentity" type="checkbox" /><span><strong>{{ t('rememberIdentity') }}</strong><small>{{ t('rememberIdentityHint') }}</small></span></label>
<div class="identity-tools">
<button type="button" class="identity-tool-button" @click="identityFileInput?.click()">{{ t('identityImport') }}</button>
<button v-if="identityMaterial" type="button" class="identity-tool-button" @click="exportIdentity">{{ t('identityExport') }}</button>
<input ref="identityFileInput" class="visually-hidden" type="file" accept=".json,.txt,application/json,text/plain" @change="importIdentity" />
</div>
<button class="primary-button connect-button" :disabled="!canJoin || serverConfigLoading || voiceState.connecting" type="submit">
<span v-if="voiceState.connecting" class="button-spinner"></span>
<span>{{ voiceState.connecting ? t('connecting') : t('enterVoice') }}</span>
@@ -69,7 +82,7 @@
</main>
<footer class="join-footer">
<span>WebSpeak</span><span class="footer-separator">·</span><span>{{ t('teamSpeakClient') }}</span><span class="footer-spacer"></span><a href="https://github.com/EchoSixHIYA/web-gateway-for-TeamSpeak" target="_blank" rel="noreferrer">{{ t('sourceCode') }}</a><span class="footer-separator">·</span><span>{{ t('browserSupport') }}</span>
<span>WebSpeak</span><span class="footer-separator">·</span><span>{{ t('teamSpeakClient') }}</span><span class="footer-spacer"></span><button type="button" class="clear-local-button" @click="clearBrowserData">{{ t('clearLocalData') }}</button><span class="footer-separator">·</span><a href="https://github.com/EchoSixHIYA/web-gateway-for-TeamSpeak" target="_blank" rel="noreferrer">{{ t('sourceCode') }}</a><span class="footer-separator">·</span><span>{{ t('browserSupport') }}</span>
</footer>
</section>
@@ -184,6 +197,7 @@
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from "vue";
import Icon from "../components/Icon.vue";
import { useVoiceWebSocket, type ChannelInfo, type ChannelMember } from "../composables/useVoiceWebSocket.js";
import { clearLocalData as clearStoredLocalData, isLocalPersistenceAvailable, listFavorites, listRecentServers, loadLocalPreferences, loadStoredIdentity, recordRecentServer, removeFavorite, saveFavorite, saveLocalPreferences, saveStoredIdentity, type FavoriteServer, type RecentServer } from "../services/local-persistence.js";
interface TreeChannel extends ChannelInfo {
depth: number;
@@ -200,6 +214,7 @@ const {
outputVolume,
inputDevices,
selectedInputDeviceId,
identityMaterial,
micLevel,
microphoneTestActive,
speakingIds,
@@ -229,6 +244,9 @@ const channel = ref(initialChannel);
const serverAddress = ref(initialServerAddress());
const serverPassword = ref("");
const accessMode = ref<"fixed" | "open">("fixed");
const rememberIdentity = ref(localStorage.getItem("webspeak:remember-identity") === "1");
const favoriteServers = ref<FavoriteServer[]>([]);
const recentServers = ref<RecentServer[]>([]);
const initialized = ref(false);
const siteName = ref("WebSpeak");
const welcomeText = ref("");
@@ -242,6 +260,8 @@ const audioSettingsError = ref("");
const pttActive = ref(false);
const toast = ref("");
const chatListEl = ref<HTMLElement | null>(null);
const identityFileInput = ref<HTMLInputElement | null>(null);
const localPersistenceAvailable = isLocalPersistenceAvailable();
let toastTimer: ReturnType<typeof setTimeout> | undefined;
type Language = "zh" | "en";
@@ -277,6 +297,24 @@ const translations: Record<Language, Record<string, string>> = {
targetChannel: "目标频道",
optional: "可选",
emptyDefault: "留空进入默认频道",
rememberIdentity: "记住此设备的 TeamSpeak 身份",
rememberIdentityHint: "仅保存在本设备,用于下次连接时保持身份。",
localPersistenceUnavailable: "当前浏览器无法使用持久化存储,本次将使用临时身份。",
identityImport: "导入身份",
identityExport: "导出身份",
identityExportWarning: "身份文件包含私密凭据,任何拿到文件的人都可以冒充此身份。请妥善保管。",
identityExportedToast: "身份文件已下载,请妥善保管",
identityImportedToast: "身份已导入,将在下次连接时使用",
identityImportFailed: "身份文件无效,请选择 WebSpeak 身份文件",
favoriteServers: "常用服务器",
recentServers: "最近连接",
saveFavorite: "保存到常用",
removeFavorite: "移除常用",
savedFavoriteToast: "已保存到常用服务器",
removedFavoriteToast: "已从常用服务器移除",
clearLocalData: "清除本地数据",
clearLocalDataConfirm: "确定清除本设备保存的身份、收藏、最近连接和音频偏好吗?",
localDataCleared: "本地数据已清除",
connecting: "正在连接…",
enterVoice: "进入语音空间",
connectionAuthorized: "连接信息仅用于本次语音会话",
@@ -418,6 +456,24 @@ const translations: Record<Language, Record<string, string>> = {
targetChannel: "Target channel",
optional: "Optional",
emptyDefault: "Leave empty to use the default channel",
rememberIdentity: "Remember this TeamSpeak identity on this device",
rememberIdentityHint: "Stored only on this device and reused on the next connection.",
localPersistenceUnavailable: "Persistent browser storage is unavailable; this session will use an ephemeral identity.",
identityImport: "Import identity",
identityExport: "Export identity",
identityExportWarning: "Identity files contain private credentials. Anyone with the file can impersonate this identity. Keep it safe.",
identityExportedToast: "Identity file downloaded. Keep it safe.",
identityImportedToast: "Identity imported and will be used on the next connection.",
identityImportFailed: "Invalid identity file. Choose a WebSpeak identity file.",
favoriteServers: "Favorites",
recentServers: "Recent servers",
saveFavorite: "Save favorite",
removeFavorite: "Remove favorite",
savedFavoriteToast: "Saved to favorites",
removedFavoriteToast: "Removed from favorites",
clearLocalData: "Clear local data",
clearLocalDataConfirm: "Clear this device's identity, favorites, recent servers, and audio preferences?",
localDataCleared: "Local data cleared",
connecting: "Connecting…",
enterVoice: "Enter voice space",
connectionAuthorized: "Connection details are used only for this voice session",
@@ -590,6 +646,7 @@ function localizedMessage(message: string) {
function toggleLanguage() {
language.value = language.value === "zh" ? "en" : "zh";
localStorage.setItem("webspeak:language", language.value);
void saveLocalPreferences({ schemaVersion: 1, language: language.value });
}
const heroBars = [12, 24, 18, 35, 18, 28, 42, 23, 50, 34, 19, 28, 39, 22, 46, 25, 17, 31, 14];
@@ -659,12 +716,45 @@ watch(settingsOpen, (open) => {
stopMicrophoneTest();
}
});
watch(rememberIdentity, (remember) => {
localStorage.setItem("webspeak:remember-identity", remember ? "1" : "0");
if (!remember) identityMaterial.value = "";
});
watch([rememberIdentity, identityMaterial], ([remember, material]) => {
if (remember && material) void saveStoredIdentity(material);
if (!remember && material) identityMaterial.value = "";
});
watch(() => voiceState.connected, (connected) => {
if (!connected) return;
const address = serverAddress.value.trim();
if (!address) return;
const recent: RecentServer = {
id: serverKey(address),
address,
...(nickname.value.trim() ? { nickname: nickname.value.trim() } : {}),
...(rememberIdentity.value && identityMaterial.value ? { identityId: "current" } : {}),
lastConnectedAt: Date.now(),
...(channel.value.trim() ? { lastChannelHint: { name: channel.value.trim() } } : {}),
};
void recordRecentServer(recent).then(() => listRecentServers().then((items) => { recentServers.value = items; }));
});
let deviceChangeHandler: (() => void) | undefined;
onMounted(() => {
browserError.value = checkSupport() ?? "";
void loadPublicConfig();
void loadLocalPreferences().then((preferences) => {
if (!localStorage.getItem("webspeak:language") && (preferences.language === "zh" || preferences.language === "en")) language.value = preferences.language;
});
void loadStoredIdentity().then((stored) => {
if (stored) {
identityMaterial.value = stored.privateMaterial;
rememberIdentity.value = true;
}
});
void listFavorites().then((items) => { favoriteServers.value = items; });
void listRecentServers().then((items) => { recentServers.value = items; });
deviceChangeHandler = () => { void prepareInputDevices().catch(() => undefined); };
navigator.mediaDevices?.addEventListener("devicechange", deviceChangeHandler);
});
@@ -679,11 +769,12 @@ function doConnect() {
clearError();
nickname.value = nickname.value.trim();
localStorage.setItem("webspeak:nickname", nickname.value);
void saveLocalPreferences({ schemaVersion: 1, lastNickname: nickname.value });
if (accessMode.value === "open") {
serverAddress.value = serverAddress.value.trim();
}
selectedChannelId.value = "";
connect(serverAddress.value, channel.value.trim(), nickname.value, accessMode.value === "open" ? serverPassword.value : "");
connect(serverAddress.value, channel.value.trim(), nickname.value, accessMode.value === "open" ? serverPassword.value : "", rememberIdentity.value ? identityMaterial.value : "", rememberIdentity.value);
}
function doDisconnect() {
@@ -724,6 +815,78 @@ const canJoin = computed(() => Boolean(
&& nickname.value.trim()
&& (accessMode.value === "fixed" || serverAddress.value.trim()),
));
const isFavorite = computed(() => favoriteServers.value.some((favorite) => favorite.id === serverKey(serverAddress.value)));
function serverKey(address: string): string {
return address.trim().toLocaleLowerCase();
}
function selectLocalServer(address: string, savedNickname?: string): void {
serverAddress.value = address;
if (savedNickname && !nickname.value.trim()) nickname.value = savedNickname;
}
async function toggleFavorite(): Promise<void> {
const address = serverAddress.value.trim();
if (!address) return;
const id = serverKey(address);
const existing = favoriteServers.value.find((favorite) => favorite.id === id);
if (existing) {
await removeFavorite(id);
favoriteServers.value = favoriteServers.value.filter((favorite) => favorite.id !== id);
showToast(t("removedFavoriteToast"));
return;
}
const favorite: FavoriteServer = { id, label: address, address, ...(nickname.value.trim() ? { nickname: nickname.value.trim() } : {}), ...(rememberIdentity.value && identityMaterial.value ? { identityId: "current" } : {}), ...(channel.value.trim() ? { lastChannelHint: { name: channel.value.trim() } } : {}) };
await saveFavorite(favorite);
favoriteServers.value = [...favoriteServers.value, favorite].sort((a, b) => a.label.localeCompare(b.label));
showToast(t("savedFavoriteToast"));
}
async function clearBrowserData(): Promise<void> {
if (!window.confirm(t("clearLocalDataConfirm"))) return;
await clearStoredLocalData();
for (const key of ["webspeak:nickname", "webspeak:language", "webspeak:input-device", "webspeak:remember-identity"]) localStorage.removeItem(key);
identityMaterial.value = "";
rememberIdentity.value = false;
favoriteServers.value = [];
recentServers.value = [];
showToast(t("localDataCleared"));
}
function exportIdentity(): void {
if (!identityMaterial.value) return;
if (!window.confirm(t("identityExportWarning"))) return;
const contents = JSON.stringify({
format: "webspeak-identity",
version: 1,
privateMaterial: identityMaterial.value,
exportedAt: new Date().toISOString(),
}, null, 2);
const url = URL.createObjectURL(new Blob([contents], { type: "application/json" }));
const link = document.createElement("a");
link.href = url;
link.download = "webspeak-identity.json";
link.click();
URL.revokeObjectURL(url);
showToast(t("identityExportedToast"));
}
async function importIdentity(event: Event): Promise<void> {
const input = event.target as HTMLInputElement;
const file = input.files?.[0];
input.value = "";
if (!file) return;
try {
const parsed = JSON.parse(await file.text()) as { format?: unknown; version?: unknown; privateMaterial?: unknown };
if (parsed.format !== "webspeak-identity" || parsed.version !== 1 || typeof parsed.privateMaterial !== "string" || parsed.privateMaterial.length === 0 || parsed.privateMaterial.length > 8192) throw new Error("invalid identity");
identityMaterial.value = parsed.privateMaterial;
rememberIdentity.value = true;
showToast(t("identityImportedToast"));
} catch {
showToast(t("identityImportFailed"));
}
}
async function loadPublicConfig() {
try {
@@ -975,6 +1138,10 @@ function stopPointerTalk() {
.app-shell { grid-template-columns: minmax(0, 1fr) 318px; }
.nav-rail, .channel-sidebar { display: none; }
.workspace { min-width: 0; }
.identity-tools { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 1px; }
.identity-tool-button { padding: 5px 8px; color: #277970; background: #eef8f5; border: 1px solid #d7ebe6; border-radius: 6px; font-size: 10px; cursor: pointer; }
.identity-tool-button:hover { background: #e0f3ee; }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
.channel-switcher { display: inline-flex; align-items: center; gap: 6px; min-height: 32px; padding: 0 8px; color: #006a64; background: #edf7f4; border: 1px solid #d7ebe6; border-radius: 8px; }
.channel-switcher select { max-width: 165px; padding: 0 2px; color: #31514b; border: 0; outline: 0; background: transparent; font-size: 11px; cursor: pointer; }
.member-panel { display: flex; flex-direction: column; min-height: 0; padding: 26px 18px 18px; overflow: hidden; }
@@ -1005,6 +1172,19 @@ function stopPointerTalk() {
.reconnect-copy span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 11px; }
.reconnect-actions { display: flex; align-items: center; gap: 12px; flex: 0 0 auto; }
.reconnect-actions .secondary-button { min-height: 34px; padding-inline: 13px; }
.remember-identity { display: flex; align-items: flex-start; gap: 9px; margin-top: 8px; color: #465650; cursor: pointer; }
.remember-identity input { width: 16px; height: 16px; flex: 0 0 auto; margin: 1px 0 0; accent-color: #087d74; }
.remember-identity strong, .remember-identity small { display: block; }
.remember-identity strong { font-size: 11px; font-weight: 700; }
.remember-identity small { margin-top: 3px; color: #8b9994; font-size: 10px; line-height: 1.4; }
.local-servers { display: grid; gap: 8px; margin: 1px 0 5px; }
.local-server-group { display: flex; flex-wrap: wrap; align-items: center; gap: 5px; }
.local-server-group > span { width: 100%; color: #87958f; font-size: 10px; font-weight: 700; }
.local-server-group button, .favorite-toggle { padding: 5px 8px; color: #277970; background: #eef8f5; border: 1px solid #d7ebe6; border-radius: 6px; font-size: 10px; cursor: pointer; }
.local-server-group button:hover, .favorite-toggle:hover { background: #e0f3ee; }
.favorite-toggle { justify-self: start; margin: -1px 0 4px; }
.clear-local-button { padding: 0; color: #85928d; background: transparent; border: 0; font-size: 10px; cursor: pointer; }
.clear-local-button:hover { color: #b14e47; text-decoration: underline; }
@media (max-width: 740px) { .reconnect-banner { align-items: flex-start; flex-direction: column; gap: 10px; width: calc(100% - 30px); }.reconnect-copy { align-items: flex-start; flex-direction: column; gap: 4px; }.reconnect-actions { width: 100%; justify-content: flex-end; } }
.save-button { min-height: 38px; }