68 lines
2.4 KiB
TypeScript
68 lines
2.4 KiB
TypeScript
import { api } from '../services/api.js';
|
|
import { initConfig, setState, getConfig } from '../state/store.js';
|
|
import { renderSidebar, initNavigation } from '../ui/navigation.js';
|
|
import { showToast } from '../ui/toast.js';
|
|
import { initSelectionView } from '../components/guildSelect.js';
|
|
import { initDashboardView } from '../components/dashboard.js';
|
|
|
|
function readConfig(): void {
|
|
const root = document.getElementById('app');
|
|
if (!root) throw new Error('App-Container fehlt');
|
|
const view = (root.dataset.view as 'selection' | 'dashboard') || 'selection';
|
|
const baseRoot = root.dataset.baseRoot || '/ucp';
|
|
const baseDashboard = root.dataset.baseDashboard || `${baseRoot}/dashboard`;
|
|
const baseAuth = root.dataset.baseAuth || `${baseRoot}/auth`;
|
|
const baseApi = root.dataset.baseApi || `${baseRoot}/api`;
|
|
const initialGuildId = root.dataset.guildId || undefined;
|
|
const isAdmin = root.dataset.userAdmin === 'true';
|
|
const userLabel = root.dataset.userName
|
|
? `${root.dataset.userName}${root.dataset.userDisc ? '#' + root.dataset.userDisc : ''}`
|
|
: undefined;
|
|
|
|
initConfig({ baseRoot, baseDashboard, baseAuth, baseApi, view, initialGuildId, isAdmin, userLabel });
|
|
}
|
|
|
|
async function ensureAuth() {
|
|
try {
|
|
const me = await api.me();
|
|
if (!me?.user) {
|
|
const cfg = getConfig();
|
|
window.location.href = (cfg?.baseAuth || '/auth') + '/discord';
|
|
return null;
|
|
}
|
|
const userInfo = document.getElementById('userInfo');
|
|
if (userInfo && me.user) userInfo.textContent = `${me.user.username}#${me.user.discriminator}`;
|
|
setState({ isAdmin: !!me.user?.isAdmin, userLabel: me.user ? `${me.user.username}#${me.user.discriminator}` : undefined });
|
|
return me;
|
|
} catch (err) {
|
|
console.error(err);
|
|
showToast('Authentifizierung fehlgeschlagen', true);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function bootstrap() {
|
|
readConfig();
|
|
const cfg = getConfig();
|
|
if (!cfg) return;
|
|
|
|
const sidebarRoot = document.getElementById('sidebar-root');
|
|
if (sidebarRoot) renderSidebar(sidebarRoot, !!cfg.isAdmin);
|
|
|
|
if (cfg.view === 'selection') {
|
|
initSelectionView();
|
|
} else {
|
|
await ensureAuth();
|
|
initDashboardView();
|
|
initNavigation((section) => {
|
|
// Sections werden innerhalb der jeweiligen Komponenten bedient
|
|
if (section === 'admin' && !cfg.isAdmin) showToast('Kein Admin-Recht', true);
|
|
});
|
|
}
|
|
}
|
|
|
|
bootstrap().catch((err) => {
|
|
console.error(err);
|
|
showToast('Fehler beim Laden', true);
|
|
});
|