Favorite: computeNewPosition fixed (#2134)

This commit is contained in:
Abhishek Thory
2023-10-21 04:31:32 -05:00
committed by GitHub
parent dee9807eb3
commit 159d2978d0

View File

@ -104,7 +104,7 @@ export const useFavorites = () => {
});
};
const computeNewPosition = (destIndex: number) => {
const computeNewPosition = (destIndex: number, sourceIndex: number) => {
if (destIndex === 0) {
return favorites[destIndex].position / 2;
}
@ -112,6 +112,13 @@ export const useFavorites = () => {
if (destIndex === favorites.length - 1) {
return favorites[destIndex].position + 1;
}
if (sourceIndex < destIndex) {
return (
(favorites[destIndex + 1].position + favorites[destIndex].position) / 2
);
}
return (
(favorites[destIndex - 1].position + favorites[destIndex].position) / 2
);
@ -121,7 +128,10 @@ export const useFavorites = () => {
if (!result.destination || !favorites) {
return;
}
const newPosition = computeNewPosition(result.destination.index);
const newPosition = computeNewPosition(
result.destination.index,
result.source.index,
);
const reorderFavorites = Array.from(favorites);
const [removed] = reorderFavorites.splice(result.source.index, 1);