Refactor: improve code quality and worker flow
This commit is contained in:
@@ -103,6 +103,9 @@ const buildProjectTree = (
|
||||
items: readonly ProjectItem[],
|
||||
folders: readonly PersistedProjectFolderRecord[] = [],
|
||||
): ProjectTreeNode[] => [
|
||||
// The selector still presents scaffold project leaves beside persisted folders.
|
||||
// Keep that mixed root shape explicit here so the drag/drop logic can account
|
||||
// for folder-only ordering when we persist sibling positions.
|
||||
...items.map((item) => ({
|
||||
kind: "project" as const,
|
||||
item,
|
||||
@@ -110,6 +113,11 @@ const buildProjectTree = (
|
||||
...buildPersistedFolderNodes(folders),
|
||||
];
|
||||
|
||||
const countProjectFolderSiblingsBeforeIndex = (
|
||||
siblings: readonly ProjectTreeNode[],
|
||||
index: number,
|
||||
): number => siblings.slice(0, index).filter((node) => node.kind === "folder").length;
|
||||
|
||||
const readPersistedFolders = (body: ProjectFoldersResponse): PersistedProjectFolderRecord[] =>
|
||||
Array.isArray(body.data?.folders) ? body.data.folders : [];
|
||||
|
||||
@@ -596,10 +604,11 @@ export const ProjectSelector = (props: ProjectSelectorProps): JSX.Element => {
|
||||
? persistedParentLocation.node.children
|
||||
: []
|
||||
: previewNodes;
|
||||
// The preview tree includes project leaves and folders, but the backend only
|
||||
// stores sibling order for folders. Persist a folder-only index so the server
|
||||
// can reapply the same position against the authoritative ordered tree.
|
||||
const targetIndex = previewLocation
|
||||
? previewSiblings
|
||||
.slice(0, previewLocation.index)
|
||||
.filter((node) => node.kind === "folder").length
|
||||
? countProjectFolderSiblingsBeforeIndex(previewSiblings, previewLocation.index)
|
||||
: 0;
|
||||
|
||||
if (
|
||||
@@ -795,12 +804,12 @@ export const ProjectSelector = (props: ProjectSelectorProps): JSX.Element => {
|
||||
const movePersistedFolder = async (
|
||||
folderPath: string,
|
||||
parentFolderPath: string | null,
|
||||
folderNodeId: string,
|
||||
parentNodeId: string | null,
|
||||
folderStableId: string,
|
||||
parentStableId: string | null,
|
||||
targetIndex: number,
|
||||
): Promise<void> => {
|
||||
const projectId = selectedProject().id;
|
||||
if (!folderPath || !folderNodeId || !isUuidString(projectId)) {
|
||||
if (!folderPath || !folderStableId || !isUuidString(projectId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -813,9 +822,9 @@ export const ProjectSelector = (props: ProjectSelectorProps): JSX.Element => {
|
||||
},
|
||||
body: JSON.stringify({
|
||||
folderId: folderPath,
|
||||
folderNodeId,
|
||||
folderNodeId: folderStableId,
|
||||
parentFolderId: parentFolderPath,
|
||||
parentNodeId,
|
||||
parentNodeId: parentStableId,
|
||||
targetIndex,
|
||||
}),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user