Skip to content
This repository was archived by the owner on Jul 21, 2025. It is now read-only.

Commit 14f8c3d

Browse files
vamsikrishnamathalacoderabbitai[bot]
authored andcommitted
[WEB-2774] fix:favorites reorder (makeplane#6179)
* fix:favorites reorder * chore: added error handling Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 699935e commit 14f8c3d

File tree

4 files changed

+25
-42
lines changed

4 files changed

+25
-42
lines changed

web/core/components/workspace/sidebar/favorites/favorite-folder.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { CustomMenu, Tooltip, DropIndicator, FavoriteFolderIcon, DragHandle } fr
2727
import { cn } from "@/helpers/common.helper";
2828
// hooks
2929
import { useAppTheme } from "@/hooks/store";
30+
import { useFavorite } from "@/hooks/store/use-favorite";
3031
import { usePlatformOS } from "@/hooks/use-platform-os";
3132
// constants
3233
import { FavoriteRoot } from "./favorite-items";
@@ -45,7 +46,7 @@ export const FavoriteFolder: React.FC<Props> = (props) => {
4546
const { favorite, handleRemoveFromFavorites, isLastChild, handleDrop } = props;
4647
// store hooks
4748
const { sidebarCollapsed: isSidebarCollapsed } = useAppTheme();
48-
49+
const { getGroupedFavorites } = useFavorite();
4950
const { isMobile } = usePlatformOS();
5051
const { workspaceSlug } = useParams();
5152
// states
@@ -58,6 +59,12 @@ export const FavoriteFolder: React.FC<Props> = (props) => {
5859
const actionSectionRef = useRef<HTMLDivElement | null>(null);
5960
const elementRef = useRef<HTMLDivElement | null>(null);
6061

62+
useEffect(() => {
63+
if (favorite.children === undefined && workspaceSlug) {
64+
getGroupedFavorites(workspaceSlug.toString(), favorite.id);
65+
}
66+
}, [favorite.id, favorite.children, workspaceSlug, getGroupedFavorites]);
67+
6168
useEffect(() => {
6269
const element = elementRef.current;
6370

@@ -123,7 +130,7 @@ export const FavoriteFolder: React.FC<Props> = (props) => {
123130
})
124131
);
125132
// eslint-disable-next-line react-hooks/exhaustive-deps
126-
}, [isDragging, favorite.id]);
133+
}, [isDragging, favorite.id, isLastChild, favorite.id]);
127134

128135
useOutsideClickDetector(actionSectionRef, () => setIsMenuActive(false));
129136

web/core/components/workspace/sidebar/favorites/favorite-items/root.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export const FavoriteRoot: FC<Props> = observer((props) => {
131131
})
132132
);
133133
// eslint-disable-next-line react-hooks/exhaustive-deps
134-
}, [elementRef?.current, isDragging]);
134+
}, [elementRef?.current, isDragging, isLastChild, favorite.id]);
135135

136136
useOutsideClickDetector(actionSectionRef, () => setIsMenuActive(false));
137137

