Skip to content

Commit

Permalink
chore: add emitter for navigateBack hook
Browse files Browse the repository at this point in the history
  • Loading branch information
dennyscode committed Sep 25, 2024
1 parent 9277844 commit 9af81ee
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/widget/src/hooks/useNavigateBack.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import { useCallback } from 'react';
import { useLocation } from 'react-router-dom';
import { WidgetEvent } from '../types/events.js';
import type { NavigationRouteType } from '../utils/navigationRoutes.js';
import { useNavigate } from './useNavigate.js';
import { useWidgetEvents } from './useWidgetEvents.js';

const getPreviousPath = (pathname: string) => {
// cut current page from pathname
const removeCurrentPage = pathname.substring(0, pathname.lastIndexOf('/'));
// get 2nd last page from pathname
const formattedString = removeCurrentPage.substring(
removeCurrentPage.lastIndexOf('/') + 1,
);
return formattedString;
};

export const useNavigateBack = () => {
const navigate = useNavigate();
const emitter = useWidgetEvents();
const location = useLocation();
const path = getPreviousPath(location.pathname);

const navigateBack = useCallback(() => {
// TODO: find a better router with nested memory routers support
Expand All @@ -11,6 +28,10 @@ export const useNavigateBack = () => {
//
// if (window.history.length > 2) {
navigate(-1);
if (path) {
emitter.emit(WidgetEvent.PageEntered, path as NavigationRouteType);
}

// } else {
// navigate(
// window.location.pathname.substring(
Expand All @@ -20,7 +41,7 @@ export const useNavigateBack = () => {
// { replace: true },
// );
// }
}, [navigate]);
}, [emitter, navigate, path]);

return { navigateBack, navigate };
};

0 comments on commit 9af81ee

Please sign in to comment.