/* app.js — จุดเริ่มของแอป: โหลดข้อมูล → ผูก router → เรนเดอร์หน้า */ import { hydrateIcons, toast } from "./ui.js"; import { loadProjects, normalizeData } from "./data.js"; import { defineRoutes, markActiveNav, onRouteChange, startRouter } from "./router.js"; import { countUpInside, initMotion, initReveal } from "./motion.js"; import { homeView } from "./views/home.js"; import { adminView } from "./views/admin.js"; import "./pwa.js"; const main = document.getElementById("main"); const state = { data: normalizeData({}), source: "empty", }; const ctx = { get data() { return state.data; }, get source() { return state.source; }, setData(data) { state.data = normalizeData(data); state.source = "server"; }, async reload() { const result = await loadProjects(); state.data = result.data; state.source = result.source; return result; }, }; let activeView = null; const TITLES = { "/": "โปรเจคอดิเรกของเรา", "/admin": "จัดการข้อมูล · โปรเจคอดิเรก", }; function renderRoute({ path, view }) { if (activeView && typeof activeView.destroy === "function") activeView.destroy(); main.replaceChildren(); activeView = view(ctx); activeView.mount(main); markActiveNav(path); document.title = TITLES[path] || TITLES["/"]; window.scrollTo({ top: 0, behavior: "auto" }); main.focus({ preventScroll: true }); initReveal(main); countUpInside(main); } function skeleton() { main.innerHTML = `

กำลังโหลดรายชื่อโปรเจค

`; } async function boot() { hydrateIcons(); initMotion(); skeleton(); const result = await loadProjects(); state.data = result.data; state.source = result.source; defineRoutes({ "/": homeView, "/admin": adminView }, "/"); onRouteChange(renderRoute); startRouter(); if (result.source === "cache") { toast("เปิดแบบออฟไลน์ — แสดงข้อมูลที่บันทึกไว้ในเครื่อง"); } else if (result.source === "empty") { toast("อ่านข้อมูลโปรเจคไม่ได้ — เข้าไปเพิ่มรายการได้ที่หน้า “จัดการข้อมูล”", "error"); } } boot();