-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Dashboard] [Collapsable Panels] Switch to using props (#200793)
Closes #200090 ## Summary This PR migrates the `GridLayout` component a more traditional React design using **props** rather than providing an API. This change serves two purposes: 1. It makes the eventual Dashboard migration easier, since it is more similar to `react-grid-layout`'s implementation 3. It makes the `GridLayout` component less opinionated by moving the logic for panel management (i.e. panel placement, etc) to the parent component. I tried to keep efficiency in mind for this comparison, and ensured that we are still keeping the number of rerenders **o a minimum**. This PR should not introduce **any** extra renders in comparison to the API version. ### Checklist - [x] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) ### Identify risks There are no risks to this PR, since all work is contained in the `examples` plugin. --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
cba99de
commit 5495322
Showing
13 changed files
with
525 additions
and
438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
export interface DashboardGridData { | ||
w: number; | ||
h: number; | ||
x: number; | ||
y: number; | ||
i: string; | ||
} | ||
|
||
export interface MockedDashboardPanelMap { | ||
[key: string]: { id: string; gridData: DashboardGridData & { row: number } }; | ||
} | ||
|
||
export type MockedDashboardRowMap = Array<{ title: string; collapsed: boolean }>; | ||
|
||
export interface MockSerializedDashboardState { | ||
panels: MockedDashboardPanelMap; | ||
rows: MockedDashboardRowMap; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import { cloneDeep } from 'lodash'; | ||
import { useMemo } from 'react'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
|
||
import { | ||
MockSerializedDashboardState, | ||
MockedDashboardPanelMap, | ||
MockedDashboardRowMap, | ||
} from './types'; | ||
|
||
const DASHBOARD_GRID_COLUMN_COUNT = 48; | ||
const DEFAULT_PANEL_HEIGHT = 15; | ||
const DEFAULT_PANEL_WIDTH = DASHBOARD_GRID_COLUMN_COUNT / 2; | ||
|
||
export const useMockDashboardApi = ({ | ||
savedState, | ||
}: { | ||
savedState: MockSerializedDashboardState; | ||
}) => { | ||
const mockDashboardApi = useMemo(() => { | ||
return { | ||
viewMode: new BehaviorSubject('edit'), | ||
panels$: new BehaviorSubject<MockedDashboardPanelMap>(savedState.panels), | ||
rows$: new BehaviorSubject<MockedDashboardRowMap>(savedState.rows), | ||
removePanel: (id: string) => { | ||
const panels = { ...mockDashboardApi.panels$.getValue() }; | ||
delete panels[id]; // the grid layout component will handle compacting, if necessary | ||
mockDashboardApi.panels$.next(panels); | ||
}, | ||
replacePanel: (oldId: string, newId: string) => { | ||
const currentPanels = mockDashboardApi.panels$.getValue(); | ||
const otherPanels = { ...currentPanels }; | ||
const oldPanel = currentPanels[oldId]; | ||
delete otherPanels[oldId]; | ||
otherPanels[newId] = { id: newId, gridData: { ...oldPanel.gridData, i: newId } }; | ||
mockDashboardApi.panels$.next(otherPanels); | ||
}, | ||
addNewPanel: ({ id: newId }: { id: string }) => { | ||
// we are only implementing "place at top" here, for demo purposes | ||
const currentPanels = mockDashboardApi.panels$.getValue(); | ||
const otherPanels = { ...currentPanels }; | ||
for (const [id, panel] of Object.entries(currentPanels)) { | ||
const currentPanel = cloneDeep(panel); | ||
currentPanel.gridData.y = currentPanel.gridData.y + DEFAULT_PANEL_HEIGHT; | ||
otherPanels[id] = currentPanel; | ||
} | ||
mockDashboardApi.panels$.next({ | ||
...otherPanels, | ||
[newId]: { | ||
id: newId, | ||
gridData: { | ||
i: newId, | ||
row: 0, | ||
x: 0, | ||
y: 0, | ||
w: DEFAULT_PANEL_WIDTH, | ||
h: DEFAULT_PANEL_HEIGHT, | ||
}, | ||
}, | ||
}); | ||
}, | ||
canRemovePanels: () => true, | ||
}; | ||
// only run onMount | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return mockDashboardApi; | ||
}; |
Oops, something went wrong.