mirror of
https://github.com/dong-nz/cv.git
synced 2026-11-22 16:10:40 +07:00
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:
1 parent
1bc12d23a6
commit
7b4a782f59
7 files changed
+121
-73
No files matched your search
+9
-3
@@ -6,6 +6,7 @@ import { ThemeProvider } from '@/hooks/useTheme'
|
||||
import { Layout } from '@/components/layout/Layout'
|
||||
import { LanguageProvider } from '@/hooks/useLanguage'
|
||||
import { I18nProvider } from '@/i18n/provider'
|
||||
import { editRequested, hasEditFlag } from '@/i18n/edit-session'
|
||||
import { HomePage } from '@/pages/HomePage'
|
||||
|
||||
// HomePage stays eager: it is the landing route and owns the LCP, so its code must
|
||||
@@ -19,13 +20,18 @@ const PhilosophyPage = lazy(() => import('@/pages/PhilosophyPage').then((m) => (
|
||||
const ContactPage = lazy(() => import('@/pages/ContactPage').then((m) => ({ default: m.ContactPage })))
|
||||
|
||||
// Edit mode is lazy too: visitors never download the draft store, the panel
|
||||
// or the save pipeline (spec-v4 §7 P3.4). The gate only mounts the shell when
|
||||
// `?edit` is present; the shell then authenticates or strips the query again.
|
||||
// or the save pipeline (spec-v4 §7 P3.4). The gate opens the shell on `?edit`
|
||||
// or on a session that is already in the mode; the shell authenticates, or it
|
||||
// closes the session again.
|
||||
const EditModeShell = lazy(() => import('@/components/layout/EditModeShell'))
|
||||
|
||||
function MaybeEditMode({ children }: { children: ReactNode }) {
|
||||
const location = useLocation()
|
||||
const editing = new URLSearchParams(location.search).has('edit')
|
||||
// `?edit` opens edit mode; from there the session flag keeps it across every
|
||||
// navigation and every reload until the EditBar's Exit ends it. The flag and
|
||||
// the query test live in `edit-session.ts`, which carries no dependency on
|
||||
// the draft store, so this gate keeps the edit code out of the entry chunk.
|
||||
const editing = editRequested(location.search) || hasEditFlag()
|
||||
if (!editing) return <>{children}</>
|
||||
return (
|
||||
<Suspense fallback={null}>
|
||||
|
||||
@@ -9,7 +9,7 @@ import { useState } from 'react';
|
||||
import { useI18n } from '@/i18n/provider';
|
||||
import { DRAFT_DOCS, entityOfDoc } from '@/i18n/draft';
|
||||
import { manifestsClaiming, recordEntity } from '@/data/pages';
|
||||
import { DEFAULT_BRANCH } from '@/i18n/edit-mode';
|
||||
import { DEFAULT_BRANCH } from '@/i18n/edit-session';
|
||||
import type { ConflictField, DocSaveResult, DraftDocId } from '@/i18n/draft';
|
||||
import type { ContentIssue } from '@/i18n/validate';
|
||||
|
||||
|
||||
@@ -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, useState } from 'react';
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import type { ReactNode } from 'react';
|
||||
import { useLocation, useNavigate } from 'react-router';
|
||||
import { useLanguage } from '@/hooks/useLanguage';
|
||||
@@ -23,12 +23,13 @@ import {
|
||||
DEFAULT_BRANCH,
|
||||
DRAFT_STORAGE,
|
||||
clearDraftStorage,
|
||||
ensureEditorAuth,
|
||||
editRequested,
|
||||
getEditBranch,
|
||||
requestedBranch,
|
||||
setEditBranch,
|
||||
setEditFlag,
|
||||
} from '@/i18n/edit-mode';
|
||||
} from '@/i18n/edit-session';
|
||||
import { ensureEditorAuth } from '@/i18n/edit-mode';
|
||||
import { EditContext } from '@/hooks/useEdit';
|
||||
import type { EditApi } from '@/hooks/useEdit';
|
||||
import type { CategoryLabels, ContentIssue } from '@/i18n/validate';
|
||||
@@ -73,12 +74,22 @@ export default function EditModeShell({ children }: { children: ReactNode }) {
|
||||
const [saveStopped, setSaveStopped] = useState(false);
|
||||
const [labelsUnavailable, setLabelsUnavailable] = useState(false);
|
||||
|
||||
// The entry URL is read once. The session keeps its branch across internal
|
||||
// navigation (the guard below re-attaches `?edit` to every route), so a
|
||||
// navigation must not re-run the initialization and re-fetch every document.
|
||||
const entrySearchRef = useRef(location.search);
|
||||
|
||||
// Stable across navigations: it reads the live URL instead of closing over
|
||||
// `location`, so the initialization effect that calls it on a failed auth
|
||||
// does not re-run on every route change.
|
||||
const stripEditQuery = useCallback(() => {
|
||||
const params = new URLSearchParams(location.search);
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
params.delete('edit');
|
||||
const query = params.toString();
|
||||
navigate(`${location.pathname}${query ? `?${query}` : ''}${location.hash}`, { replace: true });
|
||||
}, [navigate, location.pathname, location.search, location.hash]);
|
||||
navigate(`${window.location.pathname}${query ? `?${query}` : ''}${window.location.hash}`, {
|
||||
replace: true,
|
||||
});
|
||||
}, [navigate]);
|
||||
|
||||
// Every accepted edit re-derives the resolved content from the draft
|
||||
// documents (same join, same fallback rules as the bundled loader) and
|
||||
@@ -94,13 +105,31 @@ export default function EditModeShell({ children }: { children: ReactNode }) {
|
||||
else clearDraftStorage();
|
||||
}, []);
|
||||
|
||||
// Edit mode survives navigation (owner request 2026-10-24): a route change
|
||||
// that dropped `?edit` gets the parameter back immediately, so the address
|
||||
// keeps describing the mode the session is actually in and a reload from
|
||||
// here reopens it. Only the EditBar's Exit — or a failed auth — ends the
|
||||
// session, and both clear the flag, which unmounts this shell before this
|
||||
// guard could run against them.
|
||||
useEffect(() => {
|
||||
if (phase === 'authenticating') return;
|
||||
if (editRequested(location.search)) return;
|
||||
const params = new URLSearchParams(location.search);
|
||||
params.delete('edit');
|
||||
const rest = params.toString();
|
||||
navigate(`${location.pathname}?edit${rest ? `&${rest}` : ''}${location.hash}`, { replace: true });
|
||||
}, [phase, location.pathname, location.search, location.hash, navigate]);
|
||||
|
||||
// The session's initialization runs once per mount: the entry URL is read
|
||||
// from a ref and `stripEditQuery` is stable, so a route change inside the
|
||||
// session neither re-authenticates nor re-fetches the documents.
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
const init = async () => {
|
||||
// `?edit=<branch>` targets that branch; a bare `?edit` resumes the
|
||||
// session's branch or the default. An illegal name exits edit mode the
|
||||
// same way a failed auth does.
|
||||
const requested = requestedBranch(location.search);
|
||||
const requested = requestedBranch(entrySearchRef.current);
|
||||
if (requested === null) {
|
||||
setEditFlag(false);
|
||||
clearDraftStorage();
|
||||
@@ -170,7 +199,7 @@ export default function EditModeShell({ children }: { children: ReactNode }) {
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [refresh, stripEditQuery, location.search]);
|
||||
}, [refresh, stripEditQuery]);
|
||||
|
||||
// Mid-edit navigation guard (§5.2).
|
||||
useEffect(() => {
|
||||
|
||||
+5
-54
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in new issue
Block a user