perf(edit): read only the documents the current route needs

Edit-mode document scope comes from the page manifests (src/i18n/edit-scope.ts): an entity's structure document and its two text documents are read as one unit, the category labels only where a project renders. A route change fetches only the still-missing documents and adopts them through DraftStore.adoptBase, which refuses a document the draft has already changed. A bundle-seeded baseline is never PUT over the branch: saveDraft reads the branch first and reports a conflict. Every manifest now claims profile.name, which the footer renders on every route.
This commit is contained in:
dong.nguyen committed 2026-10-24 15:24:08 +07:00
1 parent ee1d524bd0
commit 5c71b52d81
13 files changed
+320 -71

No files matched your search

+37
View File
@@ -928,6 +928,22 @@ export class DraftStore {
this.base.set(id, fresh);
}
// A document the session did not need on the entry route becomes needed
// after a navigation (spec-v4-live-edit §5.1): its live baseline replaces the
// bundled one. A document the draft already changed keeps its draft and its
// origin — a late read never silently replaces work.
adoptBase(id: DraftDocId, doc: DocBase): void {
if (this.working.has(id)) return;
this.base.set(id, doc);
this.origin.set(id, doc.data);
}
// Rule 7's category labels arrive with the documents of the route that
// renders a project, which a session may reach only after a navigation.
setCategoryLabels(labels: CategoryLabels | null): void {
this.categoryLabels = labels;
}
// Rebase (§5.6): a real three-way merge of origin → mine against origin →
// theirs. Paths only I touched replay onto the refreshed base; a path both
// sides changed differently is a conflict the store refuses to resolve —
@@ -985,6 +1001,27 @@ export async function saveDraft(
const meta = DRAFT_DOCS.find((d) => d.id === id)!;
const paths = store.changedPaths(id);
const message = `content: ${meta.file} (${paths.length} path${paths.length === 1 ? '' : 's'})`;
// A baseline that came from the bundle carries no blob sha. Gitea reads an
// empty sha as a create (422 "repository file already exists"), and PUTting
// a bundle-seeded draft over the branch would drop whatever the branch has.
// Read the branch first and hand the document to the conflict flow: rebase
// replays my paths onto the fresh base, take-theirs discards them. Reached
// when a document is edited before its scoped read lands, or when a
// restored draft touches a document this route never renders.
if (!store.shaOf(id)) {
try {
store.resetBase(id, await fetchDoc(meta.file, branch));
} catch {
const stalled: DocSaveResult = { id, status: 'failed' };
results.push(stalled);
onResult(stalled);
return { results, stoppedEarly: true };
}
const bundleSeeded: DocSaveResult = { id, status: 'conflict' };
results.push(bundleSeeded);
onResult(bundleSeeded);
continue;
}
const res = await putDoc(meta.file, store.dataOf(id), message, store.shaOf(id), branch);
if (res.ok) {
store.markWritten(id, res.sha);
+70
View File
@@ -0,0 +1,70 @@
// Which draft documents a route needs (spec-v4-live-edit §5.1). The answer
// comes from the page manifests, never from component code: a manifest names
// the joined records its page renders, and an entity owns its structure
// document plus its per-language text documents. Those are fetched as one unit
// — a half-loaded entity would make the cross-document validator compare a live
// document against a bundled one.
import { pageManifests, recordEntity } from '../data/pages';
import { DRAFT_DOCS, TEXT_ENTITIES, entityOfDoc } from './draft';
import type { DraftDocId, DraftDocMeta, TextEntity } from './draft';
import type { ContentEntityId, PageManifest } from '../types/index';
// Manifest entity names are the joined-record names (`skillGroups`); the
// document registry names files (`skills`). pages.ts owns one direction, this
// inverts it rather than stating a second mapping.
const DOC_ENTITY = Object.fromEntries(
TEXT_ENTITIES.map((textEntity) => [recordEntity(textEntity), textEntity]),
) as Record<ContentEntityId, TextEntity>;
// Manifest routes are the router's own strings. `/projects/:id` is the only
// pattern; the synthetic `resume` consumer has no route and never matches a
// location. A trailing slash is the same page to the router, so it is removed
// before matching (`/` stays `/`).
export function manifestForPath(pathname: string): PageManifest | null {
const path = pathname !== '/' ? pathname.replace(/\/+$/, '') : pathname;
for (const manifest of pageManifests) {
const route = manifest.route;
if (!route) continue;
if (route.endsWith('/:id')) {
const prefix = route.slice(0, -'/:id'.length);
if (path.startsWith(`${prefix}/`) && path.length > prefix.length + 1) return manifest;
} else if (path === route) {
return manifest;
}
}
return null;
}
export type EditScope = {
// Registry order, so a caller can zip it against DRAFT_DOCS.
docs: DraftDocId[];
// Rule 7 (a project's category against the branch's labels) is only reachable
// where a project renders, so the two label documents follow the projects
// scope instead of riding along on every route.
needsLabels: boolean;
};
// A route no manifest describes renders nothing editable — an empty scope is
// the honest answer, and a later navigation to a real route fetches what it
// needs.
export function scopeForPath(pathname: string): EditScope {
const manifest = manifestForPath(pathname);
const entities = new Set<TextEntity>();
if (manifest) {
for (const claim of manifest.content) entities.add(DOC_ENTITY[claim.entity]);
}
const docs = DRAFT_DOCS.filter((meta) => {
const entity = entityOfDoc(meta.id);
return entity !== null && entities.has(entity);
});
return {
docs: docs.map((meta) => meta.id),
needsLabels: entities.has('projects'),
};
}
// The registry entries for a scope, in registry order.
export function metasForDocs(ids: readonly DraftDocId[]): DraftDocMeta[] {
const wanted = new Set(ids);
return DRAFT_DOCS.filter((meta) => wanted.has(meta.id));
}