Files
Papo/public/ts-build/components/dashboard.js
Pascal Prießnitz 7a296f7b4a
All checks were successful
Deploy Discord Bot / deploy (push) Successful in 38s
[deploy] Emit frontend bundle as ESM for browser
2025-12-04 17:06:27 +01:00

91 lines
3.0 KiB
JavaScript

import { api } from '../services/api.js';
import { getConfig, getState, setState } from '../state/store.js';
import { showToast } from '../ui/toast.js';
import { renderOverview } from './overview.js';
import { initTicketsSection } from './tickets/index.js';
import { initModulesSection } from './modules/index.js';
import { initEventsSection } from './events/index.js';
import { initAdminSection } from './admin/index.js';
import { renderSettingsSection } from './settings.js';
let overviewInterval = null;
let ticketsInterval = null;
async function populateGuildSelect() {
const select = document.getElementById('guildSelect');
const cfg = getConfig();
if (!select || !cfg)
return;
select.innerHTML = `<option>Loading...</option>`;
try {
const data = await api.guilds();
select.innerHTML = '';
data.guilds.forEach((g) => {
const opt = document.createElement('option');
opt.value = g.id;
opt.textContent = g.name;
if (g.id === cfg.initialGuildId)
opt.selected = true;
select.appendChild(opt);
});
const current = select.value || cfg.initialGuildId || data.guilds[0]?.id;
setState({ guildId: current || undefined });
select.value = current || '';
}
catch (err) {
console.error(err);
showToast('Guilds konnten nicht geladen werden', true);
}
}
function registerGuildChange() {
const select = document.getElementById('guildSelect');
if (!select)
return;
select.addEventListener('change', () => {
const guildId = select.value;
setState({ guildId });
refreshSections();
});
}
async function refreshSections() {
const { guildId } = getState();
if (!guildId)
return;
await renderOverview(guildId);
await initTicketsSection(guildId);
await initModulesSection(guildId);
await renderSettingsSection(guildId);
await initEventsSection(guildId);
const cfg = getConfig();
if (cfg?.isAdmin) {
await initAdminSection(guildId);
}
}
function setupPolling() {
const { guildId } = getState();
if (overviewInterval)
window.clearInterval(overviewInterval);
if (ticketsInterval)
window.clearInterval(ticketsInterval);
overviewInterval = window.setInterval(() => {
const current = getState().guildId;
if (current)
renderOverview(current);
}, 10000);
ticketsInterval = window.setInterval(() => {
const current = getState().guildId;
if (current)
initTicketsSection(current);
}, 12000);
}
export function initDashboardView() {
const cfg = getConfig();
const logoutBtn = document.getElementById('logoutBtn');
if (logoutBtn && cfg) {
logoutBtn.addEventListener('click', () => (window.location.href = `${cfg.baseAuth}/logout`));
}
populateGuildSelect().then(() => {
registerGuildChange();
refreshSections();
setupPolling();
});
}