feat: hobby project landing page (PWA + SPA) with admin backoffice
- landing page: welcome hero, live search, project card grid (10 projects from owner list) - warm brown/white theme (fastwork-inspired layout), embedded K2D Thai font - PWA: manifest, PIL-generated icons, versioned service worker with auto-update - backoffice at #/admin: edit name/description/url, reorder, delete, publish - server/publish.mjs: zero-dependency static server + PUT api/projects with validation, JSON write, git commit and push; loopback-only unless ADMIN_TOKEN - docs: README, DESIGN.md (color/type contract), AGENTS.md
This commit is contained in:
1 parent
e55b193644
commit
c00bc62272
35 files changed
+3432
-2
No files matched your search
+209
@@ -0,0 +1,209 @@
|
||||
/* data.js — ชั้นข้อมูล: อ่าน data/projects.json, ฉบับร่าง, เผยแพร่ผ่าน API ของเซิร์ฟเวอร์ */
|
||||
|
||||
export const DATA_URL = "data/projects.json";
|
||||
export const API_URL = "api/projects";
|
||||
const CACHE_KEY = "hobbypj.projects.cache.v1";
|
||||
const DRAFT_KEY = "hobbypj.projects.draft.v1";
|
||||
const TOKEN_KEY = "hobbypj.publish.token.v1";
|
||||
|
||||
export const DEFAULT_SITE = {
|
||||
site_title: "โปรเจคอดิเรกของเรา",
|
||||
welcome_message:
|
||||
"สวัสดี คนที่เข้ามาดูโปรเจคอดิเรกของเรา เลือกโปรเจคที่อยากดู ขอบคุณครับ",
|
||||
contact_email: "",
|
||||
ssl_note: "ทุกเว็บใช้ SSL ฟรี ต่ออายุอัตโนมัติ",
|
||||
};
|
||||
|
||||
const DEFAULT_PROJECT = {
|
||||
project_id: "",
|
||||
project_name: "",
|
||||
project_description: "",
|
||||
project_url: "",
|
||||
contact_email: "",
|
||||
icon: "folder",
|
||||
};
|
||||
|
||||
/** ทำข้อมูลให้อยู่ในรูปมาตรฐานเสมอ (คีย์ครบ อ่านง่าย) */
|
||||
export function normalizeData(raw) {
|
||||
const source = raw && typeof raw === "object" ? raw : {};
|
||||
const site = { ...DEFAULT_SITE, ...(source.site && typeof source.site === "object" ? source.site : {}) };
|
||||
const list = Array.isArray(source.projects) ? source.projects : [];
|
||||
const seen = new Set();
|
||||
const projects = list.map((item, index) => {
|
||||
const row = { ...DEFAULT_PROJECT, ...(item && typeof item === "object" ? item : {}) };
|
||||
const out = {
|
||||
project_id: String(row.project_id || "").trim() || `project-${index + 1}`,
|
||||
project_name: String(row.project_name || "").trim(),
|
||||
project_description: String(row.project_description || "").trim(),
|
||||
project_url: String(row.project_url || "").trim(),
|
||||
contact_email: String(row.contact_email || "").trim(),
|
||||
icon: String(row.icon || "folder").trim() || "folder",
|
||||
};
|
||||
if (seen.has(out.project_id)) out.project_id = `${out.project_id}-${index + 1}`;
|
||||
seen.add(out.project_id);
|
||||
return out;
|
||||
});
|
||||
return {
|
||||
schema_version: Number(source.schema_version) || 1,
|
||||
updated_at: source.updated_at || "",
|
||||
owner_name: String(source.owner_name || ""),
|
||||
site,
|
||||
projects,
|
||||
};
|
||||
}
|
||||
|
||||
/** shape ที่ส่งขึ้นเซิร์ฟเวอร์ — คีย์เรียงคงที่และอ่านง่ายแบบมนุษย์ */
|
||||
export function toPublishPayload(data) {
|
||||
return {
|
||||
schema_version: 1,
|
||||
updated_at: new Date().toISOString(),
|
||||
owner_name: data.owner_name || "",
|
||||
site: {
|
||||
site_title: data.site.site_title,
|
||||
welcome_message: data.site.welcome_message,
|
||||
contact_email: data.site.contact_email,
|
||||
ssl_note: data.site.ssl_note,
|
||||
},
|
||||
projects: data.projects.map((p) => ({
|
||||
project_id: p.project_id,
|
||||
project_name: p.project_name,
|
||||
project_description: p.project_description,
|
||||
project_url: p.project_url,
|
||||
contact_email: p.contact_email,
|
||||
icon: p.icon,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
export function createProject(index = 0) {
|
||||
return { ...DEFAULT_PROJECT, project_id: `project-${Date.now()}-${index}` };
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- loading */
|
||||
export async function loadProjects() {
|
||||
try {
|
||||
const res = await fetch(DATA_URL, { cache: "no-store" });
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
const json = await res.json();
|
||||
const data = normalizeData(json);
|
||||
try {
|
||||
localStorage.setItem(CACHE_KEY, JSON.stringify(data));
|
||||
} catch {
|
||||
/* โควต้าเต็ม — ไม่เป็นไร ใช้ข้อมูลออนไลน์ได้ */
|
||||
}
|
||||
return { data, source: "server" };
|
||||
} catch (error) {
|
||||
const cached = readCache();
|
||||
if (cached) return { data: cached, source: "cache", error };
|
||||
return { data: normalizeData({}), source: "empty", error };
|
||||
}
|
||||
}
|
||||
|
||||
function readCache() {
|
||||
try {
|
||||
const raw = localStorage.getItem(CACHE_KEY);
|
||||
if (!raw) return null;
|
||||
return normalizeData(JSON.parse(raw));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ draft */
|
||||
export function readDraft() {
|
||||
try {
|
||||
const raw = localStorage.getItem(DRAFT_KEY);
|
||||
if (!raw) return null;
|
||||
return normalizeData(JSON.parse(raw));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function writeDraft(data) {
|
||||
try {
|
||||
localStorage.setItem(DRAFT_KEY, JSON.stringify(data));
|
||||
} catch {
|
||||
/* เก็บไม่ได้ก็ไม่ทำให้หน้าเจ๊ง */
|
||||
}
|
||||
}
|
||||
|
||||
export function clearDraft() {
|
||||
try {
|
||||
localStorage.removeItem(DRAFT_KEY);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ token */
|
||||
export function readToken() {
|
||||
try {
|
||||
return localStorage.getItem(TOKEN_KEY) || "";
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
export function writeToken(token) {
|
||||
try {
|
||||
if (token) localStorage.setItem(TOKEN_KEY, token);
|
||||
else localStorage.removeItem(TOKEN_KEY);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- publish */
|
||||
export async function publishProjects(data) {
|
||||
const res = await fetch(API_URL, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
"x-admin-token": readToken(),
|
||||
},
|
||||
cache: "no-store",
|
||||
body: JSON.stringify(toPublishPayload(data)),
|
||||
});
|
||||
const text = await res.text();
|
||||
let body = null;
|
||||
try {
|
||||
body = text ? JSON.parse(text) : null;
|
||||
} catch {
|
||||
body = null;
|
||||
}
|
||||
if (!res.ok) {
|
||||
const message = body?.error || `HTTP ${res.status}`;
|
||||
const error = new Error(message);
|
||||
error.status = res.status;
|
||||
throw error;
|
||||
}
|
||||
return body || { ok: true };
|
||||
}
|
||||
|
||||
/** มี API ของเซิร์ฟเวอร์ให้เผยแพร่หรือไม่ (ใช้แสดงสถานะในหน้าจัดการ) */
|
||||
export async function probePublishApi() {
|
||||
try {
|
||||
const res = await fetch(API_URL, { method: "GET", cache: "no-store" });
|
||||
if (!res.ok) return { available: false, status: res.status, message: `HTTP ${res.status}` };
|
||||
const body = await res.json();
|
||||
return { available: true, writable: Boolean(body?.writable) };
|
||||
} catch (error) {
|
||||
return { available: false, status: 0, message: "เชื่อมต่อเซิร์ฟเวอร์ไม่ได้" };
|
||||
}
|
||||
}
|
||||
|
||||
export function downloadJson(data) {
|
||||
const payload = toPublishPayload(data);
|
||||
const blob = new Blob([`${JSON.stringify(payload, null, 2)}\n`], {
|
||||
type: "application/json;charset=utf-8",
|
||||
});
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = "projects.json";
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
setTimeout(() => URL.revokeObjectURL(url), 2000);
|
||||
}
|
||||
Reference in new issue
Block a user