Fix: persist folder drag and drop moves
This commit is contained in:
@@ -8,6 +8,7 @@ import { ProjectSelector } from "../ProjectSelector/ProjectSelector";
|
||||
import {
|
||||
collectBranchNodeIds,
|
||||
findTreeNodeDepth,
|
||||
findTreeNodeLocation,
|
||||
getPointerRelativeY,
|
||||
isUuidString,
|
||||
moveTreeNode,
|
||||
@@ -59,6 +60,7 @@ type WorkspaceFoldersResponse = {
|
||||
data?: {
|
||||
folders?: PersistedWorkspaceFolderRecord[];
|
||||
renamedFolder?: PersistedWorkspaceFolderRecord;
|
||||
movedFolder?: PersistedWorkspaceFolderRecord;
|
||||
previousFolderId?: string;
|
||||
};
|
||||
error?: string;
|
||||
@@ -527,9 +529,27 @@ export const WorkspaceSidebar = (props: WorkspaceSidebarProps): JSX.Element => {
|
||||
}
|
||||
|
||||
suppressTreeClickTemporarily();
|
||||
setWorkspaceTreeNodes((current) =>
|
||||
moveTreeNode(current, nextDragState.draggedNodeId, nextDragState.dropTarget as WorkspaceDragTarget, workspaceTreeAdapter),
|
||||
);
|
||||
|
||||
const currentNodes = workspaceTreeNodes();
|
||||
const draggedLocation = findTreeNodeLocation(currentNodes, nextDragState.draggedNodeId, workspaceTreeAdapter);
|
||||
const persistedParentId = nextDragState.dropTarget.parentId;
|
||||
const canPersistMove = isUuidString(activeProject()?.id ?? "");
|
||||
const persistedParentLocation = persistedParentId
|
||||
? findTreeNodeLocation(currentNodes, persistedParentId, workspaceTreeAdapter)
|
||||
: null;
|
||||
|
||||
if (
|
||||
canPersistMove &&
|
||||
draggedLocation?.node.kind === "folder" &&
|
||||
(persistedParentId === null || persistedParentLocation?.node.kind === "folder")
|
||||
) {
|
||||
void movePersistedFolder(draggedLocation.node.id, persistedParentId);
|
||||
} else {
|
||||
setWorkspaceTreeNodes((current) =>
|
||||
moveTreeNode(current, nextDragState.draggedNodeId, nextDragState.dropTarget as WorkspaceDragTarget, workspaceTreeAdapter),
|
||||
);
|
||||
}
|
||||
|
||||
setDragState(null);
|
||||
};
|
||||
|
||||
@@ -655,6 +675,45 @@ export const WorkspaceSidebar = (props: WorkspaceSidebarProps): JSX.Element => {
|
||||
}
|
||||
};
|
||||
|
||||
const movePersistedFolder = async (folderId: string, parentFolderId: string | null): Promise<void> => {
|
||||
const projectId = activeProject()?.id ?? "";
|
||||
if (!folderId || !projectId || !isUuidString(projectId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${resolveAPIBase()}/projects/${projectId}/tree/folders/move`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
folderId,
|
||||
parentFolderId,
|
||||
}),
|
||||
});
|
||||
|
||||
const body = (await response.json()) as WorkspaceFoldersResponse;
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(body.message || "Failed to move project tree folder.");
|
||||
}
|
||||
|
||||
setPersistedFolders(readPersistedWorkspaceFolders(body));
|
||||
|
||||
const previousFolderId = body.data?.previousFolderId;
|
||||
const movedFolderId = body.data?.movedFolder?.id;
|
||||
if (previousFolderId && movedFolderId && previousFolderId !== movedFolderId) {
|
||||
setCollapsedFolderIds((current) =>
|
||||
current.map((id) => (id === previousFolderId ? movedFolderId : id)),
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
};
|
||||
|
||||
const submitPendingFolderRename = async (): Promise<void> => {
|
||||
const draft = pendingFolderRename();
|
||||
const name = pendingFolderRenameName().trim();
|
||||
|
||||
Reference in New Issue
Block a user