refactor(ids): generate content id unions from the structure documents

scripts/generate-content-types.ts writes the gitignored src/types/content-ids.ts (id unions + project category values) from the four structure documents; wired as generate:ids / predev / prebuild / prepare. src/types/index.ts re-exports them type-only and keeps only the code-owned unions.

Edit mode now writes JSON only: the v4 union leg is deleted (saveIdUnions, diffIdsWithUnions, applyUnionChanges, readUnionBlock, the EditBar union card, edit.union.* labels). Validator rule 7 requires a label for every category a project uses, in both ui/projects.json documents.

Tests: smoke-draft.ts 11/11 (rename re-sluging + category-label rule replace checks 9-12); new scripts/smoke-generated-ids.ts 7/7. Verified: pnpm build (chunk names unchanged, no runtime reference to the generated module), lint with no new warnings, validate:content, resume HTML byte-identical before/after with identical documents.

Docs: spec-v5-generated-content-ids.md added; spec-v4-live-edit.md §3.1 marked superseded + §12 row 23; AGENTS.md and README.md updated.
This commit is contained in:
dong.nguyen committed 2026-10-24 13:16:18 +07:00
1 parent 1a1fc14d0e
commit a3662a9521
18 files changed
+732 -357

No files matched your search

+3 -5
View File
@@ -24,7 +24,7 @@ type EditBarProps = {
saving: boolean;
saveStopped: boolean;
saveResults: DocSaveResult[];
unionNote: 'ok' | 'failed' | null;
labelsUnavailable: boolean;
draftRestored: boolean;
branch: string;
getConflictFields: (id: DraftDocId) => ConflictField[];
@@ -56,7 +56,7 @@ export function EditBar({
saving,
saveStopped,
saveResults,
unionNote,
labelsUnavailable,
draftRestored,
branch,
getConflictFields,
@@ -248,9 +248,7 @@ export function EditBar({
</ul>
)}
{unionNote && (
<p className="edit-bar-note">{unionNote === 'ok' ? s.unionNote : s.unionFailed}</p>
)}
{labelsUnavailable && <p className="edit-bar-note">{s.labelsUnavailable}</p>}
{allWritten && !dirty && (
<p className="edit-bar-note">
{branch === DEFAULT_BRANCH ? s.deployQueued : s.mergeHint}
+34 -31
View File
@@ -3,7 +3,7 @@
// master documents, holds the draft, and re-wraps the tree in an
// I18nProvider override — the seven routes ARE the preview. Visitors never
// mount this component; its code is never in the entry chunk (§7 P3.4).
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import type { ReactNode } from 'react';
import { useLocation, useNavigate } from 'react-router';
import { useLanguage } from '@/hooks/useLanguage';
@@ -13,12 +13,9 @@ import type { ResolvedContent } from '@/i18n';
import {
DRAFT_DOCS,
DraftStore,
TYPES_FILE,
bundledBase,
fetchDoc,
fetchTextFile,
saveDraft,
saveIdUnions,
recordForTitlePath,
} from '@/i18n/draft';
import type { DocBase, DocSaveResult, DraftDocId } from '@/i18n/draft';
@@ -34,11 +31,26 @@ import {
} from '@/i18n/edit-mode';
import { EditContext } from '@/hooks/useEdit';
import type { EditApi } from '@/hooks/useEdit';
import type { ContentIssue } from '@/i18n/validate';
import type { CategoryLabels, ContentIssue } from '@/i18n/validate';
import { EditBar } from './EditBar';
type Phase = 'authenticating' | 'loading' | 'ready' | 'offline';
// The category labels are read-only UI documents (spec-v4-data-organization §4);
// rule 7 compares a project's category against them on the branch being edited
// (spec-v5 §5.4), so they are fetched beside the draft documents instead of
// taken from this bundle.
const LABEL_FILES: Record<'vi' | 'en', string> = {
vi: 'src/i18n/locales/vi/ui/projects.json',
en: 'src/i18n/locales/en/ui/projects.json',
};
const categoryMap = (data: unknown): Record<string, unknown> => {
if (!data || typeof data !== 'object' || !('categories' in data)) return {};
const categories = data.categories;
if (typeof categories !== 'object' || categories === null || Array.isArray(categories)) return {};
return Object.fromEntries(Object.entries(categories));
};
export default function EditModeShell({ children }: { children: ReactNode }) {
const navigate = useNavigate();
const location = useLocation();
@@ -48,7 +60,6 @@ export default function EditModeShell({ children }: { children: ReactNode }) {
// render reads it legally, and `revision` is the invalidation counter that
// re-derives everything the draft feeds to the UI.
const [store, setStore] = useState<DraftStore | null>(null);
const typesRef = useRef<{ source: string; sha: string } | null>(null);
const [phase, setPhase] = useState<Phase>('authenticating');
const [branch, setBranch] = useState<string>(DEFAULT_BRANCH);
const [resolved, setResolved] = useState<Record<'vi' | 'en', ResolvedContent> | null>(null);
@@ -60,7 +71,7 @@ export default function EditModeShell({ children }: { children: ReactNode }) {
const [saveResults, setSaveResults] = useState<DocSaveResult[]>([]);
const [saving, setSaving] = useState(false);
const [saveStopped, setSaveStopped] = useState(false);
const [unionNote, setUnionNote] = useState<'ok' | 'failed' | null>(null);
const [labelsUnavailable, setLabelsUnavailable] = useState(false);
const stripEditQuery = useCallback(() => {
const params = new URLSearchParams(location.search);
@@ -123,7 +134,21 @@ export default function EditModeShell({ children }: { children: ReactNode }) {
}
if (!bases) bases = bundledBase();
const created = new DraftStore(bases);
// Rule 7's inputs. A failed read leaves the labels null: the save bar
// says the label check could not run, and the rule is skipped rather
// than guessed at from a bundle that may predate the branch.
let labels: CategoryLabels | null = null;
try {
const [viLabels, enLabels] = await Promise.all([
fetchDoc(LABEL_FILES.vi, branch),
fetchDoc(LABEL_FILES.en, branch),
]);
labels = { vi: categoryMap(viLabels.data), en: categoryMap(enLabels.data) };
} catch {
setLabelsUnavailable(true);
}
const created = new DraftStore(bases, labels);
const stored = sessionStorage.getItem(DRAFT_STORAGE);
if (stored) {
@@ -136,13 +161,6 @@ export default function EditModeShell({ children }: { children: ReactNode }) {
}
}
try {
const file = await fetchTextFile(TYPES_FILE, branch);
typesRef.current = { source: file.text, sha: file.sha };
} catch {
typesRef.current = null;
}
if (cancelled) return;
setStore(created);
setPhase(offline ? 'offline' : 'ready');
@@ -271,7 +289,6 @@ export default function EditModeShell({ children }: { children: ReactNode }) {
}
setSaving(true);
setSaveStopped(false);
setUnionNote(null);
setSaveResults([]);
const { results, stoppedEarly } = await saveDraft(store, (result) => {
setSaveResults((prev) => [...prev, result]);
@@ -280,20 +297,6 @@ export default function EditModeShell({ children }: { children: ReactNode }) {
const written = new Set(results.filter((r) => r.status === 'written').map((r) => r.id));
const conflicted = results.filter((r) => r.status === 'conflict').map((r) => r.id);
setConflicts((prev) => [...new Set([...prev.filter((id) => !written.has(id)), ...conflicted])]);
// The third leg of §3.1: bring the unions to the ids whose documents
// landed — appended when a record was added, dropped when one was deleted.
const types = typesRef.current;
if (types) {
const union = await saveIdUnions(store, types, branch);
if (union) {
if (union.failed !== undefined) setUnionNote('failed');
else {
typesRef.current = { source: union.source!, sha: union.sha! };
setUnionNote('ok');
}
}
}
setSaving(false);
refresh(store);
}, [store, phase, refresh, branch]);
@@ -394,7 +397,7 @@ export default function EditModeShell({ children }: { children: ReactNode }) {
saving={saving}
saveStopped={saveStopped}
saveResults={saveResults}
unionNote={unionNote}
labelsUnavailable={labelsUnavailable}
draftRestored={draftRestored}
branch={branch}
getConflictFields={(id) => store.conflictFields(id)}
+10 -186
View File
@@ -16,7 +16,7 @@ import profileJson from '../data/profile.json';
import projectsJson from '../data/projects.json';
import skillsJson from '../data/skills.json';
import { collectContentIssues } from './validate';
import type { ContentIssue, ContentStructures } from './validate';
import type { CategoryLabels, ContentIssue, ContentStructures } from './validate';
import type { ContentEn, ContentVi } from './dict-types';
import type { Language } from '../types/index';
@@ -98,7 +98,6 @@ export function entityOfDoc(id: DraftDocId): TextEntity | null {
}
export const PROXY_BASE = '/api/content/';
export const TYPES_FILE = 'src/types/index.ts';
export function proxyUrl(file: string, ref: string): string {
return `${PROXY_BASE}${file}?ref=${encodeURIComponent(ref)}`;
@@ -228,13 +227,6 @@ export async function fetchDoc(file: string, branch: string): Promise<DocBase> {
};
}
export async function fetchTextFile(file: string, branch: string): Promise<{ text: string; sha: string }> {
const res = await fetch(proxyUrl(file, branch));
if (!res.ok) throw new Error(`GET ${file}: ${res.status}`);
const json = (await res.json()) as { content?: string; sha?: string };
return { text: decodeBase64(json.content ?? ''), sha: String(json.sha ?? '') };
}
export type PutResult =
| { ok: true; sha: string }
| { ok: false; status: number; message: string };
@@ -287,16 +279,6 @@ export function putDoc(
return putBase64(file, encodeBase64(`${JSON.stringify(data, null, 2)}\n`), message, lastCommitSha, branch);
}
export function putTextFile(
file: string,
text: string,
message: string,
lastCommitSha: string,
branch: string,
): Promise<PutResult> {
return putBase64(file, encodeBase64(text), message, lastCommitSha, branch);
}
// Offline fallback (§5.1): the bundled build-time documents, flagged stale in
// the UI, save disabled — the draft is never initialized from a bundle that
// might be older than master when the proxy is reachable.
@@ -317,115 +299,6 @@ export function bundledBase(): Record<DraftDocId, DocBase> {
};
}
// --- id unions (§3.1) ------------------------------------------------------------
// Adding a content item is three edits: a union member, a structure record,
// its Vietnamese text. Deleting one is the mirror image: the record and its
// text go, and the union member must go with them. The editor does every leg
// — once the content documents land, saveIdUnions brings each union in
// src/types/index.ts to exactly the ids its document carries.
const UNION_SPECS: ReadonlyArray<{ union: string; ids: (s: ContentStructures) => string[] }> = [
{ union: 'ProjectId', ids: (s) => s.projects.map((p) => p.id) },
{ union: 'ExperienceId', ids: (s) => s.experience.map((e) => e.id) },
{ union: 'SkillGroupId', ids: (s) => s.skills.map((g) => g.id) },
{ union: 'PhilosophyId', ids: (s) => s.profile.philosophy.map((p) => p.id) },
];
// A union is read as a block — `export type X =` followed by its ` | 'id'`
// member lines — never as a substring search: an id slug can legitimately
// appear elsewhere in the same file (ProjectCategory carries slugs that also
// read as project ids), and a substring test would silently skip that leg. An
// emptied union (`never`, the state an emptied document leaves) reads as an
// empty member list, so a record added on top of it is appended back. Any
// other shape (a one-line union) is reported as unpatchable, never corrupted.
type UnionBlock = { union: string; members: string[]; from: number; to: number };
const MEMBER_LINE = /^[ \t]*\| '([^']+)'[ \t]*;?[ \t]*\r?\n?$/;
const CLOSING_LINE = /^[ \t]*;[ \t]*\r?\n?$/;
// One line of the source including its terminator, or null at end of file.
function nextLine(source: string, from: number): { text: string; end: number } | null {
if (from >= source.length) return null;
const at = source.indexOf('\n', from);
const end = at < 0 ? source.length : at + 1;
return { text: source.slice(from, end), end };
}
// `from`/`to` are the exact span of the declaration, so nothing else in the
// file is ever rewritten.
function readUnionBlock(source: string, union: string): UnionBlock | null {
const never = new RegExp(`^export type ${union} = never;\\r?\\n?`, 'm').exec(source);
if (never) return { union, members: [], from: never.index, to: never.index + never[0].length };
const header = new RegExp(`^export type ${union} =\\r?\\n`, 'm').exec(source);
if (!header) return null;
const from = header.index;
let pos = from + header[0].length;
const members: string[] = [];
for (;;) {
const line = nextLine(source, pos);
if (!line) break;
const member = MEMBER_LINE.exec(line.text);
if (!member) break;
members.push(member[1]);
pos = line.end;
}
if (members.length === 0) return null;
const closing = nextLine(source, pos);
if (closing && CLOSING_LINE.test(closing.text)) pos = closing.end;
return { union, members, from, to: pos };
}
// An emptied union is `never`, which keeps `Record<never, T>` valid against an
// empty document.
function renderUnion(union: string, members: readonly string[], eol: string): string {
if (members.length === 0) return `export type ${union} = never;${eol}`;
const body = members.map((id) => ` | '${id}'`).join(eol);
return `export type ${union} =${eol}${body};${eol}`;
}
// One pass per union: surviving members keep their order, removed ids are
// dropped, added ids follow the document order. Null when a union's shape is
// not the one the editor writes.
export function applyUnionChanges(
source: string,
changes: { union: string; add: string[]; remove: string[] }[],
): string | null {
// git stores LF; a Windows checkout reads CRLF. A patch keeps the line
// ending the file already uses, so the commit the owner reviews is one line.
const eol = source.includes('\r\n') ? '\r\n' : '\n';
let out = source;
for (const change of changes) {
const block = readUnionBlock(out, change.union);
if (!block) return null;
const removed = new Set(change.remove);
const members = block.members.filter((id) => !removed.has(id));
for (const id of change.add) if (!members.includes(id)) members.push(id);
out = `${out.slice(0, block.from)}${renderUnion(change.union, members, eol)}${out.slice(block.to)}`;
}
return out;
}
// Ids whose documents have landed but whose union does not match them:
// missing members to append, stale members to drop. committedStructures() reads
// the post-PUT base, so a document that has not landed keeps the branch's ids
// and never reads as stale.
export function diffIdsWithUnions(
store: DraftStore,
typesSource: string,
): { added: { union: string; id: string }[]; removed: { union: string; id: string }[] } {
const structures = store.committedStructures();
const added: { union: string; id: string }[] = [];
const removed: { union: string; id: string }[] = [];
for (const spec of UNION_SPECS) {
const block = readUnionBlock(typesSource, spec.union);
if (!block) continue;
const live = new Set(spec.ids(structures));
for (const id of live) if (!block.members.includes(id)) added.push({ union: spec.union, id });
for (const id of block.members) if (!live.has(id)) removed.push({ union: spec.union, id });
}
return { added, removed };
}
// --- the store ---------------------------------------------------------------------
export type SavedDraft = {
@@ -502,7 +375,14 @@ export class DraftStore {
// is published and fixed (E3).
private created = new Set<string>();
constructor(bases: Record<DraftDocId, DocBase>) {
// Rule 7 needs the category labels of the branch being edited. They are
// read-only documents, fetched by EditModeShell beside the rest of the
// session (spec-v5 §5.4); `null` is the offline state — the rule is then
// skipped, never guessed at.
private categoryLabels: CategoryLabels | null;
constructor(bases: Record<DraftDocId, DocBase>, categoryLabels: CategoryLabels | null) {
this.categoryLabels = categoryLabels;
for (const meta of DRAFT_DOCS) {
this.base.set(meta.id, bases[meta.id]);
this.origin.set(meta.id, bases[meta.id].data);
@@ -549,22 +429,6 @@ export class DraftStore {
} as unknown as ContentStructures;
}
baseDataOf(id: DraftDocId): unknown {
return this.base.get(id)!.data;
}
// Structures as they would exist in master after the already-written PUTs —
// the union patch only covers ids whose documents actually landed.
committedStructures(): ContentStructures {
const baseData = (id: DraftDocId) => this.base.get(id)!.data;
return {
profile: baseData('profile'),
skills: baseData('skills'),
experience: baseData('experience'),
projects: baseData('projects'),
} as unknown as ContentStructures;
}
draftTexts(): { vi: ContentVi; en: ContentEn } {
return {
vi: {
@@ -584,7 +448,7 @@ export class DraftStore {
validate(): ContentIssue[] {
const texts = this.draftTexts();
return collectContentIssues(this.draftStructures(), texts.vi, texts.en);
return collectContentIssues(this.draftStructures(), texts.vi, texts.en, this.categoryLabels);
}
// --- text documents -----------------------------------------------------------
@@ -1147,43 +1011,3 @@ export async function saveDraft(
}
return { results, stoppedEarly: false };
}
// The third leg of §3.1: once the documents that add or drop ids have landed,
// bring their unions in src/types/index.ts to exactly those ids so the next CI
// build compiles. Returns the updated source/sha, or null when nothing needs
// changing (an unpatchable shape or a rejected PUT is reported to the EditBar,
// and the owner finishes that edit in Gitea).
export async function saveIdUnions(
store: DraftStore,
types: { source: string; sha: string },
branch: string,
): Promise<{
added: { union: string; id: string }[];
removed: { union: string; id: string }[];
source?: string;
sha?: string;
failed?: number;
} | null> {
const { added, removed } = diffIdsWithUnions(store, types.source);
if (added.length === 0 && removed.length === 0) return null;
const grouped = new Map<string, { add: string[]; remove: string[] }>();
const bucket = (union: string) => {
const existing = grouped.get(union);
if (existing) return existing;
const fresh = { add: [] as string[], remove: [] as string[] };
grouped.set(union, fresh);
return fresh;
};
for (const { union, id } of added) bucket(union).add.push(id);
for (const { union, id } of removed) bucket(union).remove.push(id);
const source = applyUnionChanges(
types.source,
[...grouped].map(([union, change]) => ({ union, ...change })),
);
if (source === null) return { added, removed, failed: 0 };
const summary = [...added.map((a) => `+${a.id}`), ...removed.map((r) => `-${r.id}`)].join(', ');
const message = `content: update id union members (${summary})`;
const res = await putTextFile(TYPES_FILE, source, message, types.sha, branch);
if (!res.ok) return { added, removed, failed: res.status };
return { added, removed, source, sha: res.sha };
}
+5 -1
View File
@@ -122,7 +122,11 @@ const structures = {
// Cross-document rules run at module load: a malformed content document fails
// the build here, in CI, and in the browser — never silently half-renders.
validateContent(structures, contentVi, contentEn);
// Rule 7 reads the category labels straight out of the UI tree built above.
validateContent(structures, contentVi, contentEn, {
vi: uiVi.projects.categories,
en: uiEn.projects.categories,
});
// Missing en fields fall back to vi per field; arrays are atomic — a whole
// array is replaced or inherited, never merged item by item (§3.2).
+1 -2
View File
@@ -66,8 +66,7 @@
"mergeHint": "written to the branch — merge into master to deploy",
"offline": "offline — showing bundled content, save disabled",
"draftRestored": "draft restored from this session",
"unionNote": "id unions in src/types/index.ts updated to match the saved content",
"unionFailed": "could not update src/types/index.ts — finish that edit in Gitea",
"labelsUnavailable": "category labels could not be read from this branch — the label check was skipped",
"appearsHome": "home",
"appearsResume": "resume PDF",
"appearsOn": "appears on",
+1 -2
View File
@@ -66,8 +66,7 @@
"mergeHint": "đã ghi lên branch — merge vào master để deploy",
"offline": "ngoại tuyến — đang hiển thị nội dung đóng gói, không lưu được",
"draftRestored": "bản nháp đã khôi phục từ phiên này",
"unionNote": "các id union trong src/types/index.ts đã được cập nhật theo nội dung đã lưu",
"unionFailed": "không cập nhật được src/types/index.ts — hãy hoàn tất sửa trong Gitea",
"labelsUnavailable": "không đọc được nhãn danh mục từ nhánh này — bước kiểm tra nhãn đã được bỏ qua",
"appearsHome": "Trang chủ",
"appearsResume": "CV PDF",
"appearsOn": "xuất hiện tại",
+31 -1
View File
@@ -27,6 +27,12 @@ export type ContentStructures = {
projects: ProjectStructure[];
};
// Rule 7 reads the category labels, which live in the UI group files — a
// read-only part of the dictionary tree (spec-v4-data-organization §4). They
// are passed in as a slice rather than owned here: the load-time and CI
// callers have the whole tree, the edit session fetches only this group.
export type CategoryLabels = { vi: Record<string, unknown>; en: Record<string, unknown> };
const hasHtml = (value: string) => value.includes('<') || value.includes('>');
// Walks every string value of a text document, collecting the document paths
@@ -70,6 +76,7 @@ export function collectContentIssues(
structures: ContentStructures,
vi: ContentVi,
en: ContentEn,
categoryLabels: CategoryLabels | null,
): ContentIssue[] {
const issues: ContentIssue[] = [];
@@ -164,6 +171,28 @@ export function collectContentIssues(
}
}
// Rule 7: every category a project uses has a non-empty label in both
// languages (spec-v5 D4). The category union is generated from these very
// values, so the failure worth catching is not an unknown category but a
// badge that renders empty. `null` means the labels are not available to
// this caller — the edit session's read-only fetch failed — and the rule is
// skipped, never guessed at.
if (categoryLabels) {
const used = new Set(structures.projects.map((project) => project.category));
for (const lang of ['vi', 'en'] as const) {
const labels = categoryLabels[lang];
for (const category of used) {
const label = labels[category];
if (typeof label !== 'string' || label.trim() === '') {
issues.push({
path: `src/i18n/locales/${lang}/ui/projects.json: categories.${category}`,
message: 'category used by a project has no label in this language',
});
}
}
}
}
return issues;
}
@@ -171,8 +200,9 @@ export function validateContent(
structures: ContentStructures,
vi: ContentVi,
en: ContentEn,
categoryLabels: CategoryLabels | null,
): void {
const issues = collectContentIssues(structures, vi, en);
const issues = collectContentIssues(structures, vi, en, categoryLabels);
if (issues.length > 0) {
const detail = issues.map((i) => ` ${i.path}: ${i.message}`).join('\n');
throw new Error(`content validation failed (${issues.length} issue(s)):\n${detail}`);
+12 -37
View File
@@ -4,43 +4,18 @@ export type Palette = 'red' | 'blue';
export type SocialId = 'github' | 'linkedin' | 'email' | 'telegram';
export type ProjectCategory =
| "software-engineering"
| "logistics"
| "ai-ml"
| "computer-vision"
| "architecture"
| "experiment";
// Stable id unions (spec-v4 §3.1). Text dictionaries are type-safe only against
// closed unions: adding a content item means adding a union member, a structure
// record, and its Vietnamese text — tsc catches a missing text entry, the
// validator (src/i18n/validate.ts) catches id mismatches across documents.
// SkillGroupId and PhilosophyId are internal keys and never appear in URLs.
export type ProjectId =
| 'iot-dws-integration'
| 'awb-detection';
export type ExperienceId =
| 'open-to-work'
| 'relax-gap'
| 'lazada-senior-swe'
| 'nexts-founder'
| 'sap-senior-swe';
export type SkillGroupId =
| 'software-engineering'
| 'system-architecture'
| 'ai-ml'
| 'infrastructure'
| 'engineering-practices';
export type PhilosophyId =
| 'build-for-the-real-problem'
| 'think-in-systems'
| 'measure-impact'
| 'keep-learning'
| 'business-domain-knowledge-is-a-key-of-success';
// Content ids and the category values a project uses are generated from the
// structure documents by scripts/generate-content-types.ts (spec-v5): the union
// is exactly the document, so a record delete cannot leave a stale member and a
// text entry for it is a tsc error. Type-only re-export — erased at emit, no
// runtime import.
export type {
ExperienceId,
PhilosophyId,
ProjectCategory,
ProjectId,
SkillGroupId,
} from './content-ids';
// Page manifests (spec-v4-data-organization §5): one per route plus the
// synthetic resume consumer. A manifest claims which joined records a page