-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathchart-panel.tsx
More file actions
153 lines (143 loc) · 4.5 KB
/
chart-panel.tsx
File metadata and controls
153 lines (143 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { Box, BoxProps, Theme } from "@mui/material";
import { makeStyles } from "@mui/styles";
import clsx from "clsx";
import keyBy from "lodash/keyBy";
import React, { forwardRef, HTMLProps, useMemo } from "react";
import ChartPanelLayoutCanvas, {
chartPanelLayoutGridClasses,
} from "@/components/chart-panel-layout-grid";
import { ChartPanelLayoutTall } from "@/components/chart-panel-layout-tall";
import { ChartPanelLayoutVertical } from "@/components/chart-panel-layout-vertical";
import { ChartSelectionTabs } from "@/components/chart-selection-tabs";
import { DashboardInteractiveFilters } from "@/components/dashboard-interactive-filters";
import { ChartConfig, Layout, LayoutDashboard } from "@/config-types";
import { hasChartConfigs } from "@/configurator";
import { useDataCubesComponentsQuery } from "@/graphql/hooks";
import { useConfiguratorState, useLocale } from "@/src";
const useStyles = makeStyles((theme: Theme) => ({
panelLayout: {
display: "flex",
flexDirection: "column",
gap: theme.spacing(4),
},
chartWrapper: {
[`.${chartPanelLayoutGridClasses.root} &`]: {
transition: theme.transitions.create(["box-shadow"], {
duration: theme.transitions.duration.shortest,
}),
},
[`.${chartPanelLayoutGridClasses.root} &:has(.${chartPanelLayoutGridClasses.dragHandle}:hover)`]:
{
boxShadow: theme.shadows[6],
},
},
chartWrapperInner: {
display: "contents",
overflow: "hidden",
width: "auto",
height: "100%",
},
}));
export type ChartWrapperProps = BoxProps & {
editing?: boolean;
layoutType?: Layout["type"];
};
export const ChartWrapper = forwardRef<HTMLDivElement, ChartWrapperProps>(
(props, ref) => {
const { children, editing, layoutType, ...rest } = props;
const classes = useStyles();
const [state] = useConfiguratorState(hasChartConfigs);
const dataSource = state.dataSource;
const locale = useLocale();
const commonQueryVariables = {
sourceType: dataSource.type,
sourceUrl: dataSource.url,
locale,
};
const componentIris = undefined;
const chartConfig = state.chartConfigs.find(
(x) => x.key === state.activeChartKey
);
const [{ data: components }] = useDataCubesComponentsQuery({
variables: {
...commonQueryVariables,
cubeFilters:
chartConfig?.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
filters: cube.filters,
joinBy: cube.joinBy,
})) ?? [],
},
});
const dimensionsByIri = useMemo(() => {
return keyBy(
[
...(components?.dataCubesComponents.dimensions ?? []),
...(components?.dataCubesComponents.measures ?? []),
],
(x) => x.iri
);
}, [components?.dataCubesComponents]);
return (
<Box
ref={ref}
{...rest}
className={clsx(classes.chartWrapper, props.className)}
>
{(editing || layoutType === "tab") && (
<ChartSelectionTabs dimensionsByIri={dimensionsByIri} />
)}
<Box
className={classes.chartWrapperInner}
sx={{ minHeight: [150, 300, 500] }}
>
{children}
</Box>
</Box>
);
}
);
type ChartPanelLayoutProps = React.PropsWithChildren<{
layoutType: LayoutDashboard["layout"];
chartConfigs: ChartConfig[];
renderChart: (chartConfig: ChartConfig) => JSX.Element;
}> &
HTMLProps<HTMLDivElement>;
export type ChartPanelLayoutTypeProps = {
chartConfigs: ChartConfig[];
renderChart: (chartConfig: ChartConfig) => JSX.Element;
className?: string;
};
const Wrappers: Record<
LayoutDashboard["layout"],
(props: ChartPanelLayoutTypeProps) => JSX.Element
> = {
vertical: ChartPanelLayoutVertical,
tall: ChartPanelLayoutTall,
canvas: ChartPanelLayoutCanvas,
};
export const ChartPanelLayout = (props: ChartPanelLayoutProps) => {
const {
children,
renderChart,
chartConfigs,
className,
layoutType,
...rest
} = props;
const classes = useStyles();
const Wrapper = Wrappers[layoutType];
const [state] = useConfiguratorState(hasChartConfigs);
return (
<div className={clsx(classes.panelLayout, className)} {...rest}>
{/** We want to completely remount this component if chartConfigs change */}
{state.layout.type === "dashboard" ? (
<DashboardInteractiveFilters
key={chartConfigs.map((x) => x.key).join(",")}
/>
) : null}
<Wrapper chartConfigs={chartConfigs} renderChart={renderChart} />
</div>
);
};