2020-01-13 01:03:22 -06:00
|
|
|
import { AnyAction, createAction } from '@reduxjs/toolkit';
|
2020-07-03 01:53:54 -05:00
|
|
|
import { NavIndex, NavModel, NavModelItem } from '@grafana/data';
|
2020-01-13 01:03:22 -06:00
|
|
|
|
2018-08-31 06:24:36 -05:00
|
|
|
import config from 'app/core/config';
|
|
|
|
|
2018-09-02 12:36:36 -05:00
|
|
|
export function buildInitialState(): NavIndex {
|
|
|
|
const navIndex: NavIndex = {};
|
|
|
|
const rootNodes = config.bootData.navTree as NavModelItem[];
|
|
|
|
buildNavIndex(navIndex, rootNodes);
|
|
|
|
return navIndex;
|
2018-08-31 06:24:36 -05:00
|
|
|
}
|
|
|
|
|
2018-09-02 12:36:36 -05:00
|
|
|
function buildNavIndex(navIndex: NavIndex, children: NavModelItem[], parentItem?: NavModelItem) {
|
|
|
|
for (const node of children) {
|
Chore: Fix all Typescript strict null errors (#26204)
* 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>
2020-07-10 05:46:59 -05:00
|
|
|
navIndex[node.id!] = {
|
2018-09-02 12:36:36 -05:00
|
|
|
...node,
|
|
|
|
parentItem: parentItem,
|
|
|
|
};
|
2018-08-31 06:24:36 -05:00
|
|
|
|
2018-09-02 12:36:36 -05:00
|
|
|
if (node.children) {
|
|
|
|
buildNavIndex(navIndex, node.children, node);
|
2018-08-31 06:24:36 -05:00
|
|
|
}
|
|
|
|
}
|
2020-07-03 01:53:54 -05:00
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
2018-09-02 12:36:36 -05:00
|
|
|
}
|
2018-08-31 06:24:36 -05:00
|
|
|
|
2020-01-13 01:03:22 -06:00
|
|
|
export const initialState: NavIndex = {};
|
|
|
|
|
|
|
|
export const updateNavIndex = createAction<NavModelItem>('navIndex/updateNavIndex');
|
2020-08-07 02:00:44 -05:00
|
|
|
// Since the configuration subtitle includes the organization name, we include this action to update the org name if it changes.
|
|
|
|
export const updateConfigurationSubtitle = createAction<string>('navIndex/updateConfigurationSubtitle');
|
|
|
|
|
|
|
|
export const getItemWithNewSubTitle = (item: NavModelItem, subTitle: string): NavModelItem => ({
|
|
|
|
...item,
|
|
|
|
parentItem: {
|
|
|
|
...item.parentItem,
|
|
|
|
text: item.parentItem?.text ?? '',
|
|
|
|
subTitle,
|
|
|
|
},
|
|
|
|
});
|
2018-09-02 12:36:36 -05:00
|
|
|
|
2020-01-13 01:03:22 -06:00
|
|
|
// 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;
|
2018-09-11 07:14:03 -05:00
|
|
|
|
Chore: Fix all Typescript strict null errors (#26204)
* 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>
2020-07-10 05:46:59 -05:00
|
|
|
for (const node of payload.children!) {
|
|
|
|
newPages[node.id!] = {
|
2020-01-13 01:03:22 -06:00
|
|
|
...node,
|
|
|
|
parentItem: payload,
|
|
|
|
};
|
|
|
|
}
|
2018-09-11 07:14:03 -05:00
|
|
|
|
2020-01-13 01:03:22 -06:00
|
|
|
return { ...state, ...newPages };
|
2020-08-07 02:00:44 -05:00
|
|
|
} else if (updateConfigurationSubtitle.match(action)) {
|
|
|
|
const subTitle = `Organization: ${action.payload}`;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
cfg: { ...state.cfg, subTitle },
|
|
|
|
datasources: getItemWithNewSubTitle(state.datasources, subTitle),
|
|
|
|
users: getItemWithNewSubTitle(state.users, subTitle),
|
|
|
|
teams: getItemWithNewSubTitle(state.teams, subTitle),
|
|
|
|
plugins: getItemWithNewSubTitle(state.plugins, subTitle),
|
|
|
|
'org-settings': getItemWithNewSubTitle(state['org-settings'], subTitle),
|
|
|
|
apikeys: getItemWithNewSubTitle(state.apikeys, subTitle),
|
|
|
|
};
|
2018-09-11 07:14:03 -05:00
|
|
|
}
|
2020-01-13 01:03:22 -06:00
|
|
|
|
2018-08-31 06:24:36 -05:00
|
|
|
return state;
|
|
|
|
};
|