feat(edit): keep edit mode across navigation, only Exit leaves

Edit mode is a session, not a URL state. MaybeEditMode mounts the shell on
?edit or on the session flag; while mounted, EditModeShell re-attaches ?edit
after a route change that dropped it (replace navigation, skipped while
authenticating so it never fights a failed auth). The session initializes once
per mount - entry URL from a ref, stripEditQuery reads window.location - so
navigation neither re-authenticates, re-fetches, nor re-restores the draft.

?edit parsing, the session/branch flags and the portfolio-draft key move to a
dependency-free src/i18n/edit-session.ts so App.tsx can read the gate without
pulling the Basic-auth probe (and through it draft.ts) into the entry chunk;
edit-mode.ts is now the auth handshake alone.

Verified: tsc -b, pnpm build (entry chunk carries no probe literal,
EditModeShell stays a separate lazy chunk), pnpm lint (no new warnings),
throwaway smokes for the session semantics (17/17) and the router behaviour
behind the guard (5/5), both deleted after the run.
This commit is contained in:
dong.nguyen committed 2026-10-24 13:56:16 +07:00
1 parent 1bc12d23a6
commit 7b4a782f59
7 files changed
+121 -73

No files matched your search

+5 -54
View File
@@ -1,62 +1,13 @@
// Edit-mode session mechanics (spec-v4 §5.2, §6.2): the `?edit` entry, the
// sessionStorage flags that keep a session in the mode across reloads, the
// target branch (`?edit=<branch>`), and the Basic-auth handshake through the
// Edit-mode authentication (spec-v4 §5.2): the Basic-auth handshake through the
// protected proxy endpoint. The protected endpoint is the security boundary —
// edit mode is only a UI affordance.
// edit mode is only a UI affordance. The `?edit` entry, the session flags and
// the branch rules live in `edit-session.ts`, which App.tsx reads without
// pulling this file's dependency on the draft store into the entry chunk.
import { PROXY_BASE } from './draft';
const EDIT_FLAG = 'portfolio-edit';
const BRANCH_FLAG = 'portfolio-edit-branch';
export const DRAFT_STORAGE = 'portfolio-draft';
// The repository's default branch — what a bare `?edit` targets.
export const DEFAULT_BRANCH = 'master';
import { hasEditFlag } from './edit-session';
const PROBE_FILE = 'src/data/profile.json';
// Git's own ref rules, reduced to what a working branch name can be: no
// leading '-' or '/', no '..', no '//', no trailing '/', no `.lock` suffix,
// and only characters safe in a URL query value.
const BRANCH_RE = /^(?!-)(?!\/)(?!.*\.\.)(?!.*\/\/)(?!.*\.lock$)(?!.*\/$)[A-Za-z0-9][A-Za-z0-9._/-]{0,63}$/;
export function editRequested(search: string): boolean {
return new URLSearchParams(search).has('edit');
}
// `?edit` or `?edit=1` → undefined (fall back to the session's branch, or
// the default); `?edit=<branch>` → that branch; an illegal name → null (the
// shell exits edit mode the same way it does on a failed auth).
export function requestedBranch(search: string): string | null | undefined {
const value = new URLSearchParams(search).get('edit');
if (!value || value === '1') return undefined;
return BRANCH_RE.test(value) ? value : null;
}
export function getEditBranch(): string {
const saved = sessionStorage.getItem(BRANCH_FLAG);
return saved && BRANCH_RE.test(saved) ? saved : DEFAULT_BRANCH;
}
export function setEditBranch(branch: string): void {
sessionStorage.setItem(BRANCH_FLAG, branch);
}
export function hasEditFlag(): boolean {
return sessionStorage.getItem(EDIT_FLAG) === '1';
}
export function setEditFlag(on: boolean): void {
if (on) sessionStorage.setItem(EDIT_FLAG, '1');
else {
sessionStorage.removeItem(EDIT_FLAG);
sessionStorage.removeItem(BRANCH_FLAG);
}
}
export function clearDraftStorage(): void {
sessionStorage.removeItem(DRAFT_STORAGE);
}
function probeUrl(branch: string, cacheBust: boolean): string {
const url = new URL(`${PROXY_BASE}${PROBE_FILE}`, window.location.origin);
url.searchParams.set('ref', branch);
+60
View File
@@ -0,0 +1,60 @@
// The edit-mode session state (spec-v4 §5.2, §6.2): the `?edit` entry, the
// sessionStorage flags that keep a session in the mode across navigation and
// reloads, and the target branch. Dependency-free on purpose — App.tsx reads
// the mode gate from here, and that import must not pull the draft store or
// the save pipeline into the entry chunk (spec-v4 §7 P3.4). The Basic-auth
// handshake that needs them stays in `edit-mode.ts`.
const EDIT_FLAG = 'portfolio-edit';
const BRANCH_FLAG = 'portfolio-edit-branch';
export const DRAFT_STORAGE = 'portfolio-draft';
// The repository's default branch — what a bare `?edit` targets.
export const DEFAULT_BRANCH = 'master';
// Git's own ref rules, reduced to what a working branch name can be: no
// leading '-' or '/', no '..', no '//', no trailing '/', no `.lock` suffix,
// and only characters safe in a URL query value.
const BRANCH_RE = /^(?!-)(?!\/)(?!.*\.\.)(?!.*\/\/)(?!.*\.lock$)(?!.*\/$)[A-Za-z0-9][A-Za-z0-9._/-]{0,63}$/;
export function editRequested(search: string): boolean {
return new URLSearchParams(search).has('edit');
}
// `?edit` or `?edit=1` → undefined (fall back to the session's branch, or
// the default); `?edit=<branch>` → that branch; an illegal name → null (the
// shell exits edit mode the same way it does on a failed auth).
export function requestedBranch(search: string): string | null | undefined {
const value = new URLSearchParams(search).get('edit');
if (!value || value === '1') return undefined;
return BRANCH_RE.test(value) ? value : null;
}
export function getEditBranch(): string {
const saved = sessionStorage.getItem(BRANCH_FLAG);
return saved && BRANCH_RE.test(saved) ? saved : DEFAULT_BRANCH;
}
export function setEditBranch(branch: string): void {
sessionStorage.setItem(BRANCH_FLAG, branch);
}
// The mode is a session, not a URL state (owner request 2026-10-24): `?edit`
// opens it, the flag keeps it while the user moves through the site, and only
// the EditBar's Exit clears it. Every internal link the shell hands the router
// carries `?edit`, so the URL and the flag agree in practice; the flag is what
// survives a back navigation to an address taken before edit mode opened.
export function hasEditFlag(): boolean {
return sessionStorage.getItem(EDIT_FLAG) === '1';
}
export function setEditFlag(on: boolean): void {
if (on) sessionStorage.setItem(EDIT_FLAG, '1');
else {
sessionStorage.removeItem(EDIT_FLAG);
sessionStorage.removeItem(BRANCH_FLAG);
}
}
export function clearDraftStorage(): void {
sessionStorage.removeItem(DRAFT_STORAGE);
}