mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Chore: Fix typescript strict null errors * Added new limit * Fixed ts issue * fixed tests * trying to fix type inference * Fixing more ts errors * Revert tsconfig option * Fix * Fixed code * More fixes * fix tests * Updated snapshot * Chore: More ts strict null fixes * More fixes in some really messed up azure config components * More fixes, current count: 441 * 419 * More fixes * Fixed invalid initial state in explore * Fixing tests * Fixed tests * Explore fix * More fixes * Progress * Sub 300 * Now at 218 * Progress * Update * Progress * Updated tests * at 159 * fixed tests * Progress * YAy blow 100! at 94 * 10,9,8,7,6,5,4,3,2,1... lift off * Fixed tests * Fixed more type errors Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
import { AnyAction, createAction } from '@reduxjs/toolkit';
|
|
import { NavIndex, NavModel, NavModelItem } from '@grafana/data';
|
|
|
|
import config from 'app/core/config';
|
|
|
|
export function buildInitialState(): NavIndex {
|
|
const navIndex: NavIndex = {};
|
|
const rootNodes = config.bootData.navTree as NavModelItem[];
|
|
buildNavIndex(navIndex, rootNodes);
|
|
return navIndex;
|
|
}
|
|
|
|
function buildNavIndex(navIndex: NavIndex, children: NavModelItem[], parentItem?: NavModelItem) {
|
|
for (const node of children) {
|
|
navIndex[node.id!] = {
|
|
...node,
|
|
parentItem: parentItem,
|
|
};
|
|
|
|
if (node.children) {
|
|
buildNavIndex(navIndex, node.children, node);
|
|
}
|
|
}
|
|
|
|
navIndex['not-found'] = { ...buildWarningNav('Page not found', '404 Error').node };
|
|
}
|
|
|
|
function buildWarningNav(text: string, subTitle?: string): NavModel {
|
|
const node = {
|
|
text,
|
|
subTitle,
|
|
icon: 'exclamation-triangle',
|
|
};
|
|
return {
|
|
breadcrumbs: [node],
|
|
node: node,
|
|
main: node,
|
|
};
|
|
}
|
|
|
|
export const initialState: NavIndex = {};
|
|
|
|
export const updateNavIndex = createAction<NavModelItem>('navIndex/updateNavIndex');
|
|
|
|
// Redux Toolkit uses ImmerJs as part of their solution to ensure that state objects are not mutated.
|
|
// ImmerJs has an autoFreeze option that freezes objects from change which means this reducer can't be migrated to createSlice
|
|
// because the state would become frozen and during run time we would get errors because Angular would try to mutate
|
|
// the frozen state.
|
|
// https://github.com/reduxjs/redux-toolkit/issues/242
|
|
export const navIndexReducer = (state: NavIndex = initialState, action: AnyAction): NavIndex => {
|
|
if (updateNavIndex.match(action)) {
|
|
const newPages: NavIndex = {};
|
|
const payload = action.payload;
|
|
|
|
for (const node of payload.children!) {
|
|
newPages[node.id!] = {
|
|
...node,
|
|
parentItem: payload,
|
|
};
|
|
}
|
|
|
|
return { ...state, ...newPages };
|
|
}
|
|
|
|
return state;
|
|
};
|