mirror of
https://github.com/grafana/grafana.git
synced 2024-11-30 12:44:10 -06:00
Navigation: split logic, reenable unit test, copy whole node to prevent recursion error (#53033)
* split logic, reenable unit test, copy whole node to prevent recursion error * remove double assignment
This commit is contained in:
parent
3f58e8ec2a
commit
f64b241d32
@ -42,8 +42,7 @@ describe('getNavModel', () => {
|
||||
expect(navModel.node.parentItem?.id).toBe(navModel.main.id);
|
||||
});
|
||||
|
||||
// TODO reenable this test once we figure out the logic for 2nd level children
|
||||
test.skip('returns the correct nav model for a 2nd-level child', () => {
|
||||
test('returns the correct nav model for a 2nd-level child', () => {
|
||||
const navModel = getNavModel(navIndex, 'apps/subapp/child1');
|
||||
expect(navModel.main.id).toBe('apps');
|
||||
expect(navModel.node.id).toBe('apps/subapp/child1');
|
||||
|
@ -18,10 +18,11 @@ const getNotFoundModel = (): NavModel => {
|
||||
export const getNavModel = (navIndex: NavIndex, id: string, fallback?: NavModel, onlyChild = false): NavModel => {
|
||||
if (navIndex[id]) {
|
||||
const node = navIndex[id];
|
||||
const main = onlyChild ? node : getSectionRoot(node);
|
||||
const nodeWithActive = enrichNodeWithActiveState(node);
|
||||
const main = onlyChild ? nodeWithActive : getSectionRoot(nodeWithActive);
|
||||
|
||||
return {
|
||||
node,
|
||||
node: nodeWithActive,
|
||||
main,
|
||||
};
|
||||
}
|
||||
@ -34,23 +35,28 @@ export const getNavModel = (navIndex: NavIndex, id: string, fallback?: NavModel,
|
||||
};
|
||||
|
||||
function getSectionRoot(node: NavModelItem): NavModelItem {
|
||||
if (!node.parentItem) {
|
||||
return node;
|
||||
return node.parentItem ? getSectionRoot(node.parentItem) : node;
|
||||
}
|
||||
|
||||
function enrichNodeWithActiveState(node: NavModelItem): NavModelItem {
|
||||
const nodeCopy = { ...node };
|
||||
if (nodeCopy.parentItem) {
|
||||
nodeCopy.parentItem = { ...nodeCopy.parentItem };
|
||||
const root = nodeCopy.parentItem;
|
||||
|
||||
if (root.children) {
|
||||
root.children = root.children.map((item) => {
|
||||
if (item.id === node.id) {
|
||||
return { ...nodeCopy, active: true };
|
||||
}
|
||||
|
||||
return item;
|
||||
});
|
||||
}
|
||||
|
||||
nodeCopy.parentItem = enrichNodeWithActiveState(root);
|
||||
}
|
||||
|
||||
const root = (node.parentItem = { ...node.parentItem });
|
||||
|
||||
if (root.children) {
|
||||
root.children = root.children.map((item) => {
|
||||
if (item.id === node.id) {
|
||||
return { ...item, active: true };
|
||||
}
|
||||
|
||||
return item;
|
||||
});
|
||||
}
|
||||
|
||||
return getSectionRoot(root);
|
||||
return nodeCopy;
|
||||
}
|
||||
|
||||
export const getTitleFromNavModel = (navModel: NavModel) => {
|
||||
|
Loading…
Reference in New Issue
Block a user