Skip to content

Commit c28003a

Browse files
ziomeckaKatarzyna Ziomek-Zdanowicz
and
Katarzyna Ziomek-Zdanowicz
authored
7829 Read hiddenVariables, mode, FNDashboard from grfana's state (#44)
Co-authored-by: Katarzyna Ziomek-Zdanowicz <[email protected]>
1 parent 80a90f4 commit c28003a

File tree

8 files changed

+200
-167
lines changed

8 files changed

+200
-167
lines changed

public/app/core/reducers/fn-slice.ts

+48-38
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
2-
import { WritableDraft } from 'immer/dist/internal';
1+
import { createSlice, PayloadAction, SliceCaseReducers } from '@reduxjs/toolkit';
32

43
import { GrafanaThemeType } from '@grafana/data';
5-
import { dispatch } from 'app/store/store';
64

75
import { AnyObject } from '../../fn-app/types';
86

@@ -14,17 +12,41 @@ export interface FnGlobalState {
1412
controlsContainer: string | null;
1513
pageTitle: string;
1614
queryParams: AnyObject;
17-
hiddenVariables: string[];
15+
hiddenVariables: readonly string[];
1816
}
1917

20-
export type UpdateFNGlobalStateAction = PayloadAction<{
21-
type: keyof FnGlobalState;
22-
payload: FnGlobalState[keyof FnGlobalState];
23-
}>;
18+
export type UpdateFNGlobalStateAction = PayloadAction<Partial<FnGlobalState>>;
19+
20+
export type SetFnStateAction = PayloadAction<Omit<FnGlobalState, 'hiddenVariables'>>;
21+
22+
export type FnPropMappedFromState = Extract<keyof FnGlobalState, 'FNDashboard' | 'hiddenVariables' | 'mode'>;
23+
export type FnStateProp = keyof FnGlobalState;
24+
25+
export type FnPropsMappedFromState = Pick<FnGlobalState, FnPropMappedFromState>;
26+
27+
export const fnStateProps: FnStateProp[] = [
28+
'FNDashboard',
29+
'controlsContainer',
30+
'hiddenVariables',
31+
'mode',
32+
'pageTitle',
33+
'queryParams',
34+
'slug',
35+
'uid',
36+
];
37+
38+
export const fnPropsMappedFromState: readonly FnPropMappedFromState[] = [
39+
'FNDashboard',
40+
'hiddenVariables',
41+
'mode',
42+
] as const;
2443

2544
const INITIAL_MODE = GrafanaThemeType.Light;
2645

