Skip to content

Commit

Permalink
chore: Refactor NavigationManager to improve navigation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
edmdz committed Aug 2, 2024
1 parent ce7d86a commit 9762e7a
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/components/NavigationManager.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@ const NavigationManager = ({ initialComponent, initialTitle }) => {
setNavigationStack(updater);
}, []);

const navigate = useCallback((component, title) => {
updateStack(prevStack => {
const existingIndex = prevStack.findIndex(item => extractMainTitle(item.title) === extractMainTitle(title));
return existingIndex !== -1 ? prevStack.slice(0, existingIndex + 1) : [...prevStack, { component, title }];
});
}, [updateStack]);

const navigateBack = useCallback((stepsBack = 1) => {
updateStack(prevStack => prevStack.slice(0, -stepsBack));
}, [updateStack]);

const navigate = useCallback((component, title) => {
const existingIndex = navigationStack.findIndex(item => extractMainTitle(item.title) === extractMainTitle(title));
if (existingIndex !== -1) {
const stepsBack = navigationStack.length - 1 - existingIndex;
if (stepsBack > 0) {
navigateBack(stepsBack);
}
} else {
updateStack(prevStack => [...prevStack, { component, title }]);
}
}, [navigationStack, navigateBack]);

const resetNavigation = useCallback(() => {
updateStack(prevStack => [prevStack[0]]);
}, [updateStack]);
Expand Down

0 comments on commit 9762e7a

Please sign in to comment.