Files
hobbypj-contents/ui/views/home.js
T
Bkk1234 db7724b237 feat(home): link the homepage of every local project in the workspace
- server: GET /api/local-projects scans sibling folders for index.html
  (title from <title>/<h1>, generic titles fall back to the folder name,
  sorted by newest edit) and GET /projects/<folder>/ serves that folder
- both endpoints are loopback-only; node_modules/.git/.hermes blocked and
  path traversal rejected; WORKSPACE_DIR=none disables the feature
- home: new 'โปรเจคในเครื่อง' list (2 columns >=700px) that hides itself when
  the API is absent, so the live static site never shows fake rows
- SW skips /projects/* so other projects' files are never cached; version 2026.11.19.5
- docs: README/AGENTS/DESIGN cover the endpoints, the loopback rule and the layout
2026-11-20 14:26:54 +07:00

246 lines
11 KiB
JavaScript

/* views/home.js — หน้าแรก: ข้อความต้อนรับ + ค้นหา + การ์ดโปรเจค */
import { escapeHtml, hostOf, icon, normalizeUrl } from "../ui.js";
import { initReveal, splitWordsHtml } from "../motion.js";
import { fetchLocalProjects } from "../data.js";
const TH_DATE = new Intl.DateTimeFormat("th-TH", { day: "numeric", month: "short", year: "numeric" });
function formatDate(iso) {
try {
return TH_DATE.format(new Date(iso));
} catch {
return "";
}
}
/** สุ่มลำดับด้วย Fisher–Yates แล้วหยิบมา count ใบ (ไม่ซ้ำ) */
function sampleRandom(list, count) {
const pool = [...list];
for (let i = pool.length - 1; i > 0; i -= 1) {
const j = Math.floor(Math.random() * (i + 1));
[pool[i], pool[j]] = [pool[j], pool[i]];
}
return pool.slice(0, Math.max(0, count));
}
export function homeView(ctx) {
let root = null;
let query = "";
let inputEl = null;
let gridEl = null;
let countEl = null;
function filtered() {
const items = ctx.data.projects;
const q = query.trim().toLowerCase();
if (!q) return items;
return items.filter((p) => {
const haystack = [p.project_name, p.project_description, hostOf(p.project_url), p.project_url]
.join(" ")
.toLowerCase();
return haystack.includes(q);
});
}
function cardHtml(project, index = 0) {
const url = normalizeUrl(project.project_url);
const host = hostOf(project.project_url);
const name = escapeHtml(project.project_name || "ไม่มีชื่อโปรเจค");
const desc = escapeHtml(project.project_description || "ยังไม่มีคำอธิบาย");
const nameHtml = url
? `<a href="${escapeHtml(url)}" target="_blank" rel="noopener noreferrer">${name}</a>`
: `<span>${name}</span>`;
return `
<article class="project-card" data-reveal="pop" style="--fx-delay:${(index % 3) * 95}ms">
<div class="project-card__top">
<span class="project-card__icon" aria-hidden="true">${icon(project.icon)}</span>
<div class="project-card__heading">
<h3 class="project-card__name">${nameHtml}</h3>
<p class="project-card__desc">${desc}</p>
</div>
</div>
<div class="project-card__foot">
<span class="project-card__host">${escapeHtml(host || "ยังไม่ใส่ลิงก์")}</span>
<span class="project-card__go">เปิดเว็บไซต์ ${icon("arrowRight")}</span>
</div>
</article>`;
}
function renderGrid() {
if (!gridEl) return;
const items = filtered();
if (countEl) {
countEl.textContent =
query.trim().length > 0
? `พบ ${items.length} จาก ${ctx.data.projects.length} โปรเจค`
: `${ctx.data.projects.length} โปรเจค`;
}
if (items.length === 0) {
const isSearch = query.trim().length > 0;
gridEl.className = "";
gridEl.innerHTML = `
<div class="empty">
<span class="empty__icon" aria-hidden="true">${icon(isSearch ? "search" : "folder")}</span>
<p class="empty__title">${isSearch ? "ไม่พบโปรเจคที่ค้นหา" : "ยังไม่มีโปรเจคในรายการ"}</p>
<p class="empty__text">${
isSearch
? "ลองพิมพ์คำอื่น หรือล้างคำค้นหาเพื่อดูทั้งหมด"
: "เข้าไปเพิ่มโปรเจคได้ที่หน้า “จัดการข้อมูล”"
}</p>
</div>`;
return;
}
gridEl.className = "project-grid";
gridEl.innerHTML = items.map((project, index) => cardHtml(project, index)).join("");
// การ์ดชุดใหม่หลังกรองต้องถูก observe/เปิดให้เห็นด้วย ไม่งั้นจะค้างที่ opacity 0
initReveal(gridEl);
}
function localRowHtml(item) {
const folder = escapeHtml(item.folder);
const entry = escapeHtml(item.entry || "index.html");
const title = escapeHtml(item.title || item.folder);
const date = formatDate(item.updated_at);
return `
<li class="local-row">
<a class="local-row__link" href="${escapeHtml(item.url || "#")}" target="_blank" rel="noopener noreferrer">
<span class="local-row__icon" aria-hidden="true">${icon("folder")}</span>
<span class="local-row__body">
<span class="local-row__title">${title}</span>
<span class="local-row__meta"><code>${folder}</code> · ${entry}${
date ? ` · แก้ล่าสุด ${date}` : ""
}</span>
</span>
<span class="local-row__go"><span class="local-row__go-text">เปิดหน้าแรก</span>${icon("arrowRight")}</span>
</a>
</li>`;
}
/** โปรเจคในเครื่อง — มีเฉพาะเมื่อเปิดผ่านเซิร์ฟเวอร์หลังบ้าน (ไม่ขึ้นบนเว็บจริง) */
async function loadLocalProjects() {
const box = root?.querySelector("[data-local]");
const list = root?.querySelector("[data-local-list]");
const countEl = root?.querySelector("[data-local-count]");
if (!box || !list) return;
const result = await fetchLocalProjects();
if (!root || !result.projects.length) return;
if (countEl) countEl.textContent = `${result.projects.length} โฟลเดอร์`;
list.innerHTML = result.projects.map(localRowHtml).join("");
box.hidden = false;
initReveal(box);
}
return {
mount(container) {
root = container;
const site = ctx.data.site;
const total = ctx.data.projects.length;
const withEmail = ctx.data.projects.filter((p) => p.contact_email).length;
root.innerHTML = `
<div class="wrap">
<section class="hero" aria-labelledby="hero-title">
<div class="hero__ambience" aria-hidden="true">
<span class="hero__orb hero__orb--a"></span>
<span class="hero__orb hero__orb--b"></span>
</div>
<div class="hero__inner">
<p class="hero__eyebrow">${icon("spark")}<span>${escapeHtml(
site.site_title,
)}</span></p>
<p class="hero__wordmark" aria-label="hobbyproject"><span class="wm-dark">hobbyp</span><span class="wm-light">ro</span><span class="wm-dark">j</span><span class="wm-light">ect</span></p>
<h1 class="hero__title" id="hero-title">${splitWordsHtml(
site.welcome_message,
escapeHtml,
)}</h1>
<p class="hero__lead">รวมเว็บที่ทำเองไว้ในที่เดียว เลือกดูได้ตามใจ ไม่มีโฆษณา ไม่มีล็อกอิน</p>
<form class="searchbar" role="search" autocomplete="off">
<span class="searchbar__icon" aria-hidden="true">${icon("search")}</span>
<label class="visually-hidden" for="project-search">ค้นหาโปรเจค</label>
<input id="project-search" type="search" name="q"
placeholder="ค้นหาชื่อโปรเจค หรือคำอธิบาย" enterkeyhint="search" />
<button class="searchbar__clear" type="button" aria-label="ล้างคำค้นหา" hidden>
${icon("close")}
</button>
</form>
<div class="hero__meta">
<span>${icon("folder")}<span data-countup="${total}">${total}</span> โปรเจค</span>
${
withEmail
? `<span>${icon("list")}${withEmail} โปรเจคมีอีเมลติดต่อ</span>`
: ""
}
</div>
</div>
<div class="hero__deco" aria-hidden="true">
${sampleRandom(ctx.data.projects, 3)
.map(
(project, index) => `
<div class="hero__deco-card" style="--i:${index}">
<span class="hero__deco-icon">${icon(project.icon)}</span>
<span class="hero__deco-name">${escapeHtml(project.project_name)}</span>
</div>`,
)
.join("")}
</div>
</section>
<div class="section-head" data-reveal="fade">
<div>
<h2 class="section-head__title" id="projects-title">รายชื่อโปรเจค</h2>
<p class="section-head__sub">กดที่การ์ดเพื่อเปิดเว็บนั้นในแท็บใหม่</p>
</div>
<p class="section-head__count" data-count></p>
</div>
<div data-grid aria-labelledby="projects-title"></div>
<section class="local-section" data-local hidden aria-labelledby="local-title">
<div class="section-head" data-reveal="fade">
<div>
<h2 class="section-head__title" id="local-title">โปรเจคในเครื่อง</h2>
<p class="section-head__sub">
เปิดหน้าแรกของแต่ละโฟลเดอร์ในเครื่องนี้โดยตรง — เห็นเฉพาะเครื่องที่รันเซิร์ฟเวอร์ ไม่ขึ้นบนเว็บจริง
</p>
</div>
<p class="section-head__count" data-local-count></p>
</div>
<ul class="local-list" data-local-list></ul>
</section>
</div>`;
gridEl = root.querySelector("[data-grid]");
countEl = root.querySelector("[data-count]");
inputEl = root.querySelector("#project-search");
const clearBtn = root.querySelector(".searchbar__clear");
const form = root.querySelector(".searchbar");
form.addEventListener("submit", (event) => {
event.preventDefault();
inputEl.blur();
});
inputEl.addEventListener("input", () => {
query = inputEl.value;
clearBtn.hidden = query.length === 0;
renderGrid();
});
clearBtn.addEventListener("click", () => {
inputEl.value = "";
query = "";
clearBtn.hidden = true;
inputEl.focus();
renderGrid();
});
renderGrid();
loadLocalProjects();
},
destroy() {
root = null;
inputEl = null;
gridEl = null;
countEl = null;
},
};
}