mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -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);
|
expect(navModel.node.parentItem?.id).toBe(navModel.main.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO reenable this test once we figure out the logic for 2nd level children
|
test('returns the correct nav model for a 2nd-level child', () => {
|
||||||
test.skip('returns the correct nav model for a 2nd-level child', () => {
|
|
||||||
const navModel = getNavModel(navIndex, 'apps/subapp/child1');
|
const navModel = getNavModel(navIndex, 'apps/subapp/child1');
|
||||||
expect(navModel.main.id).toBe('apps');
|
expect(navModel.main.id).toBe('apps');
|
||||||
expect(navModel.node.id).toBe('apps/subapp/child1');
|
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 => {
|
export const getNavModel = (navIndex: NavIndex, id: string, fallback?: NavModel, onlyChild = false): NavModel => {
|
||||||
if (navIndex[id]) {
|
if (navIndex[id]) {
|
||||||
const node = navIndex[id];
|
const node = navIndex[id];
|
||||||
const main = onlyChild ? node : getSectionRoot(node);
|
const nodeWithActive = enrichNodeWithActiveState(node);
|
||||||
|
const main = onlyChild ? nodeWithActive : getSectionRoot(nodeWithActive);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
node,
|
node: nodeWithActive,
|
||||||
main,
|
main,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -34,23 +35,28 @@ export const getNavModel = (navIndex: NavIndex, id: string, fallback?: NavModel,
|
|||||||
};
|
};
|
||||||
|
|
||||||
function getSectionRoot(node: NavModelItem): NavModelItem {
|
function getSectionRoot(node: NavModelItem): NavModelItem {
|
||||||
if (!node.parentItem) {
|
return node.parentItem ? getSectionRoot(node.parentItem) : node;
|
||||||
return 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);
|
||||||
}
|
}
|
||||||
|
return nodeCopy;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getTitleFromNavModel = (navModel: NavModel) => {
|
export const getTitleFromNavModel = (navModel: NavModel) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user