web/core/components/workspace/sidebar/favorites/favorites-menu.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,33 +87,27 @@ export const SidebarFavoritesMenu = observer(() => {
8787
const sourceData = source.data as TargetData;
8888

8989
if (!sourceData.id) return;
90-
9190
if (isFolder) {
9291
// handle move to a new parent folder if dropped on a folder
9392
if (parentId && parentId !== sourceData.parentId) {
94-
handleMoveToFolder(sourceData.id, parentId);
95-
}
96-
//handle remove from folder if dropped outside of the folder
97-
if (parentId && parentId !== sourceData.parentId && sourceData.isChild) {
98-
handleRemoveFromFavoritesFolder(sourceData.id);
93+
handleMoveToFolder(sourceData.id, parentId); /**parent id */
9994
}
100-
10195
// handle reordering at root level
10296
if (droppedFavId) {
10397
if (instruction != "make-child") {
104-
handleReorder(sourceData.id, droppedFavId, instruction);
98+
handleReorder(sourceData.id, droppedFavId, instruction); /** sequence */
10599
}
106100
}
107101
} else {
108102
//handling reordering for favorites
109103
if (droppedFavId) {
110-
handleReorder(sourceData.id, droppedFavId, instruction);
104+
handleReorder(sourceData.id, droppedFavId, instruction); /** sequence */
111105
}
106+
}
112107

113-
// handle removal from folder if dropped outside a folder
114-
if (!parentId && sourceData.isChild) {
115-
handleRemoveFromFavoritesFolder(sourceData.id);
116-
}
108+
/**remove if dropped outside and source is a child */
109+
if (!parentId && sourceData.isChild) {
110+
handleRemoveFromFavoritesFolder(sourceData.id); /**parent null */
117111
}
118112
};
119113

web/core/store/favorite.store.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,14 @@ export class FavoriteStore implements IFavoriteStore {
174174
* @returns Promise<void>
175175
*/
176176
moveFavoriteToFolder = async (workspaceSlug: string, favoriteId: string, data: Partial<IFavorite>) => {
177-
const oldParent = this.favoriteMap[favoriteId].parent;
178177
try {
178+
await this.favoriteService.updateFavorite(workspaceSlug, favoriteId, data);
179179
runInAction(() => {
180180
// add parent of the favorite
181181
set(this.favoriteMap, [favoriteId, "parent"], data.parent);
182182
});
183-
await this.favoriteService.updateFavorite(workspaceSlug, favoriteId, data);
184183
} catch (error) {
185-
console.error("Failed to move favorite from favorite store");
186-
187-
// revert the changes
188-
runInAction(() => {
189-
if (!data.parent) return;
190-
191-
// revert the parent
192-
set(this.favoriteMap, [favoriteId, "parent"], oldParent);
193-
});
184+
console.error("Failed to move favorite to folder", error);
194185
throw error;
195186
}
196187
};
@@ -201,7 +192,6 @@ export class FavoriteStore implements IFavoriteStore {
201192
destinationId: string,
202193
edge: string | undefined
203194
) => {
204-
const initialSequence = this.favoriteMap[favoriteId].sequence;
205195
try {
206196
let resultSequence = 10000;
207197
if (edge) {
@@ -221,35 +211,27 @@ export class FavoriteStore implements IFavoriteStore {
221211
}
222212
}
223213
}
214+
215+
await this.favoriteService.updateFavorite(workspaceSlug, favoriteId, { sequence: resultSequence });
216+
224217
runInAction(() => {
225218
set(this.favoriteMap, [favoriteId, "sequence"], resultSequence);
226219
});
227-
228-
await this.favoriteService.updateFavorite(workspaceSlug, favoriteId, { sequence: resultSequence });
229220
} catch (error) {
230221
console.error("Failed to move favorite folder");
231-
runInAction(() => {
232-
set(this.favoriteMap, [favoriteId, "sequence"], initialSequence);
233-
throw error;
234-
});
222+
throw error;
235223
}
236224
};
237225

238226
removeFromFavoriteFolder = async (workspaceSlug: string, favoriteId: string) => {
239-
const parent = this.favoriteMap[favoriteId].parent;
240227
try {
228+
await this.favoriteService.updateFavorite(workspaceSlug, favoriteId, { parent: null });
241229
runInAction(() => {
242230
//remove parent
243231
set(this.favoriteMap, [favoriteId, "parent"], null);
244232
});
245-
await this.favoriteService.updateFavorite(workspaceSlug, favoriteId, { parent: null });
246233
} catch (error) {
247234
console.error("Failed to move favorite");
248-
runInAction(() => {
249-
set(this.favoriteMap, [favoriteId, "parent"], parent);
250-
251-
throw error;
252-
});
253235
throw error;
254236
}
255237
};
@@ -384,7 +366,7 @@ export class FavoriteStore implements IFavoriteStore {
384366
set(this.favoriteMap, [favorite.id], favorite);
385367
this.favoriteIds.push(favorite.id);
386368
this.favoriteIds = uniqBy(this.favoriteIds, (id) => id);
387-
favorite.entity_identifier && set(this.entityMap, [favorite.entity_identifier], favorite);
369+
if (favorite.entity_identifier) set(this.entityMap, [favorite.entity_identifier], favorite);
388370
});
389371
});
390372

0 commit comments

Comments
 (0)