refactor: migrate project data models and transition editor state management to the new project-based API architecture.

This commit is contained in:
taDuc
2026-05-12 05:18:54 +07:00
parent 8f911abe35
commit 8f6d848d55
15 changed files with 162 additions and 169 deletions
+21 -21
View File
@@ -14,7 +14,7 @@ import GeometryBindingPanel from "@/uhm/components/editor/GeometryBindingPanel";
import { Entity, fetchEntities, searchEntitiesByName } from "@/uhm/api/entities";
import { ApiError } from "@/uhm/api/http";
import { fetchCurrentUser } from "@/uhm/api/auth";
import { SectionCommit } from "@/uhm/api/sections";
import { ProjectCommit } from "@/uhm/api/projects";
import { searchWikisByTitle, type Wiki } from "@/uhm/api/wikis";
import { searchGeometriesByEntityName, type EntityGeometriesSearchItem, type EntityGeometrySearchGeo } from "@/uhm/api/geometries";
import type { EntitySnapshot } from "@/uhm/types/entities";
@@ -57,13 +57,13 @@ import {
loadBackgroundLayerVisibilityFromStorage,
persistBackgroundLayerVisibility,
} from "@/uhm/lib/editor/background/backgroundVisibilityStorage";
import { useSectionCommands } from "@/uhm/lib/editor/section/useSectionCommands";
import { useProjectCommands } from "@/uhm/lib/editor/project/useProjectCommands";
import { EMPTY_FEATURE_COLLECTION } from "@/uhm/lib/map/geo/constants";
import { FIXED_TIMELINE_RANGE, clampYearToFixedRange } from "@/uhm/lib/utils/timeline";
import { useFeatureCommands } from "./featureCommands";
import { deleteSubmission } from "@/uhm/api/sections";
import { deleteSubmission } from "@/uhm/api/projects";
import type { WikiSnapshot } from "@/uhm/types/wiki";
import type { EntityWikiLinkSnapshot } from "@/uhm/types/sections";
import type { EntityWikiLinkSnapshot } from "@/uhm/types/projects";
import UnifiedSearchBar, { type UnifiedSearchKind } from "@/uhm/components/ui/UnifiedSearchBar";
const CURRENT_YEAR = new Date().getUTCFullYear();
@@ -107,8 +107,8 @@ export default function Page() {
isOpeningSection,
setIsOpeningSection,
setAvailableSections,
selectedSectionId,
setSelectedSectionId,
selectedProjectId,
setSelectedProjectId,
newSectionTitle,
setNewSectionTitle,
commitTitle,
@@ -116,10 +116,10 @@ export default function Page() {
editorUserIdInput,
activeSection,
setActiveSection,
sectionState,
setSectionState,
projectState,
setProjectState,
sectionCommits,
setSectionCommits,
setProjectCommits,
baselineSnapshot,
setBaselineSnapshot,
entityCatalog,
@@ -367,13 +367,13 @@ export default function Page() {
+ (entitiesDirty ? 1 : 0)
+ (entityWikiDirty ? 1 : 0);
const sectionCommands = useSectionCommands({
const sectionCommands = useProjectCommands({
editor,
editorUserId,
emptyFeatureCollection: EMPTY_FEATURE_COLLECTION,
activeSection,
sectionState,
selectedSectionId,
projectState,
selectedProjectId,
newSectionTitle,
pendingSaveCount,
snapshotEntities,
@@ -382,11 +382,11 @@ export default function Page() {
baselineSnapshot,
commitTitle,
setActiveSection,
setSelectedSectionId,
setSectionState,
setSelectedProjectId,
setProjectState,
setBaselineSnapshot,
setInitialData,
setSectionCommits,
setProjectCommits,
setSnapshotEntities,
setSnapshotWikis,
setSnapshotEntityWikiLinks,
@@ -1144,8 +1144,8 @@ export default function Page() {
}
};
const headCommit = sectionState?.head_commit_id
? sectionCommits.find((commit) => commit.id === sectionState.head_commit_id) || null
const headCommit = projectState?.head_commit_id
? sectionCommits.find((commit) => commit.id === projectState.head_commit_id) || null
: null;
const handleCreateFeature = (feature: Feature) => {
@@ -1166,12 +1166,12 @@ export default function Page() {
isSaving={isSaving}
isSubmitting={isSubmitting}
sectionTitle={activeSection?.title || "Đang tải project"}
sectionStatus={sectionState?.status || "editing"}
projectStatus={projectState?.status || "editing"}
commitTitle={commitTitle}
onCommitTitleChange={setCommitTitle}
commitCount={sectionCommits.length}
hasHeadCommit={Boolean(sectionState?.head_commit_id)}
headCommitId={sectionState?.head_commit_id || null}
hasHeadCommit={Boolean(projectState?.head_commit_id)}
headCommitId={projectState?.head_commit_id || null}
latestCommitLabel={headCommit ? `Head: ${formatCommitTitle(headCommit)}` : null}
commits={sectionCommits}
changesCount={pendingSaveCount}
@@ -1633,7 +1633,7 @@ function normalizeEditorUserId(value: string): string {
return normalized || DEFAULT_EDITOR_USER_ID;
}
function formatCommitTitle(commit: SectionCommit): string {
function formatCommitTitle(commit: ProjectCommit): string {
return commit.edit_summary?.trim() || `Commit ${commit.id.slice(0, 8)}`;
}
+1 -1
View File
@@ -14,7 +14,7 @@ import Badge from "@/components/ui/badge/Badge";
import { CreateProjectPayload, Project } from "@/interface/project";
import { apiCreateProject, apiCreateProjectCommit, apiGetProjectCommits, getCurrentProject } from "@/service/projectService";
import { normalizeEditorSnapshot } from "@/uhm/lib/editor/snapshot/editorSnapshot";
import type { EditorSnapshot } from "@/uhm/types/sections";
import type { EditorSnapshot } from "@/uhm/types/projects";
export type ProjectSortColumn = "created_at" | "updated_at" | "title";
+1 -1
View File
@@ -209,7 +209,7 @@ export default function WikiEditorPage() {
type: "doc",
content: [
{ type: "paragraph", content: [{ type: "text", text: "Write your wiki content here." }] },
{ type: "heading", attrs: { level: 2 }, content: [{ type: "text", text: "Section" }] },
{ type: "heading", attrs: { level: 2 }, content: [{ type: "text", text: "Project" }] },
{ type: "paragraph", content: [{ type: "text", text: "Use H1/H2/H3 and the TOC will follow." }] },
],
},
@@ -2,43 +2,43 @@ import { API_BASE_URL, API_ENDPOINTS } from "@/uhm/api/config";
import { ApiError, jsonRequestInit, requestJson } from "@/uhm/api/http";
import type {
CreateCommitInput,
CreateSectionInput,
CreateProjectInput,
EditorLoadResponse,
RestoreCommitInput,
Section,
SectionCommit,
SectionState,
SectionSubmission,
} from "@/uhm/types/sections";
Project,
ProjectCommit,
ProjectState,
ProjectSubmission,
} from "@/uhm/types/projects";
export type {
CreateCommitInput,
CreateSectionInput,
CreateProjectInput,
EditorLoadResponse,
RestoreCommitInput,
Section,
SectionCommit,
SectionState,
SectionSubmission,
} from "@/uhm/types/sections";
Project,
ProjectCommit,
ProjectState,
ProjectSubmission,
} from "@/uhm/types/projects";
// Sections (API cũ) => Projects (API mới)
// Projects (API cũ) => Projects (API mới)
export async function fetchSections(): Promise<Section[]> {
export async function fetchProjects(): Promise<Project[]> {
// /users/current/project requires JWT.
return requestJson<Section[]>(API_ENDPOINTS.currentUserProjects);
return requestJson<Project[]>(API_ENDPOINTS.currentUserProjects);
}
export async function createSection(input: CreateSectionInput): Promise<Section> {
export async function createProject(input: CreateProjectInput): Promise<Project> {
// POST /projects
return requestJson<Section>(API_ENDPOINTS.projects, jsonRequestInit("POST", input));
return requestJson<Project>(API_ENDPOINTS.projects, jsonRequestInit("POST", input));
}
export async function openSectionEditor(sectionId: string): Promise<EditorLoadResponse> {
export async function openSectionEditor(projectId: string): Promise<EditorLoadResponse> {
// API mới không có endpoint "editor". FE tự load:
// 1) Project details
// 2) Project commits (to get snapshot_json of latest commit)
const project = await requestJson<Section>(`${API_ENDPOINTS.projects}/${encodeURIComponent(sectionId)}`);
const project = await requestJson<Project>(`${API_ENDPOINTS.projects}/${encodeURIComponent(projectId)}`);
const pending = (project.submissions || []).find((s) => s?.status === "PENDING") || null;
if (pending) {
@@ -51,33 +51,33 @@ export async function openSectionEditor(sectionId: string): Promise<EditorLoadRe
);
}
const commits = await fetchSectionCommits(sectionId);
const commits = await fetchProjectCommits(projectId);
const headCommitId = project.latest_commit_id ?? null;
const headCommit = headCommitId ? commits.find((c) => c.id === headCommitId) || null : null;
const snapshot = headCommit?.snapshot_json ?? null;
const state: SectionState = {
const state: ProjectState = {
status: project.project_status || "ACTIVE",
head_commit_id: headCommitId,
locked_by: project.locked_by ?? null,
};
return {
section: project,
project: project,
state,
commit: headCommit,
snapshot,
};
}
export async function createSectionCommit(
sectionId: string,
export async function createProjectCommit(
projectId: string,
input: CreateCommitInput
): Promise<{ commit: SectionCommit; state: SectionState }> {
): Promise<{ commit: ProjectCommit; state: ProjectState }> {
// POST /projects/{id}/commits
const commit = await requestJson<SectionCommit>(
`${API_ENDPOINTS.projects}/${encodeURIComponent(sectionId)}/commits`,
const commit = await requestJson<ProjectCommit>(
`${API_ENDPOINTS.projects}/${encodeURIComponent(projectId)}/commits`,
jsonRequestInit("POST", {
snapshot_json: input.snapshot,
edit_summary: input.edit_summary,
@@ -85,8 +85,8 @@ export async function createSectionCommit(
);
// Refresh project state (latest_commit_id may have moved).
const project = await requestJson<Section>(`${API_ENDPOINTS.projects}/${encodeURIComponent(sectionId)}`);
const state: SectionState = {
const project = await requestJson<Project>(`${API_ENDPOINTS.projects}/${encodeURIComponent(projectId)}`);
const state: ProjectState = {
status: project.project_status || "ACTIVE",
head_commit_id: project.latest_commit_id ?? null,
locked_by: project.locked_by ?? null,
@@ -95,27 +95,27 @@ export async function createSectionCommit(
return { commit, state };
}
export async function fetchSectionCommits(sectionId: string): Promise<SectionCommit[]> {
return requestJson<SectionCommit[]>(`${API_ENDPOINTS.projects}/${encodeURIComponent(sectionId)}/commits`);
export async function fetchProjectCommits(projectId: string): Promise<ProjectCommit[]> {
return requestJson<ProjectCommit[]>(`${API_ENDPOINTS.projects}/${encodeURIComponent(projectId)}/commits`);
}
export async function restoreSectionCommit(
sectionId: string,
export async function restoreProjectCommit(
projectId: string,
input: RestoreCommitInput
): Promise<{ commit: SectionCommit | null; state: SectionState }> {
): Promise<{ commit: ProjectCommit | null; state: ProjectState }> {
// POST /projects/{id}/commits/restore
await requestJson(
`${API_ENDPOINTS.projects}/${encodeURIComponent(sectionId)}/commits/restore`,
`${API_ENDPOINTS.projects}/${encodeURIComponent(projectId)}/commits/restore`,
jsonRequestInit("POST", { commit_id: input.commit_id })
);
// Reload commits + project to determine new head commit.
const project = await requestJson<Section>(`${API_ENDPOINTS.projects}/${encodeURIComponent(sectionId)}`);
const commits = await fetchSectionCommits(sectionId);
const project = await requestJson<Project>(`${API_ENDPOINTS.projects}/${encodeURIComponent(projectId)}`);
const commits = await fetchProjectCommits(projectId);
const headCommitId = project.latest_commit_id ?? null;
const headCommit = headCommitId ? commits.find((c) => c.id === headCommitId) || null : null;
const state: SectionState = {
const state: ProjectState = {
status: project.project_status || "ACTIVE",
head_commit_id: headCommitId,
locked_by: project.locked_by ?? null,
@@ -124,18 +124,18 @@ export async function restoreSectionCommit(
return { commit: headCommit, state };
}
export async function submitSection(sectionId: string, content: string): Promise<SectionSubmission> {
export async function submitSection(projectId: string, content: string): Promise<ProjectSubmission> {
// Submit latest commit of project
const project = await requestJson<Section>(`${API_ENDPOINTS.projects}/${encodeURIComponent(sectionId)}`);
const project = await requestJson<Project>(`${API_ENDPOINTS.projects}/${encodeURIComponent(projectId)}`);
const commitId = project.latest_commit_id;
if (!commitId) {
throw new Error("Project has no latest commit to submit");
}
return requestJson<SectionSubmission>(
return requestJson<ProjectSubmission>(
API_ENDPOINTS.submissions,
jsonRequestInit("POST", {
project_id: sectionId,
project_id: projectId,
commit_id: commitId,
content: content,
})
+3 -3
View File
@@ -23,7 +23,7 @@ type Props = {
isSaving: boolean;
isSubmitting: boolean;
sectionTitle: string;
sectionStatus: string;
projectStatus: string;
commitTitle: string;
onCommitTitleChange: (title: string) => void;
commitCount: number;
@@ -62,7 +62,7 @@ export default function Editor({
isSaving,
isSubmitting,
sectionTitle,
sectionStatus,
projectStatus,
commitTitle,
onCommitTitleChange,
commitCount,
@@ -109,7 +109,7 @@ export default function Editor({
<ProjectPanel
sectionTitle={sectionTitle}
sectionStatus={sectionStatus}
projectStatus={projectStatus}
commitCount={commitCount}
latestCommitLabel={latestCommitLabel}
/>
@@ -3,7 +3,7 @@
import { useEffect, useMemo, useState } from "react";
import type { Entity } from "@/uhm/types/entities";
import type { WikiSnapshot } from "@/uhm/types/wiki";
import type { EntityWikiLinkSnapshot } from "@/uhm/types/sections";
import type { EntityWikiLinkSnapshot } from "@/uhm/types/projects";
type EntityChoice = { id: string; name: string };
type WikiChoice = { id: string; title: string; operation?: string };
+3 -3
View File
@@ -2,14 +2,14 @@ import { Panel } from "./Panel";
type ProjectPanelProps = {
sectionTitle: string;
sectionStatus: string;
projectStatus: string;
commitCount: number;
latestCommitLabel: string | null;
};
export function ProjectPanel({
sectionTitle,
sectionStatus,
projectStatus,
commitCount,
latestCommitLabel,
}: ProjectPanelProps) {
@@ -18,7 +18,7 @@ export function ProjectPanel({
<div style={{ fontSize: 12, color: "#cbd5e1", lineHeight: 1.4 }}>
<div style={{ color: "white", fontWeight: 850, overflowWrap: "anywhere" }}>{sectionTitle}</div>
<div style={{ marginTop: 6 }}>
Status: <span style={{ color: "#e2e8f0" }}>{sectionStatus}</span>
Status: <span style={{ color: "#e2e8f0" }}>{projectStatus}</span>
</div>
<div style={{ marginTop: 6 }}>
Commits: <span style={{ color: "#e2e8f0" }}>{commitCount}</span>
+1 -1
View File
@@ -6,7 +6,7 @@ import type {
} from "@/uhm/types/geo";
import type { EntitySnapshot } from "@/uhm/types/entities";
import type { WikiSnapshot } from "@/uhm/types/wiki";
import type { EntityWikiLinkSnapshot } from "@/uhm/types/sections";
import type { EntityWikiLinkSnapshot } from "@/uhm/types/projects";
export type Change = GeometryChange;
@@ -2,17 +2,17 @@ import { useCallback } from "react";
import type { Dispatch, SetStateAction } from "react";
import { ApiError } from "@/uhm/api/http";
import {
createSection,
createSectionCommit,
fetchSectionCommits,
fetchSections,
createProject,
createProjectCommit,
fetchProjectCommits,
fetchProjects,
openSectionEditor,
submitSection,
} from "@/uhm/api/sections";
} from "@/uhm/api/projects";
import { buildEditorSnapshot, normalizeEditorSnapshot } from "@/uhm/lib/editor/snapshot/editorSnapshot";
import type { Change } from "@/uhm/lib/editor/draft/editorTypes";
import type { Feature, FeatureCollection, FeatureId, GeometryEntitySnapshot, GeometrySnapshot } from "@/uhm/types/geo";
import type { EditorSnapshot, Section, SectionCommit, SectionState, EntityWikiLinkSnapshot } from "@/uhm/types/sections";
import type { EditorSnapshot, Project, ProjectCommit, ProjectState, EntityWikiLinkSnapshot } from "@/uhm/types/projects";
import type { EntitySnapshot } from "@/uhm/types/entities";
import type { WikiSnapshot } from "@/uhm/types/wiki";
@@ -27,9 +27,9 @@ type Options = {
editor: EditorDraftApi;
editorUserId: string;
emptyFeatureCollection: FeatureCollection;
activeSection: Section | null;
sectionState: SectionState | null;
selectedSectionId: string;
activeSection: Project | null;
projectState: ProjectState | null;
selectedProjectId: string;
newSectionTitle: string;
pendingSaveCount: number;
snapshotEntities: EntitySnapshot[];
@@ -37,12 +37,12 @@ type Options = {
snapshotEntityWikiLinks: EntityWikiLinkSnapshot[];
baselineSnapshot: EditorSnapshot | null;
commitTitle: string;
setActiveSection: Dispatch<SetStateAction<Section | null>>;
setSelectedSectionId: Dispatch<SetStateAction<string>>;
setSectionState: Dispatch<SetStateAction<SectionState | null>>;
setActiveSection: Dispatch<SetStateAction<Project | null>>;
setSelectedProjectId: Dispatch<SetStateAction<string>>;
setProjectState: Dispatch<SetStateAction<ProjectState | null>>;
setBaselineSnapshot: Dispatch<SetStateAction<EditorSnapshot | null>>;
setInitialData: Dispatch<SetStateAction<FeatureCollection>>;
setSectionCommits: Dispatch<SetStateAction<SectionCommit[]>>;
setProjectCommits: Dispatch<SetStateAction<ProjectCommit[]>>;
setSnapshotEntities: Dispatch<SetStateAction<EntitySnapshot[]>>;
setSnapshotWikis: Dispatch<SetStateAction<WikiSnapshot[]>>;
setSnapshotEntityWikiLinks: Dispatch<SetStateAction<EntityWikiLinkSnapshot[]>>;
@@ -52,27 +52,27 @@ type Options = {
setIsSaving: Dispatch<SetStateAction<boolean>>;
setIsSubmitting: Dispatch<SetStateAction<boolean>>;
setIsOpeningSection: Dispatch<SetStateAction<boolean>>;
setAvailableSections: Dispatch<SetStateAction<Section[]>>;
setAvailableSections: Dispatch<SetStateAction<Project[]>>;
setNewSectionTitle: Dispatch<SetStateAction<string>>;
setCommitTitle: Dispatch<SetStateAction<string>>;
};
export function useSectionCommands(options: Options) {
const openSectionForEditing = useCallback(async (sectionId: string) => {
const editorPayload = await openSectionEditor(sectionId);
export function useProjectCommands(options: Options) {
const openSectionForEditing = useCallback(async (projectId: string) => {
const editorPayload = await openSectionEditor(projectId);
const snapshot = normalizeEditorSnapshot(editorPayload.snapshot);
// When starting a fresh editor session from a commit snapshot, treat all rows as baseline state:
// operations should not carry over as deltas into the next commit.
const sessionSnapshot = snapshot ? toEditorSessionSnapshot(snapshot) : null;
const commits = await fetchSectionCommits(sectionId);
const commits = await fetchProjectCommits(projectId);
const nextInitialData = sessionSnapshot?.editor_feature_collection || options.emptyFeatureCollection;
options.setActiveSection(editorPayload.section);
options.setSelectedSectionId(editorPayload.section.id);
options.setSectionState(editorPayload.state);
options.setActiveSection(editorPayload.project);
options.setSelectedProjectId(editorPayload.project.id);
options.setProjectState(editorPayload.state);
options.setBaselineSnapshot(sessionSnapshot);
options.setInitialData(nextInitialData);
options.setSectionCommits(commits);
options.setProjectCommits(commits);
options.setSnapshotEntities(sessionSnapshot?.entities || []);
options.setSnapshotWikis(sessionSnapshot?.wikis || []);
options.setSnapshotEntityWikiLinks(sessionSnapshot?.entity_wiki || []);
@@ -81,8 +81,8 @@ export function useSectionCommands(options: Options) {
}, [options]);
const commitSection = useCallback(async () => {
if (!options.activeSection || !options.sectionState) {
options.setEntityStatus("Chưa mở được section editor.");
if (!options.activeSection || !options.projectState) {
options.setEntityStatus("Chưa mở được project editor.");
return;
}
if (options.pendingSaveCount <= 0) {
@@ -95,7 +95,7 @@ export function useSectionCommands(options: Options) {
options.setEntityStatus(null);
try {
const snapshot = buildEditorSnapshot({
section: options.activeSection,
project: options.activeSection,
draft: options.editor.draft,
changes: geometryChanges,
snapshotEntities: options.snapshotEntities,
@@ -124,13 +124,13 @@ export function useSectionCommands(options: Options) {
// If stringify fails, let API call throw a more actionable error downstream.
}
const result = await createSectionCommit(options.activeSection.id, {
const result = await createProjectCommit(options.activeSection.id, {
snapshot,
edit_summary: editSummary,
});
const sessionSnapshot = toEditorSessionSnapshot(snapshot);
options.setSectionState(result.state);
options.setProjectState(result.state);
options.setBaselineSnapshot(sessionSnapshot);
options.setSnapshotEntities(sessionSnapshot.entities || []);
options.setSnapshotWikis(sessionSnapshot.wikis || []);
@@ -138,7 +138,7 @@ export function useSectionCommands(options: Options) {
options.setInitialData(options.editor.draft);
options.editor.clearChanges();
options.setCommitTitle("");
options.setSectionCommits(await fetchSectionCommits(options.activeSection.id));
options.setProjectCommits(await fetchProjectCommits(options.activeSection.id));
options.setEntityFormStatus("Đã tạo commit.");
} catch (err) {
if (err instanceof ApiError) {
@@ -154,26 +154,26 @@ export function useSectionCommands(options: Options) {
}, [options]);
const openSelectedSection = useCallback(async () => {
const sectionId = options.selectedSectionId.trim();
if (!sectionId) {
options.setEntityStatus("Hãy chọn section để mở.");
const projectId = options.selectedProjectId.trim();
if (!projectId) {
options.setEntityStatus("Hãy chọn project để mở.");
return;
}
if (options.pendingSaveCount > 0) {
const confirmed = window.confirm("Section hiện tại có thay đổi chưa Commit. Mở section khác sẽ bỏ các thay đổi này. Tiếp tục?");
const confirmed = window.confirm("Project hiện tại có thay đổi chưa Commit. Mở project khác sẽ bỏ các thay đổi này. Tiếp tục?");
if (!confirmed) return;
}
options.setIsOpeningSection(true);
options.setEntityStatus(null);
try {
await openSectionForEditing(sectionId);
options.setEntityStatus("Đã mở section để chỉnh sửa.");
await openSectionForEditing(projectId);
options.setEntityStatus("Đã mở project để chỉnh sửa.");
} catch (err) {
if (err instanceof ApiError) {
options.setEntityStatus(`Mở section thất bại: ${err.body}`);
options.setEntityStatus(`Mở project thất bại: ${err.body}`);
} else {
options.setEntityStatus("Mở section thất bại.");
options.setEntityStatus("Mở project thất bại.");
}
} finally {
options.setIsOpeningSection(false);
@@ -183,31 +183,31 @@ export function useSectionCommands(options: Options) {
const createAndOpenSection = useCallback(async () => {
const title = options.newSectionTitle.trim();
if (!title) {
options.setEntityStatus("Tên section là bắt buộc.");
options.setEntityStatus("Tên project là bắt buộc.");
return;
}
if (options.pendingSaveCount > 0) {
const confirmed = window.confirm("Section hiện tại có thay đổi chưa Commit. Tạo section mới sẽ bỏ các thay đổi này. Tiếp tục?");
const confirmed = window.confirm("Project hiện tại có thay đổi chưa Commit. Tạo project mới sẽ bỏ các thay đổi này. Tiếp tục?");
if (!confirmed) return;
}
options.setIsOpeningSection(true);
options.setEntityStatus(null);
try {
const section = await createSection({
const project = await createProject({
title,
description: null,
});
const sections = await fetchSections();
options.setAvailableSections(sections);
const projects = await fetchProjects();
options.setAvailableSections(projects);
options.setNewSectionTitle("");
await openSectionForEditing(section.id);
options.setEntityStatus("Đã tạo và mở section mới.");
await openSectionForEditing(project.id);
options.setEntityStatus("Đã tạo và mở project mới.");
} catch (err) {
if (err instanceof ApiError) {
options.setEntityStatus(`Tạo section thất bại: ${err.body}`);
options.setEntityStatus(`Tạo project thất bại: ${err.body}`);
} else {
options.setEntityStatus("Tạo section thất bại.");
options.setEntityStatus("Tạo project thất bại.");
}
} finally {
options.setIsOpeningSection(false);
@@ -215,8 +215,8 @@ export function useSectionCommands(options: Options) {
}, [openSectionForEditing, options]);
const submitCurrentSection = useCallback(async (content: string) => {
if (!options.activeSection || !options.sectionState?.head_commit_id) {
options.setEntityStatus("Section hiện tại chưa có head để submit.");
if (!options.activeSection || !options.projectState?.head_commit_id) {
options.setEntityStatus("Project hiện tại chưa có head để submit.");
return;
}
if (options.pendingSaveCount > 0) {
@@ -241,8 +241,8 @@ export function useSectionCommands(options: Options) {
}, [options]);
const restoreCommit = useCallback(async (commitId: string) => {
if (!options.activeSection || !options.sectionState) {
options.setEntityStatus("Chưa mở được section editor.");
if (!options.activeSection || !options.projectState) {
options.setEntityStatus("Chưa mở được project editor.");
return;
}
if (options.pendingSaveCount > 0) {
@@ -255,8 +255,8 @@ export function useSectionCommands(options: Options) {
try {
// FE-only restore: load snapshot from selected commit and apply to editor state.
// Do NOT move project's head commit on backend.
const commits = await fetchSectionCommits(options.activeSection.id);
const target = commits.find((c: SectionCommit) => c.id === commitId) || null;
const commits = await fetchProjectCommits(options.activeSection.id);
const target = commits.find((c: ProjectCommit) => c.id === commitId) || null;
if (!target) {
options.setEntityStatus("Không tìm thấy commit để restore.");
return;
@@ -274,8 +274,8 @@ export function useSectionCommands(options: Options) {
options.setSelectedFeatureIds([]);
options.setEntityFormStatus(null);
// Refresh commits list for UI, but keep sectionState/head as-is.
options.setSectionCommits(commits);
// Refresh commits list for UI, but keep projectState/head as-is.
options.setProjectCommits(commits);
options.setEntityFormStatus("Đã load snapshot từ commit (không đổi head trên BE).");
} catch (err) {
if (err instanceof ApiError) {
@@ -1,15 +1,15 @@
import { useCallback, useState } from "react";
import type { Dispatch, SetStateAction } from "react";
import type { EditorSnapshot, Section, SectionCommit, SectionState } from "@/uhm/types/sections";
import type { EditorSnapshot, Project, ProjectCommit, ProjectState } from "@/uhm/types/projects";
type Options = {
defaultEditorUserId: string;
};
type SectionTask = "idle" | "saving" | "submitting" | "opening-section";
type SectionTask = "idle" | "saving" | "submitting" | "opening-project";
export function useSectionSessionState(options: Options) {
// Single state machine cho các tác vụ async của section (saving/submitting/opening).
export function useProjectSessionState(options: Options) {
// Single state machine cho các tác vụ async của project (saving/submitting/opening).
const [sectionTask, setSectionTask] = useState<SectionTask>("idle");
const setTaskFlag = useCallback((task: Exclude<SectionTask, "idle">, next: SetStateAction<boolean>) => {
setSectionTask((prev) => {
@@ -22,7 +22,7 @@ export function useSectionSessionState(options: Options) {
const isSaving = sectionTask === "saving";
const isSubmitting = sectionTask === "submitting";
const isOpeningSection = sectionTask === "opening-section";
const isOpeningSection = sectionTask === "opening-project";
const setIsSaving: Dispatch<SetStateAction<boolean>> = useCallback((next) => {
setTaskFlag("saving", next);
}, [setTaskFlag]);
@@ -30,25 +30,25 @@ export function useSectionSessionState(options: Options) {
setTaskFlag("submitting", next);
}, [setTaskFlag]);
const setIsOpeningSection: Dispatch<SetStateAction<boolean>> = useCallback((next) => {
setTaskFlag("opening-section", next);
setTaskFlag("opening-project", next);
}, [setTaskFlag]);
// Danh sách sections để user chọn mở.
const [availableSections, setAvailableSections] = useState<Section[]>([]);
// Section ID đang được chọn trong dropdown.
const [selectedSectionId, setSelectedSectionId] = useState("");
// Title section mới (để create).
// Danh sách projects để user chọn mở.
const [availableSections, setAvailableSections] = useState<Project[]>([]);
// Project ID đang được chọn trong dropdown.
const [selectedProjectId, setSelectedProjectId] = useState("");
// Title project mới (để create).
const [newSectionTitle, setNewSectionTitle] = useState("");
// Input title cho commit.
const [commitTitle, setCommitTitle] = useState("");
// User ID dùng để gắn vào commit/submit/lock.
const [editorUserIdInput, setEditorUserIdInput] = useState(options.defaultEditorUserId);
// Section đang mở để edit (null nếu chưa mở).
const [activeSection, setActiveSection] = useState<Section | null>(null);
// Trạng thái section (version/head/status/lock).
const [sectionState, setSectionState] = useState<SectionState | null>(null);
// Danh sách commits của section đang mở.
const [sectionCommits, setSectionCommits] = useState<SectionCommit[]>([]);
// Project đang mở để edit (null nếu chưa mở).
const [activeSection, setActiveSection] = useState<Project | null>(null);
// Trạng thái project (version/head/status/lock).
const [projectState, setProjectState] = useState<ProjectState | null>(null);
// Danh sách commits của project đang mở.
const [sectionCommits, setProjectCommits] = useState<ProjectCommit[]>([]);
// Baseline snapshot currently loaded for this editor session.
const [baselineSnapshot, setBaselineSnapshot] = useState<EditorSnapshot | null>(null);
@@ -61,8 +61,8 @@ export function useSectionSessionState(options: Options) {
setIsOpeningSection,
availableSections,
setAvailableSections,
selectedSectionId,
setSelectedSectionId,
selectedProjectId,
setSelectedProjectId,
newSectionTitle,
setNewSectionTitle,
commitTitle,
@@ -71,10 +71,10 @@ export function useSectionSessionState(options: Options) {
setEditorUserIdInput,
activeSection,
setActiveSection,
sectionState,
setSectionState,
projectState,
setProjectState,
sectionCommits,
setSectionCommits,
setProjectCommits,
baselineSnapshot,
setBaselineSnapshot,
};
@@ -1,6 +1,6 @@
import { useState } from "react";
import type { WikiSnapshot } from "@/uhm/types/wiki";
import type { EntityWikiLinkSnapshot } from "@/uhm/types/sections";
import type { EntityWikiLinkSnapshot } from "@/uhm/types/projects";
export function useWikiSessionState() {
const [snapshotWikis, setSnapshotWikis] = useState<WikiSnapshot[]>([]);
@@ -4,9 +4,9 @@ import type { Change } from "@/uhm/lib/editor/draft/editorTypes";
import type { EntitySnapshot } from "@/uhm/types/entities";
import type { EntitySnapshotOperation } from "@/uhm/types/entities";
import type { Feature, FeatureCollection, GeometryEntitySnapshot, GeometrySnapshot } from "@/uhm/types/geo";
import type { EditorSnapshot, Section } from "@/uhm/types/sections";
import type { EditorSnapshot, Project } from "@/uhm/types/projects";
import type { WikiSnapshot } from "@/uhm/types/wiki";
import type { EntityWikiLinkSnapshot } from "@/uhm/types/sections";
import type { EntityWikiLinkSnapshot } from "@/uhm/types/projects";
type UnknownRecord = Record<string, unknown>;
@@ -251,7 +251,7 @@ export function normalizeEditorSnapshot(raw: unknown): EditorSnapshot | null {
}
export function buildEditorSnapshot(options: {
section: Section;
project: Project;
draft: FeatureCollection;
changes: Change[];
snapshotEntities: EntitySnapshot[];
@@ -2,7 +2,7 @@ import { useState } from "react";
import type { FeatureCollection } from "@/uhm/types/geo";
import { useBackgroundSessionState } from "@/uhm/lib/editor/session/useBackgroundSessionState";
import { useEntitySessionState } from "@/uhm/lib/editor/session/useEntitySessionState";
import { useSectionSessionState } from "@/uhm/lib/editor/session/useSectionSessionState";
import { useProjectSessionState } from "@/uhm/lib/editor/session/useProjectSessionState";
import { useTimelineState } from "@/uhm/lib/editor/session/useTimelineState";
import { useWikiSessionState } from "@/uhm/lib/editor/session/useWikiSessionState";
import type { EditorMode, TimelineRange } from "@/uhm/lib/editor/session/sessionTypes";
@@ -24,10 +24,10 @@ type Options = {
export function useEditorSessionState(options: Options) {
// Mode thao tác map/editor hiện tại.
const [mode, setMode] = useState<EditorMode>("idle");
// FeatureCollection "gốc" của session hiện tại (global timeline hoặc section snapshot).
// FeatureCollection "gốc" của session hiện tại (global timeline hoặc project snapshot).
const [initialData, setInitialData] = useState<FeatureCollection>(options.emptyFeatureCollection);
const section = useSectionSessionState({
const project = useProjectSessionState({
defaultEditorUserId: options.defaultEditorUserId,
});
const entity = useEntitySessionState();
@@ -43,7 +43,7 @@ export function useEditorSessionState(options: Options) {
setMode,
initialData,
setInitialData,
...section,
...project,
...entity,
...timeline,
...background,
+1 -1
View File
@@ -11,7 +11,7 @@ import { useUndoStack } from "@/uhm/lib/editor/draft/useUndoStack";
import type { Change, UndoAction } from "@/uhm/lib/editor/draft/editorTypes";
import type { EntitySnapshot } from "@/uhm/types/entities";
import type { WikiSnapshot } from "@/uhm/types/wiki";
import type { EntityWikiLinkSnapshot } from "@/uhm/types/sections";
import type { EntityWikiLinkSnapshot } from "@/uhm/types/projects";
export type { Feature, FeatureCollection, FeatureProperties, Geometry } from "@/uhm/types/geo";
export type { Change, UndoAction } from "@/uhm/lib/editor/draft/editorTypes";
@@ -11,7 +11,7 @@ export type EntityWikiLinkSnapshot = {
operation?: "reference" | "binding" | "delete";
};
// BackEndGo uses Projects/Commits/Submissions. "Section" is legacy naming in FE.
// BackEndGo uses Projects/Commits/Submissions. "Project" is legacy naming in FE.
export type ProjectStatus = string;
export type ProjectSubmissionStatus = "PENDING" | "APPROVED" | "REJECTED" | string;
@@ -71,9 +71,9 @@ export type ProjectSubmission = {
};
export type EditorSnapshot = {
// Legacy: before BEGo flow moved fully to project/commit records, FE stored a minimal "section" ref
// Legacy: before BEGo flow moved fully to project/commit records, FE stored a minimal "project" ref
// inside snapshot_json. New snapshots omit this entirely.
section?: {
project?: {
id: string;
title: string;
};
@@ -90,13 +90,13 @@ export type EditorSnapshot = {
export type CommitSnapshot = EditorSnapshot;
export type EditorLoadResponse = {
section: Project;
project: Project;
state: ProjectState;
commit: ProjectCommit | null;
snapshot: EditorSnapshot | null;
};
export type CreateSectionInput = {
export type CreateProjectInput = {
title: string;
description?: string | null;
status?: "PRIVATE" | "PUBLIC" | "ARCHIVE";
@@ -111,10 +111,3 @@ export type RestoreCommitInput = {
commit_id: string;
};
// Legacy aliases (to reduce churn in existing FE code). Prefer Project* names above.
export type SectionStatus = ProjectStatus;
export type SectionSubmissionStatus = ProjectSubmissionStatus;
export type SectionState = ProjectState;
export type Section = Project;
export type SectionCommit = ProjectCommit;
export type SectionSubmission = ProjectSubmission;