Skip to content

Commit

Permalink
fix: menu rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
pReya committed Nov 21, 2024
1 parent 3cd2be6 commit 164472a
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/util/CommonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,42 @@ const CommonUtils = {
return baseUrl;
},

rebaseSingleMenuItem: (item: IMenuItem, baseUrl: string): IMenuItem => {
let itemWithoutChildren;

if (item.target?.startsWith("https") || item.target?.startsWith("mailto")) {
itemWithoutChildren = {
...item,
};
} else {
itemWithoutChildren = {
...item,
...(item.target
? {
target: baseUrl === "/" ? item.target : baseUrl + item.target,
}
: {}),
};
}

if (item.children?.length) {
return {
...itemWithoutChildren,
children: item.children.map((child) =>
CommonUtils.rebaseSingleMenuItem(child, baseUrl)
),
};
}
return itemWithoutChildren;
},

/**
* Attach baseURl to all Menu items
* */
rebaseMenu: (baseUrl: string, menu: IMenuItem[]): IMenuItem[] => {
return menu?.map((menuItem) => ({
...menuItem,
...(menuItem.target
? {
target:
baseUrl === "/" ? menuItem.target : baseUrl + menuItem.target,
}
: {}),
...(menuItem.children?.length
? {
children: menuItem.children?.map((subMenuItem) => ({
...subMenuItem,
target:
baseUrl === "/" ? subMenuItem.target : baseUrl + subMenuItem.target,
})),
}
: {}),
}));
return menu?.map((menuItem) =>
CommonUtils.rebaseSingleMenuItem(menuItem, baseUrl)
);
},
};

Expand Down

0 comments on commit 164472a

Please sign in to comment.