2022-06-07 16:14:49 +05:30
|
|
|
import { PlusOutlined, SaveFilled } from '@ant-design/icons';
|
2023-07-25 16:25:36 +03:00
|
|
|
import { PANEL_TYPES } from 'constants/queryBuilder';
|
2022-06-07 16:14:49 +05:30
|
|
|
import useComponentPermission from 'hooks/useComponentPermission';
|
2023-01-11 14:39:06 +05:30
|
|
|
import { useIsDarkMode } from 'hooks/useDarkMode';
|
2023-05-19 13:14:32 +05:30
|
|
|
import { Dispatch, SetStateAction } from 'react';
|
2022-06-07 16:14:49 +05:30
|
|
|
import { Layout } from 'react-grid-layout';
|
|
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
|
import { AppState } from 'store/reducers';
|
|
|
|
|
import { Widgets } from 'types/api/dashboard/getAll';
|
|
|
|
|
import AppReducer from 'types/reducer/app';
|
2023-08-02 08:22:33 +03:00
|
|
|
import DashboardReducer from 'types/reducer/dashboards';
|
2022-06-07 16:14:49 +05:30
|
|
|
|
|
|
|
|
import { LayoutProps, State } from '.';
|
|
|
|
|
import {
|
|
|
|
|
Button,
|
|
|
|
|
ButtonContainer,
|
|
|
|
|
Card,
|
|
|
|
|
CardContainer,
|
|
|
|
|
ReactGridLayout,
|
|
|
|
|
} from './styles';
|
|
|
|
|
|
|
|
|
|
function GraphLayout({
|
|
|
|
|
layouts,
|
|
|
|
|
saveLayoutState,
|
|
|
|
|
onLayoutSaveHandler,
|
|
|
|
|
addPanelLoading,
|
|
|
|
|
onAddPanelHandler,
|
|
|
|
|
onLayoutChangeHandler,
|
|
|
|
|
widgets,
|
|
|
|
|
setLayout,
|
|
|
|
|
}: GraphLayoutProps): JSX.Element {
|
2023-08-02 08:22:33 +03:00
|
|
|
const { isAddWidget } = useSelector<AppState, DashboardReducer>(
|
|
|
|
|
(state) => state.dashboards,
|
|
|
|
|
);
|
2022-06-07 16:14:49 +05:30
|
|
|
const { role } = useSelector<AppState, AppReducer>((state) => state.app);
|
2023-01-11 14:39:06 +05:30
|
|
|
const isDarkMode = useIsDarkMode();
|
2022-06-07 16:14:49 +05:30
|
|
|
|
2022-06-08 22:57:34 +05:30
|
|
|
const [saveLayoutPermission, addPanelPermission] = useComponentPermission(
|
|
|
|
|
['save_layout', 'add_panel'],
|
|
|
|
|
role,
|
|
|
|
|
);
|
2022-06-07 16:14:49 +05:30
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<ButtonContainer>
|
2022-06-08 22:57:34 +05:30
|
|
|
{saveLayoutPermission && (
|
2022-06-07 16:14:49 +05:30
|
|
|
<Button
|
|
|
|
|
loading={saveLayoutState.loading}
|
|
|
|
|
onClick={(): Promise<void> => onLayoutSaveHandler(layouts)}
|
|
|
|
|
icon={<SaveFilled />}
|
|
|
|
|
danger={saveLayoutState.error}
|
|
|
|
|
>
|
|
|
|
|
Save Layout
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
|
2022-06-08 22:57:34 +05:30
|
|
|
{addPanelPermission && (
|
|
|
|
|
<Button
|
|
|
|
|
loading={addPanelLoading}
|
2023-08-02 08:22:33 +03:00
|
|
|
disabled={addPanelLoading || isAddWidget}
|
2022-06-08 22:57:34 +05:30
|
|
|
onClick={onAddPanelHandler}
|
|
|
|
|
icon={<PlusOutlined />}
|
|
|
|
|
>
|
|
|
|
|
Add Panel
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
2022-06-07 16:14:49 +05:30
|
|
|
</ButtonContainer>
|
|
|
|
|
|
|
|
|
|
<ReactGridLayout
|
|
|
|
|
cols={12}
|
|
|
|
|
rowHeight={100}
|
|
|
|
|
autoSize
|
|
|
|
|
width={100}
|
2022-06-08 22:57:34 +05:30
|
|
|
isDraggable={addPanelPermission}
|
|
|
|
|
isDroppable={addPanelPermission}
|
|
|
|
|
isResizable={addPanelPermission}
|
2022-06-07 16:14:49 +05:30
|
|
|
useCSSTransforms
|
|
|
|
|
allowOverlap={false}
|
|
|
|
|
onLayoutChange={onLayoutChangeHandler}
|
2023-01-13 17:29:51 +05:30
|
|
|
draggableHandle=".drag-handle"
|
2022-06-07 16:14:49 +05:30
|
|
|
>
|
2023-05-20 10:42:40 +05:30
|
|
|
{layouts.map(({ Component, ...rest }) => {
|
2022-06-07 16:14:49 +05:30
|
|
|
const currentWidget = (widgets || [])?.find((e) => e.id === rest.i);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<CardContainer
|
|
|
|
|
isDarkMode={isDarkMode}
|
|
|
|
|
key={currentWidget?.id || 'empty'} // don't change this key
|
|
|
|
|
data-grid={rest}
|
|
|
|
|
>
|
2023-07-25 16:25:36 +03:00
|
|
|
<Card $panelType={currentWidget?.panelTypes || PANEL_TYPES.TIME_SERIES}>
|
2022-06-07 16:14:49 +05:30
|
|
|
<Component setLayout={setLayout} />
|
|
|
|
|
</Card>
|
|
|
|
|
</CardContainer>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</ReactGridLayout>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface GraphLayoutProps {
|
|
|
|
|
layouts: LayoutProps[];
|
|
|
|
|
saveLayoutState: State;
|
|
|
|
|
onLayoutSaveHandler: (layout: Layout[]) => Promise<void>;
|
|
|
|
|
addPanelLoading: boolean;
|
|
|
|
|
onAddPanelHandler: VoidFunction;
|
|
|
|
|
onLayoutChangeHandler: (layout: Layout[]) => Promise<void>;
|
|
|
|
|
widgets: Widgets[] | undefined;
|
2023-05-19 13:14:32 +05:30
|
|
|
setLayout: Dispatch<SetStateAction<LayoutProps[]>>;
|
2022-06-07 16:14:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default GraphLayout;
|