refactor: tab saveContext resolution post collection remove action

This commit is contained in:
jamesgeorge007
2024-05-01 21:02:11 +05:30
parent 0abdc63f0e
commit 412daa4d17
3 changed files with 40 additions and 29 deletions

View File

@@ -1812,8 +1812,7 @@ const dropCollection = async (payload: {
: resolvedDestinationCollectionIndex : resolvedDestinationCollectionIndex
} }
resolveSaveContextOnCollectionReorder( resolveSaveContextOnCollectionReorder({
{
lastIndex: pathToLastIndex(draggedCollectionIndex), lastIndex: pathToLastIndex(draggedCollectionIndex),
newIndex: -1, newIndex: -1,
folderPath: draggedParentCollectionIndex, folderPath: draggedParentCollectionIndex,
@@ -1821,9 +1820,7 @@ const dropCollection = async (payload: {
restCollectionState.value, restCollectionState.value,
draggedParentCollectionIndex draggedParentCollectionIndex
).length, ).length,
}, })
"drop"
)
updateSaveContextForAffectedRequests( updateSaveContextForAffectedRequests(
draggedCollectionIndex, draggedCollectionIndex,

View File

@@ -22,15 +22,12 @@ import { HandleRef } from "~/services/new-workspace/handle"
* @returns * @returns
*/ */
export function resolveSaveContextOnCollectionReorder( export function resolveSaveContextOnCollectionReorder(payload: {
payload: {
lastIndex: number lastIndex: number
newIndex: number newIndex: number
folderPath: string folderPath: string
length?: number // better way to do this? now it could be undefined length?: number // better way to do this? now it could be undefined
}, }) {
type: "remove" | "drop" = "remove"
) {
const { lastIndex, folderPath, length } = payload const { lastIndex, folderPath, length } = payload
let { newIndex } = payload let { newIndex } = payload
@@ -45,12 +42,6 @@ export function resolveSaveContextOnCollectionReorder(
if (newIndex === -1) { if (newIndex === -1) {
// if (newIndex === -1) remove it from the map because it will be deleted // if (newIndex === -1) remove it from the map because it will be deleted
affectedIndexes.delete(lastIndex) affectedIndexes.delete(lastIndex)
// when collection deleted opended requests from that collection be affected
if (type === "remove") {
resetSaveContextForAffectedRequests(
folderPath ? `${folderPath}/${lastIndex}` : lastIndex.toString()
)
}
} }
// add folder path as prefix to the affected indexes // add folder path as prefix to the affected indexes

View File

@@ -59,6 +59,10 @@ import {
WorkspaceRequest, WorkspaceRequest,
} from "~/services/new-workspace/workspace" } from "~/services/new-workspace/workspace"
import {
getFoldersByPath,
resolveSaveContextOnCollectionReorder,
} from "~/helpers/collection/collection"
import { import {
getRequestsByPath, getRequestsByPath,
resolveSaveContextOnRequestReorder, resolveSaveContextOnRequestReorder,
@@ -279,10 +283,9 @@ export class PersonalWorkspaceProviderService
const { collectionID } = collectionHandle.value.data const { collectionID } = collectionHandle.value.data
const isRootCollection = collectionID.split("/").length === 1 const isRootCollection = collectionID.split("/").length === 1
if (isRootCollection) {
const collectionIndex = parseInt(collectionID) const collectionIndex = parseInt(collectionID)
if (isRootCollection) {
const collectionToRemove = navigateToFolderWithIndexPath( const collectionToRemove = navigateToFolderWithIndexPath(
restCollectionStore.value.state, restCollectionStore.value.state,
[collectionIndex] [collectionIndex]
@@ -322,6 +325,26 @@ export class PersonalWorkspaceProviderService
} }
} }
if (isRootCollection) {
resolveSaveContextOnCollectionReorder({
lastIndex: collectionIndex,
newIndex: -1,
folderPath: "", // root folder
length: restCollectionStore.value.state.length,
})
} else {
const parentCollectionID = collectionID.split("/").slice(0, -1).join("/") // remove last folder to get parent folder
resolveSaveContextOnCollectionReorder({
lastIndex: this.pathToLastIndex(collectionID),
newIndex: -1,
folderPath: parentCollectionID,
length: getFoldersByPath(
restCollectionStore.value.state,
parentCollectionID
).length,
})
}
return Promise.resolve(E.right(undefined)) return Promise.resolve(E.right(undefined))
} }