Files
Papo/public/ts/components/dashboard.ts
Pascal Prießnitz 22caa79b54
All checks were successful
Deploy Discord Bot / deploy (push) Successful in 37s
[deploy]
2025-12-04 16:43:38 +01:00

89 lines
2.8 KiB
TypeScript

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: number | null = null;
let ticketsInterval: number | null = null;
async function populateGuildSelect() {
const select = document.getElementById('guildSelect') as HTMLSelectElement | null;
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') as HTMLSelectElement | null;
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();
});
}