refactor(workbench): split editor state and components

This commit is contained in:
que01 committed 2026-09-02 15:04:58 +08:00
1 parent caa3cb843f
commit 834470cfa1
23 files changed
+1219 -878

No files matched your search

@@ -0,0 +1,59 @@
import { useEffect, useRef, useState } from 'react';
import { api } from '../lib/api';
import { EditSession } from '../edit/session';
import type { EditDiagnostic, HandleManifest, PreviewLayerName } from '../edit/types';
import type { GeoJson, WorkbenchState } from '../types/state';
/**
* Owns the stable v2 session and the server state that surrounds it. Gesture
* construction and map interaction stay with the composition root for now.
*/
export function useDirectEditSession({
editMode,
state,
selectedSegmentId,
setStatus,
}: {
editMode: boolean;
state?: WorkbenchState;
selectedSegmentId?: string;
setStatus: (status: string) => void;
}) {
const [manifest, setManifest] = useState<HandleManifest | null>(null);
const [preview, setPreview] = useState<Partial<Record<PreviewLayerName, GeoJson | null>> | null>(null);
const [diagnostics, setDiagnostics] = useState<EditDiagnostic[]>([]);
const [revision, setRevision] = useState(0);
const sessionRef = useRef<EditSession | null>(null);
const loadedDocument = useRef<string | null>(null);
if (editMode && !sessionRef.current) sessionRef.current = new EditSession();
const touch = () => setRevision((value) => value + 1);
useEffect(() => {
// Flag off: no manifest request at all, so the network trace matches browse mode.
if (!editMode) return;
api
.editState(selectedSegmentId ? { segment: selectedSegmentId } : undefined)
.then((value) => {
if (!('handles' in value)) return;
setManifest(value.handles);
const key = `${value.areaId}:${value.documentVersion}`;
if (loadedDocument.current === key) return;
loadedDocument.current = key;
sessionRef.current?.load(value.document.constraints, value.document.operations, value.documentVersion);
touch();
})
.catch((error: Error) => setStatus(`直接编辑手柄不可用:${error.message}`));
}, [editMode, selectedSegmentId, setStatus, state]);
return {
session: sessionRef.current,
manifest,
setManifest,
preview,
setPreview,
diagnostics,
setDiagnostics,
revision,
touch,
};
}