mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* change home id to be more unique, refactor so that home circular reference is never created * prefix starred dashboards * update reducer
55 lines
1.9 KiB
TypeScript
55 lines
1.9 KiB
TypeScript
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|
|
|
import { NavModelItem } from '@grafana/data';
|
|
import { config } from '@grafana/runtime';
|
|
|
|
export const initialState: NavModelItem[] = config.bootData?.navTree ?? [];
|
|
|
|
// this matches the prefix set in the backend navtree
|
|
export const ID_PREFIX = 'starred/';
|
|
|
|
const navTreeSlice = createSlice({
|
|
name: 'navBarTree',
|
|
initialState,
|
|
reducers: {
|
|
setStarred: (state, action: PayloadAction<{ id: string; title: string; url: string; isStarred: boolean }>) => {
|
|
const starredItems = state.find((navItem) => navItem.id === 'starred');
|
|
const { id, title, url, isStarred } = action.payload;
|
|
if (starredItems) {
|
|
if (isStarred) {
|
|
if (!starredItems.children) {
|
|
starredItems.children = [];
|
|
}
|
|
const newStarredItem: NavModelItem = {
|
|
id: ID_PREFIX + id,
|
|
text: title,
|
|
url,
|
|
};
|
|
starredItems.children.push(newStarredItem);
|
|
starredItems.children.sort((a, b) => a.text.localeCompare(b.text));
|
|
} else {
|
|
const index = starredItems.children?.findIndex((item) => item.id === ID_PREFIX + id) ?? -1;
|
|
if (index > -1) {
|
|
starredItems?.children?.splice(index, 1);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
updateDashboardName: (state, action: PayloadAction<{ id: string; title: string; url: string }>) => {
|
|
const { id, title, url } = action.payload;
|
|
const starredItems = state.find((navItem) => navItem.id === 'starred');
|
|
if (starredItems) {
|
|
const navItem = starredItems.children?.find((navItem) => navItem.id === id);
|
|
if (navItem) {
|
|
navItem.text = title;
|
|
navItem.url = url;
|
|
starredItems.children?.sort((a, b) => a.text.localeCompare(b.text));
|
|
}
|
|
}
|
|
},
|
|
},
|
|
});
|
|
|
|
export const { setStarred, updateDashboardName } = navTreeSlice.actions;
|
|
export const navTreeReducer = navTreeSlice.reducer;
|