add/remove server
This commit is contained in:
@@ -199,6 +199,15 @@ function getServerEntries() {
|
||||
});
|
||||
}
|
||||
|
||||
function setConfig(nextConfig) {
|
||||
localStorage.setItem('config', JSON.stringify(nextConfig));
|
||||
}
|
||||
|
||||
async function refreshServerStatus() {
|
||||
await InitializeServerStatus();
|
||||
renderMenu();
|
||||
}
|
||||
|
||||
function buildDefaultOptions(channel) {
|
||||
const selected = {};
|
||||
if (!channel || !Array.isArray(channel.options)) return selected;
|
||||
@@ -227,6 +236,9 @@ function renderMenu() {
|
||||
const sourceSelect = document.getElementById('source-select');
|
||||
const channelSelect = document.getElementById('channel-select');
|
||||
const filtersContainer = document.getElementById('filters-container');
|
||||
const sourcesList = document.getElementById('sources-list');
|
||||
const addSourceBtn = document.getElementById('add-source-btn');
|
||||
const sourceInput = document.getElementById('source-input');
|
||||
|
||||
if (!sourceSelect || !channelSelect || !filtersContainer) return;
|
||||
|
||||
@@ -299,6 +311,84 @@ function renderMenu() {
|
||||
applyTheme();
|
||||
};
|
||||
}
|
||||
|
||||
if (sourcesList) {
|
||||
sourcesList.innerHTML = "";
|
||||
serverEntries.forEach((entry) => {
|
||||
const row = document.createElement('div');
|
||||
row.className = 'source-item';
|
||||
|
||||
const text = document.createElement('span');
|
||||
text.textContent = entry.url;
|
||||
|
||||
const removeBtn = document.createElement('button');
|
||||
removeBtn.type = 'button';
|
||||
removeBtn.textContent = 'Remove';
|
||||
removeBtn.onclick = async () => {
|
||||
const config = getConfig();
|
||||
config.servers = (config.servers || []).filter((serverObj) => {
|
||||
const key = Object.keys(serverObj)[0];
|
||||
return key !== entry.url;
|
||||
});
|
||||
setConfig(config);
|
||||
|
||||
const remaining = getServerEntries();
|
||||
if (remaining.length === 0) {
|
||||
localStorage.removeItem('session');
|
||||
} else {
|
||||
const nextServerUrl = remaining[0].url;
|
||||
const nextServer = remaining[0];
|
||||
const nextChannel = nextServer.data && nextServer.data.channels ? nextServer.data.channels[0] : null;
|
||||
setSession({
|
||||
server: nextServerUrl,
|
||||
channel: nextChannel,
|
||||
options: nextChannel ? buildDefaultOptions(nextChannel) : {}
|
||||
});
|
||||
}
|
||||
|
||||
await refreshServerStatus();
|
||||
resetAndReload();
|
||||
};
|
||||
|
||||
row.appendChild(text);
|
||||
row.appendChild(removeBtn);
|
||||
sourcesList.appendChild(row);
|
||||
});
|
||||
}
|
||||
|
||||
if (addSourceBtn && sourceInput) {
|
||||
addSourceBtn.onclick = async () => {
|
||||
const raw = sourceInput.value.trim();
|
||||
if (!raw) return;
|
||||
const normalized = raw.endsWith('/') ? raw.slice(0, -1) : raw;
|
||||
|
||||
const config = getConfig();
|
||||
const exists = (config.servers || []).some((serverObj) => Object.keys(serverObj)[0] === normalized);
|
||||
if (!exists) {
|
||||
config.servers = config.servers || [];
|
||||
config.servers.push({ [normalized]: {} });
|
||||
setConfig(config);
|
||||
sourceInput.value = '';
|
||||
await refreshServerStatus();
|
||||
|
||||
const session = getSession();
|
||||
if (!session || session.server !== normalized) {
|
||||
const entries = getServerEntries();
|
||||
const addedEntry = entries.find((entry) => entry.url === normalized);
|
||||
const nextChannel = addedEntry && addedEntry.data && addedEntry.data.channels
|
||||
? addedEntry.data.channels[0]
|
||||
: null;
|
||||
setSession({
|
||||
server: normalized,
|
||||
channel: nextChannel,
|
||||
options: nextChannel ? buildDefaultOptions(nextChannel) : {}
|
||||
});
|
||||
}
|
||||
renderMenu();
|
||||
resetAndReload();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function renderFilters(container, session) {
|
||||
|
||||
Reference in New Issue
Block a user