mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
New Dashboard: Fix "build a dashboard" when empty dash page feature is enabled (#66816)
* New Dashboard: Fix "build a dashboard" when used with empty dash page feature Closes #66659
This commit is contained in:
@@ -12,6 +12,9 @@ import {
|
|||||||
onCreateNewRow,
|
onCreateNewRow,
|
||||||
onPasteCopiedPanel,
|
onPasteCopiedPanel,
|
||||||
} from 'app/features/dashboard/utils/dashboard';
|
} from 'app/features/dashboard/utils/dashboard';
|
||||||
|
import { useDispatch, useSelector } from 'app/types';
|
||||||
|
|
||||||
|
import { setInitialDatasource } from '../../state/reducers';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
dashboard: DashboardModel;
|
dashboard: DashboardModel;
|
||||||
@@ -19,6 +22,8 @@ interface Props {
|
|||||||
|
|
||||||
export const AddPanelMenu = ({ dashboard }: Props) => {
|
export const AddPanelMenu = ({ dashboard }: Props) => {
|
||||||
const copiedPanelPlugin = useMemo(() => getCopiedPanelPlugin(), []);
|
const copiedPanelPlugin = useMemo(() => getCopiedPanelPlugin(), []);
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
const initialDatasource = useSelector((state) => state.dashboard.initialDatasource);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Menu>
|
<Menu>
|
||||||
@@ -27,9 +32,10 @@ export const AddPanelMenu = ({ dashboard }: Props) => {
|
|||||||
label={t('dashboard.add-menu.visualization', 'Visualization')}
|
label={t('dashboard.add-menu.visualization', 'Visualization')}
|
||||||
testId={selectors.components.PageToolbar.itemButton('Add new visualization menu item')}
|
testId={selectors.components.PageToolbar.itemButton('Add new visualization menu item')}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
const id = onCreateNewPanel(dashboard, initialDatasource);
|
||||||
reportInteraction('dashboards_toolbar_add_clicked', { item: 'add_visualization' });
|
reportInteraction('dashboards_toolbar_add_clicked', { item: 'add_visualization' });
|
||||||
const id = onCreateNewPanel(dashboard);
|
|
||||||
locationService.partial({ editPanel: id });
|
locationService.partial({ editPanel: id });
|
||||||
|
dispatch(setInitialDatasource(undefined));
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<Menu.Item
|
<Menu.Item
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
|
|
||||||
import { getDataSourceSrv, locationService } from '@grafana/runtime';
|
import { config, getDataSourceSrv, locationService } from '@grafana/runtime';
|
||||||
import { Page } from 'app/core/components/Page/Page';
|
import { Page } from 'app/core/components/Page/Page';
|
||||||
import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
|
import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
|
||||||
|
import { useDispatch } from 'app/types';
|
||||||
|
|
||||||
import { getNewDashboardModelData, setDashboardToFetchFromLocalStorage } from '../state/initDashboard';
|
import { getNewDashboardModelData, setDashboardToFetchFromLocalStorage } from '../state/initDashboard';
|
||||||
|
import { setInitialDatasource } from '../state/reducers';
|
||||||
|
|
||||||
export default function NewDashboardWithDS(props: GrafanaRouteComponentProps<{ datasourceUid: string }>) {
|
export default function NewDashboardWithDS(props: GrafanaRouteComponentProps<{ datasourceUid: string }>) {
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const { datasourceUid } = props.match.params;
|
const { datasourceUid } = props.match.params;
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const ds = getDataSourceSrv().getInstanceSettings(datasourceUid);
|
const ds = getDataSourceSrv().getInstanceSettings(datasourceUid);
|
||||||
@@ -17,19 +20,24 @@ export default function NewDashboardWithDS(props: GrafanaRouteComponentProps<{ d
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const newDashboard = getNewDashboardModelData();
|
if (!config.featureToggles.emptyDashboardPage) {
|
||||||
const { dashboard } = newDashboard;
|
const newDashboard = getNewDashboardModelData();
|
||||||
dashboard.panels[0] = {
|
const { dashboard } = newDashboard;
|
||||||
...dashboard.panels[0],
|
dashboard.panels[0] = {
|
||||||
datasource: {
|
...dashboard.panels[0],
|
||||||
uid: ds.uid,
|
datasource: {
|
||||||
type: ds.type,
|
uid: ds.uid,
|
||||||
},
|
type: ds.type,
|
||||||
};
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
setDashboardToFetchFromLocalStorage(newDashboard);
|
||||||
|
} else {
|
||||||
|
dispatch(setInitialDatasource(datasourceUid));
|
||||||
|
}
|
||||||
|
|
||||||
setDashboardToFetchFromLocalStorage(newDashboard);
|
|
||||||
locationService.replace('/dashboard/new');
|
locationService.replace('/dashboard/new');
|
||||||
}, [datasourceUid]);
|
}, [datasourceUid, dispatch]);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ import { Button, useStyles2 } from '@grafana/ui';
|
|||||||
import { Trans } from 'app/core/internationalization';
|
import { Trans } from 'app/core/internationalization';
|
||||||
import { DashboardModel } from 'app/features/dashboard/state';
|
import { DashboardModel } from 'app/features/dashboard/state';
|
||||||
import { onAddLibraryPanel, onCreateNewPanel, onCreateNewRow } from 'app/features/dashboard/utils/dashboard';
|
import { onAddLibraryPanel, onCreateNewPanel, onCreateNewRow } from 'app/features/dashboard/utils/dashboard';
|
||||||
|
import { useDispatch, useSelector } from 'app/types';
|
||||||
|
|
||||||
|
import { setInitialDatasource } from '../state/reducers';
|
||||||
|
|
||||||
export interface Props {
|
export interface Props {
|
||||||
dashboard: DashboardModel;
|
dashboard: DashboardModel;
|
||||||
@@ -15,6 +18,8 @@ export interface Props {
|
|||||||
|
|
||||||
export const DashboardEmpty = ({ dashboard, canCreate }: Props) => {
|
export const DashboardEmpty = ({ dashboard, canCreate }: Props) => {
|
||||||
const styles = useStyles2(getStyles);
|
const styles = useStyles2(getStyles);
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
const initialDatasource = useSelector((state) => state.dashboard.initialDatasource);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.centeredContent}>
|
<div className={styles.centeredContent}>
|
||||||
@@ -36,9 +41,10 @@ export const DashboardEmpty = ({ dashboard, canCreate }: Props) => {
|
|||||||
icon="plus"
|
icon="plus"
|
||||||
aria-label="Add new panel"
|
aria-label="Add new panel"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
const id = onCreateNewPanel(dashboard, initialDatasource);
|
||||||
reportInteraction('dashboards_emptydashboard_clicked', { item: 'add_visualization' });
|
reportInteraction('dashboards_emptydashboard_clicked', { item: 'add_visualization' });
|
||||||
const id = onCreateNewPanel(dashboard);
|
|
||||||
locationService.partial({ editPanel: id });
|
locationService.partial({ editPanel: id });
|
||||||
|
dispatch(setInitialDatasource(undefined));
|
||||||
}}
|
}}
|
||||||
disabled={!canCreate}
|
disabled={!canCreate}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ export const initialState: DashboardState = {
|
|||||||
getModel: () => null,
|
getModel: () => null,
|
||||||
permissions: [],
|
permissions: [],
|
||||||
initError: null,
|
initError: null,
|
||||||
|
initialDatasource: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
const dashboardSlice = createSlice({
|
const dashboardSlice = createSlice({
|
||||||
@@ -53,6 +54,9 @@ const dashboardSlice = createSlice({
|
|||||||
addPanel: (state, action: PayloadAction<PanelModel>) => {
|
addPanel: (state, action: PayloadAction<PanelModel>) => {
|
||||||
//state.panels[action.payload.id] = { pluginId: action.payload.type };
|
//state.panels[action.payload.id] = { pluginId: action.payload.type };
|
||||||
},
|
},
|
||||||
|
setInitialDatasource: (state, action: PayloadAction<string | undefined>) => {
|
||||||
|
state.initialDatasource = action.payload;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -79,6 +83,7 @@ export const {
|
|||||||
dashboardInitServices,
|
dashboardInitServices,
|
||||||
cleanUpDashboard,
|
cleanUpDashboard,
|
||||||
addPanel,
|
addPanel,
|
||||||
|
setInitialDatasource,
|
||||||
} = dashboardSlice.actions;
|
} = dashboardSlice.actions;
|
||||||
|
|
||||||
export const dashboardReducer = dashboardSlice.reducer;
|
export const dashboardReducer = dashboardSlice.reducer;
|
||||||
|
|||||||
@@ -7,11 +7,12 @@ import store from 'app/core/store';
|
|||||||
import { DashboardModel, PanelModel } from 'app/features/dashboard/state';
|
import { DashboardModel, PanelModel } from 'app/features/dashboard/state';
|
||||||
import { calculateNewPanelGridPos } from 'app/features/dashboard/utils/panel';
|
import { calculateNewPanelGridPos } from 'app/features/dashboard/utils/panel';
|
||||||
|
|
||||||
export function onCreateNewPanel(dashboard: DashboardModel): number | undefined {
|
export function onCreateNewPanel(dashboard: DashboardModel, datasource?: string): number | undefined {
|
||||||
const newPanel: Partial<PanelModel> = {
|
const newPanel: Partial<PanelModel> = {
|
||||||
type: 'timeseries',
|
type: 'timeseries',
|
||||||
title: 'Panel Title',
|
title: 'Panel Title',
|
||||||
gridPos: calculateNewPanelGridPos(dashboard),
|
gridPos: calculateNewPanelGridPos(dashboard),
|
||||||
|
datasource: datasource ? { uid: datasource } : null,
|
||||||
isNew: true,
|
isNew: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { DataQuery } from '@grafana/data';
|
import { DataQuery } from '@grafana/data';
|
||||||
import { Dashboard } from '@grafana/schema';
|
import { Dashboard, DataSourceRef } from '@grafana/schema';
|
||||||
import { DashboardModel } from 'app/features/dashboard/state/DashboardModel';
|
import { DashboardModel } from 'app/features/dashboard/state/DashboardModel';
|
||||||
|
|
||||||
import { DashboardAcl } from './acl';
|
import { DashboardAcl } from './acl';
|
||||||
@@ -101,6 +101,7 @@ export interface QueriesToUpdateOnDashboardLoad {
|
|||||||
export interface DashboardState {
|
export interface DashboardState {
|
||||||
getModel: GetMutableDashboardModelFn;
|
getModel: GetMutableDashboardModelFn;
|
||||||
initPhase: DashboardInitPhase;
|
initPhase: DashboardInitPhase;
|
||||||
|
initialDatasource?: DataSourceRef['uid'];
|
||||||
initError: DashboardInitError | null;
|
initError: DashboardInitError | null;
|
||||||
permissions: DashboardAcl[];
|
permissions: DashboardAcl[];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user