Fix account tab persistence and viewer fallback

This commit is contained in:
2026-06-27 19:09:07 +02:00
parent 3eb6bdf534
commit 59cdad220a
3 changed files with 85 additions and 17 deletions
+38 -7
View File
@@ -10,6 +10,8 @@ const state = {
authStates: {},
};
const ACTIVE_ACCOUNT_STORAGE_KEY = "telegramScraper.activeAccount";
/* ── API helper ─────────────────────────────────────── */
async function api(path, options = {}) {
@@ -78,6 +80,33 @@ function isAccountAuthorized(auth) {
);
}
function saveActiveAccount(accountId) {
if (accountId) {
localStorage.setItem(ACTIVE_ACCOUNT_STORAGE_KEY, accountId);
}
}
function loadSavedActiveAccount(accounts) {
let savedId = localStorage.getItem(ACTIVE_ACCOUNT_STORAGE_KEY);
if (!savedId) {
savedId = localStorage.getItem("activeAccount");
if (savedId) {
localStorage.setItem(ACTIVE_ACCOUNT_STORAGE_KEY, savedId);
localStorage.removeItem("activeAccount");
}
}
if (savedId && accounts.some((account) => account.id === savedId)) {
return savedId;
}
if (savedId) {
localStorage.removeItem(ACTIVE_ACCOUNT_STORAGE_KEY);
}
if (state.activeAccount && accounts.some((account) => account.id === state.activeAccount)) {
return state.activeAccount;
}
return accounts[0]?.id || null;
}
/* ── Account tab management ─────────────────────────── */
function renderAccountTabs(accounts) {
@@ -175,9 +204,8 @@ function renderAccountPanel(accountId) {
}
function switchAccount(accountId) {
if (state.activeAccount === accountId) return;
state.activeAccount = accountId;
localStorage.setItem("activeAccount", accountId);
saveActiveAccount(accountId);
// Update tabs
document.querySelectorAll(".account-tab").forEach((tab) => {
@@ -490,9 +518,10 @@ async function loadAccounts() {
accounts.forEach((acc) => renderAccountPanel(acc.id));
// Determine active account (persisted across reloads)
const savedId = localStorage.getItem("activeAccount");
const activeId = (savedId && accounts.some(a => a.id === savedId)) ? savedId : (state.activeAccount || accounts[0].id);
switchAccount(activeId);
const activeId = loadSavedActiveAccount(accounts);
if (activeId) {
switchAccount(activeId);
}
// Update settings
renderSettingsAccounts();
@@ -524,11 +553,13 @@ function renderSettingsAccounts() {
node.querySelector(".account-remove-btn").addEventListener("click", async () => {
if (!confirmAction(`Remove account "${acc.label || acc.id}"? All its data will be deleted.`)) return;
try {
const removingActive = state.activeAccount === acc.id;
await api(`/api/accounts/${acc.id}`, { method: "DELETE" });
await loadAccounts();
if (state.activeAccount === acc.id) {
if (removingActive) {
state.activeAccount = null;
localStorage.removeItem(ACTIVE_ACCOUNT_STORAGE_KEY);
}
await loadAccounts();
} catch (err) {
alert(`Failed to remove account: ${err.message}`);
}