refactor state storge, UI editor

This commit is contained in:
taDuc
2026-05-07 13:38:52 +07:00
parent a29a3a2049
commit 8b1df73797
46 changed files with 3345 additions and 3112 deletions
+3 -6
View File
@@ -16,11 +16,11 @@ export type TimelineRange = {
export type EntityFormState = {
name: string;
slug: string;
type_id: string;
description: string;
};
export type GeometryMetaFormState = {
type_key: string;
time_start: string;
time_end: string;
binding: string;
@@ -29,16 +29,13 @@ export type GeometryMetaFormState = {
export type PendingEntityCreate = {
id: string;
name: string;
slug: string | null;
type_id: string;
description: string | null;
status: number;
};
export type CreatedEntitySummary = {
id: string;
name: string;
type_id?: string | null;
};
export type GeometryPreset = EntityGeometryPreset;
@@ -2,23 +2,16 @@ import { useState } from "react";
import type { Entity } from "@/uhm/types/entities";
import type { EntitySnapshot } from "@/uhm/types/entities";
import type { FeatureId } from "@/uhm/types/geo";
import { DEFAULT_ENTITY_TYPE_ID } from "@/uhm/lib/entityTypeOptions";
import type {
CreatedEntitySummary,
EntityFormState,
GeometryMetaFormState,
PendingEntityCreate,
} from "@/uhm/lib/editor/session/sessionTypes";
export function useEntitySessionState() {
// Entities đã persisted từ backend (dùng cho search/binding).
const [persistedEntities, setPersistedEntities] = useState<Entity[]>([]);
// Entities được "pin" vào project dưới dạng reference (không cần chọn geometry).
const [projectEntityRefs, setProjectEntityRefs] = useState<EntitySnapshot[]>([]);
// Entities tạo mới trong phiên nhưng chưa commit lên backend.
const [pendingEntityCreates, setPendingEntityCreates] = useState<PendingEntityCreate[]>([]);
// Tóm tắt entities đã tạo (để hiển thị nhanh ở sidebar).
const [createdEntities, setCreatedEntities] = useState<CreatedEntitySummary[]>([]);
// Entity catalog loaded from backend (global list, used for search/lookup).
const [entityCatalog, setEntityCatalog] = useState<Entity[]>([]);
// Snapshot entity store for the current editor session (single source of truth for snapshot.entities).
const [snapshotEntities, setSnapshotEntities] = useState<EntitySnapshot[]>([]);
// Thông báo trạng thái/lỗi liên quan entity/session.
const [entityStatus, setEntityStatus] = useState<string | null>(null);
// Feature đang được chọn để thao tác bind entities/metadata.
@@ -26,13 +19,13 @@ export function useEntitySessionState() {
// Form tạo entity mới (độc lập).
const [entityForm, setEntityForm] = useState<EntityFormState>({
name: "",
slug: "",
type_id: DEFAULT_ENTITY_TYPE_ID,
description: "",
});
// Danh sách entity IDs đang chọn để bind vào geometry hiện tại.
const [selectedGeometryEntityIds, setSelectedGeometryEntityIds] = useState<string[]>([]);
// Form metadata geometry (time range + binding ids).
const [geometryMetaForm, setGeometryMetaForm] = useState<GeometryMetaFormState>({
type_key: "",
time_start: "",
time_end: "",
binding: "",
@@ -51,14 +44,10 @@ export function useEntitySessionState() {
const [isEntitySearchLoading, setIsEntitySearchLoading] = useState(false);
return {
persistedEntities,
setPersistedEntities,
projectEntityRefs,
setProjectEntityRefs,
pendingEntityCreates,
setPendingEntityCreates,
createdEntities,
setCreatedEntities,
entityCatalog,
setEntityCatalog,
snapshotEntities,
setSnapshotEntities,
entityStatus,
setEntityStatus,
selectedFeatureId,
@@ -51,8 +51,8 @@ export function useSectionSessionState(options: Options) {
const [sectionState, setSectionState] = useState<SectionState | null>(null);
// Danh sách commits của section đang mở.
const [sectionCommits, setSectionCommits] = useState<SectionCommit[]>([]);
// Snapshot gần nhất đã load (để build snapshot diff/metadata).
const [lastSectionSnapshot, setLastSectionSnapshot] = useState<EditorSnapshot | null>(null);
// Baseline snapshot currently loaded for this editor session.
const [baselineSnapshot, setBaselineSnapshot] = useState<EditorSnapshot | null>(null);
return {
isSaving,
@@ -79,7 +79,7 @@ export function useSectionSessionState(options: Options) {
setSectionState,
sectionCommits,
setSectionCommits,
lastSectionSnapshot,
setLastSectionSnapshot,
baselineSnapshot,
setBaselineSnapshot,
};
}
@@ -3,7 +3,7 @@ import type { WikiSnapshot } from "@/uhm/types/wiki";
import type { EntityWikiLinkSnapshot } from "@/uhm/types/sections";
export function useWikiSessionState() {
const [wikis, setWikis] = useState<WikiSnapshot[]>([]);
const [entityWikiLinks, setEntityWikiLinks] = useState<EntityWikiLinkSnapshot[]>([]);
return { wikis, setWikis, entityWikiLinks, setEntityWikiLinks };
const [snapshotWikis, setSnapshotWikis] = useState<WikiSnapshot[]>([]);
const [snapshotEntityWikiLinks, setSnapshotEntityWikiLinks] = useState<EntityWikiLinkSnapshot[]>([]);
return { snapshotWikis, setSnapshotWikis, snapshotEntityWikiLinks, setSnapshotEntityWikiLinks };
}