// SimpleNote Web - Main Application import { api } from './api.js'; import { renderLogin, initLoginHandlers } from './views/login.js'; import { renderDashboard } from './views/dashboard.js'; import { renderDocument } from './views/document.js'; import { renderEditor } from './views/editor.js'; class App { constructor() { this.currentView = null; this.state = { token: localStorage.getItem('sn_token'), view: 'dashboard', params: {} }; } async init() { if (!this.state.token) { this.renderLogin(); return; } api.setToken(this.state.token); try { await api.login(this.state.token); this.render(); } catch (e) { this.state.token = null; localStorage.removeItem('sn_token'); this.renderLogin(); } } renderLogin() { const app = document.getElementById('app'); app.innerHTML = renderLogin(); initLoginHandlers(async (token) => { try { await api.login(token); this.state.token = token; this.state.view = 'dashboard'; this.render(); } catch (e) { return 'Invalid token'; } }); } async render() { const app = document.getElementById('app'); switch (this.state.view) { case 'dashboard': await renderDashboard(this); break; case 'document': await renderDocument(this); break; case 'editor': renderEditor(this); break; default: await renderDashboard(this); } } navigate(view, params = {}) { this.state.view = view; this.state.params = params; this.render(); } showToast(message, type = 'info') { const container = document.getElementById('toast-container'); const toast = document.createElement('div'); toast.className = `toast ${type}`; toast.innerHTML = ` `; container.appendChild(toast); setTimeout(() => toast.remove(), 4000); } async confirmDelete(message) { return new Promise((resolve) => { const backdrop = document.createElement('div'); backdrop.className = 'modal-backdrop'; backdrop.innerHTML = `
${message}