27-
const initialState: FnGlobalState = {
46+
export const FN_STATE_KEY = 'fnGlobalState';
47+
48+
export const INITIAL_FN_STATE: FnGlobalState = {
49+
// NOTE: initial value is false
2850
FNDashboard: false,
2951
uid: '',
3052
slug: '',
@@ -33,37 +55,25 @@ const initialState: FnGlobalState = {
3355
pageTitle: '',
3456
queryParams: {},
3557
hiddenVariables: [],
36-
};
58+
} as const;
3759

38-
const fnSlice = createSlice({
39-
name: 'fnGlobalState',
40-
initialState,
41-
reducers: {
42-
setInitialMountState: (state, action: PayloadAction<Omit<FnGlobalState, 'hiddenVariables'>>) => {
43-
return { ...state, ...action.payload };
44-
},
45-
updateFnState: (state: WritableDraft<FnGlobalState>, action: UpdateFNGlobalStateAction) => {
46-
const { type, payload } = action.payload;
47-
48-
return {
49-
...state,
50-
[type]: payload,
51-
};
52-
},
60+
const reducers: SliceCaseReducers<FnGlobalState> = {
61+
updateFnState: (state, action: SetFnStateAction) => {
62+
return { ...state, ...action.payload };
63+
},
64+
updatePartialFnStates: (state, action: UpdateFNGlobalStateAction) => {
65+
return {
66+
...state,
67+
...action.payload,
68+
};
5369
},
70+
};
71+
72+
const fnSlice = createSlice<FnGlobalState, SliceCaseReducers<FnGlobalState>, string>({
73+
name: FN_STATE_KEY,
74+
initialState: INITIAL_FN_STATE,
75+
reducers,
5476
});
5577

56-
export const { updateFnState, setInitialMountState } = fnSlice.actions;
78+
export const { updatePartialFnStates, updateFnState } = fnSlice.actions;
5779
export const fnSliceReducer = fnSlice.reducer;
58-
59-
export const updateFNGlobalState = (
60-
type: keyof FnGlobalState,
61-
payload: UpdateFNGlobalStateAction['payload']['payload']
62-
): void => {
63-
dispatch(
64-
updateFnState({
65-
type,
66-
payload,
67-
})
68-
);
69-
};

public/app/features/dashboard/components/SubMenu/SubMenu.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, { PureComponent } from 'react';
33
import { connect, MapStateToProps } from 'react-redux';
44

55
import { AnnotationQuery, DataQuery } from '@grafana/data';
6+
import { FnGlobalState } from 'app/core/reducers/fn-slice';
67

78
import { StoreState } from '../../../../types';
89
import { getSubMenuVariables, getVariablesState } from '../../../variables/state/selectors';
@@ -18,11 +19,11 @@ interface OwnProps {
1819
dashboard: DashboardModel;
1920
links: DashboardLink[];
2021
annotations: AnnotationQuery[];
21-
hiddenVariables?: string[];
2222
}
2323

2424
interface ConnectedProps {
2525
variables: VariableModel[];
26+
hiddenVariables: FnGlobalState['hiddenVariables'];
2627
}
2728

2829
interface DispatchProps {}
@@ -76,8 +77,10 @@ class SubMenuUnConnected extends PureComponent<Props> {
7677
const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = (state, ownProps) => {
7778
const { uid } = ownProps.dashboard;
7879
const templatingState = getVariablesState(uid, state);
80+
7981
return {
8082
variables: getSubMenuVariables(uid, templatingState.variables),
83+
hiddenVariables: state.fnGlobalState.hiddenVariables,
8184
};
8285
};
8386

public/app/features/dashboard/components/SubMenu/SubMenuItems.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import React, { FunctionComponent, useEffect, useState } from 'react';
22

33
import { selectors } from '@grafana/e2e-selectors';
4+
import { FnGlobalState } from 'app/core/reducers/fn-slice';
45

56
import { PickerRenderer } from '../../../variables/pickers/PickerRenderer';
67
import { VariableHide, VariableModel } from '../../../variables/types';
78

89
interface Props {
910
variables: VariableModel[];
1011
readOnly?: boolean;
11-
hiddenVariables?: string[];
12+
hiddenVariables?: FnGlobalState['hiddenVariables'];
1213
}
1314

1415
export const SubMenuItems: FunctionComponent<Props> = ({ variables, readOnly, hiddenVariables }) => {

public/app/features/dashboard/containers/DashboardPage.tsx

+23-23
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { GrafanaContext, GrafanaContextType } from 'app/core/context/GrafanaCont
1212
import { createErrorNotification } from 'app/core/copy/appNotification';
1313
import { getKioskMode } from 'app/core/navigation/kiosk';
1414
import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
15+
import { FnGlobalState } from 'app/core/reducers/fn-slice';
1516
import { getNavModel } from 'app/core/selectors/navModel';
1617
import { PanelModel } from 'app/features/dashboard/state';
1718
import { dashboardWatcher } from 'app/features/live/dashboard/dashboardWatcher';
@@ -60,7 +61,10 @@ export type DashboardPageRouteSearchParams = {
6061
};
6162

6263
export type MapStateToDashboardPageProps = MapStateToProps<
63-
Pick<DashboardState, 'initPhase' | 'initError'> & { dashboard: ReturnType<DashboardState['getModel']> },
64+
Pick<DashboardState, 'initPhase' | 'initError'> & { dashboard: ReturnType<DashboardState['getModel']> } & Pick<
65+
FnGlobalState,
66+
'FNDashboard'
67+
>,
6468
OwnProps,
6569
StoreState
6670
>;
@@ -80,6 +84,7 @@ export const mapStateToProps: MapStateToDashboardPageProps = (state) => ({
8084
initError: state.dashboard.initError,
8185
dashboard: state.dashboard.getModel(),
8286
navIndex: state.navIndex,
87+
FNDashboard: state.fnGlobalState.FNDashboard,
8388
});
8489

8590
const mapDispatchToProps: MapDispatchToDashboardPageProps = {
@@ -94,9 +99,7 @@ const connector = connect(mapStateToProps, mapDispatchToProps);
9499

95100
type OwnProps = {
96101
isPublic?: boolean;
97-
isFNDashboard?: boolean;
98102
controlsContainer?: string | null;
99-
hiddenVariables?: string[];
100103
fnLoader?: ReactNode;
101104
};
102105

@@ -141,9 +144,11 @@ export class UnthemedDashboardPage extends PureComponent<Props, State> {
141144

142145
componentDidMount() {
143146
this.initDashboard();
144-
const { isPublic, isFNDashboard } = this.props;
145-
if (!isPublic && !isFNDashboard) {
146-
this.forceRouteReloadCounter = (this.props.history.location.state as any)?.routeReloadCounter || 0;
147+
148+
const { isPublic, FNDashboard } = this.props;
149+
150+
if (!isPublic && !FNDashboard) {
151+
this.forceRouteReloadCounter = (this.props.history.location?.state as any)?.routeReloadCounter || 0;
147152
}
148153
}
149154

@@ -157,7 +162,7 @@ export class UnthemedDashboardPage extends PureComponent<Props, State> {
157162
}
158163

159164
initDashboard() {
160-
const { dashboard, isPublic, match, queryParams, isFNDashboard } = this.props;
165+
const { dashboard, isPublic, match, queryParams, FNDashboard } = this.props;
161166

162167
if (dashboard) {
163168
this.closeDashboard();
@@ -170,7 +175,7 @@ export class UnthemedDashboardPage extends PureComponent<Props, State> {
170175
urlFolderId: queryParams.folderId,
171176
panelType: queryParams.panelType,
172177
routeName: this.props.route.routeName,
173-
fixUrl: !isPublic && !isFNDashboard,
178+
fixUrl: !isPublic && !FNDashboard,
174179
accessToken: match.params.accessToken,
175180
keybindingSrv: this.context.keybindings,
176181
});
@@ -180,13 +185,13 @@ export class UnthemedDashboardPage extends PureComponent<Props, State> {
180185
}
181186

182187
componentDidUpdate(prevProps: Props, prevState: State) {
183-
const { dashboard, match, templateVarsChangedInUrl, isPublic, isFNDashboard } = this.props;
188+
const { dashboard, match, templateVarsChangedInUrl, isPublic, FNDashboard } = this.props;
184189

185190
if (!dashboard) {
186191
return;
187192
}
188193

189-
if (!isPublic && !isFNDashboard) {
194+
if (!isPublic && !FNDashboard) {
190195
const routeReloadCounter = (this.props.history.location?.state as any)?.routeReloadCounter;
191196

192197
if (
@@ -372,11 +377,11 @@ export class UnthemedDashboardPage extends PureComponent<Props, State> {
372377
}
373378

374379
render() {
375-
const { dashboard, initError, queryParams, isPublic, isFNDashboard, fnLoader } = this.props;
380+
const { dashboard, initError, queryParams, isPublic, FNDashboard, fnLoader } = this.props;
376381
const { editPanel, viewPanel, updateScrollTop, pageNav, sectionNav } = this.state;
377-
const kioskMode = isFNDashboard ? KioskMode.FN : !isPublic ? getKioskMode(this.props.queryParams) : KioskMode.Full;
382+
const kioskMode = FNDashboard ? KioskMode.FN : !isPublic ? getKioskMode(this.props.queryParams) : KioskMode.Full;
378383

379-
if (!dashboard ) {
384+
if (!dashboard) {
380385
return fnLoader ? <>{fnLoader}</> : <DashboardLoading initPhase={this.props.initPhase} />;
381386
}
382387

@@ -387,7 +392,7 @@ export class UnthemedDashboardPage extends PureComponent<Props, State> {
387392
<header data-testid={selectors.pages.Dashboard.DashNav.navV2}>
388393
<DashNav
389394
dashboard={dashboard}
390-
title={!isFNDashboard ? dashboard.title : ''}
395+
title={!FNDashboard ? dashboard.title : ''}
391396
folderTitle={dashboard.meta.folderTitle}
392397
isFullscreen={!!viewPanel}
393398
onAddPanel={this.onAddPanel}
@@ -420,17 +425,12 @@ export class UnthemedDashboardPage extends PureComponent<Props, State> {
420425
scrollRef={this.setScrollRef}
421426
scrollTop={updateScrollTop}
422427
>
423-
{!isFNDashboard && <DashboardPrompt dashboard={dashboard} />}
428+
{!FNDashboard && <DashboardPrompt dashboard={dashboard} />}
424429

425430
{initError && <DashboardFailed />}
426431
{showSubMenu && (
427432
<section aria-label={selectors.pages.Dashboard.SubMenu.submenu}>
428-
<SubMenu
429-
dashboard={dashboard}
430-
annotations={dashboard.annotations.list}
431-
links={dashboard.links}
432-
hiddenVariables={this.props.hiddenVariables}
433-
/>
433+
<SubMenu dashboard={dashboard} annotations={dashboard.annotations.list} links={dashboard.links} />
434434
</section>
435435
)}
436436

@@ -465,9 +465,9 @@ export class UnthemedDashboardPage extends PureComponent<Props, State> {
465465
}
466466

467467
function updateStatePageNavFromProps(props: Props, state: State): State {
468-
const { dashboard, isFNDashboard } = props;
468+
const { dashboard, FNDashboard } = props;
469469

470-
if (!dashboard || isFNDashboard) {
470+
if (!dashboard || FNDashboard) {
471471
return state;
472472
}
473473

0 commit comments

Comments
 (0)