-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathchart-published.tsx
More file actions
407 lines (389 loc) · 13 KB
/
chart-published.tsx
File metadata and controls
407 lines (389 loc) · 13 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import { Trans } from "@lingui/macro";
import { Box, Theme } from "@mui/material";
import { makeStyles } from "@mui/styles";
import clsx from "clsx";
import { forwardRef, useCallback, useEffect, useMemo, useRef } from "react";
import { useStore } from "zustand";
import { DataSetTable } from "@/browse/datatable";
import { extractChartConfigsComponentIris } from "@/charts/shared/chart-helpers";
import { LoadingStateProvider } from "@/charts/shared/chart-loading-state";
import { isUsingImputation } from "@/charts/shared/imputation";
import { ChartErrorBoundary } from "@/components/chart-error-boundary";
import { ChartFootnotes } from "@/components/chart-footnotes";
import { ChartPanelLayout, ChartWrapper } from "@/components/chart-panel";
import { ChartControls } from "@/components/chart-shared";
import {
ChartTablePreviewProvider,
useChartTablePreview,
} from "@/components/chart-table-preview";
import { useChartStyles } from "@/components/chart-utils";
import { ChartWithFilters } from "@/components/chart-with-filters";
import { DashboardInteractiveFilters } from "@/components/dashboard-interactive-filters";
import Flex from "@/components/flex";
import { HintBlue, HintRed, HintYellow } from "@/components/hint";
import {
MetadataPanelStoreContext,
createMetadataPanelStore,
} from "@/components/metadata-panel-store";
import {
ChartConfig,
ConfiguratorStatePublished,
DataSource,
getChartConfig,
isPublished,
useConfiguratorState,
} from "@/configurator";
import { Description, Title } from "@/configurator/components/annotators";
import { DRAWER_WIDTH } from "@/configurator/components/drawer";
import {
DEFAULT_DATA_SOURCE,
useIsTrustedDataSource,
} from "@/domain/datasource";
import {
useDataCubesComponentsQuery,
useDataCubesMetadataQuery,
} from "@/graphql/hooks";
import { DataCubePublicationStatus } from "@/graphql/resolver-types";
import { useLocale } from "@/locales/use-locale";
import {
InteractiveFiltersChartProvider,
InteractiveFiltersProvider,
} from "@/stores/interactive-filters";
import { useEmbedOptions } from "@/utils/embed";
import useEvent from "@/utils/use-event";
type ChartPublishedProps = {
configKey?: string;
};
type ChartPublishedIndividualChartProps = ChartPublishInnerProps;
const ChartPublishedIndividualChart = forwardRef<
HTMLDivElement,
ChartPublishedIndividualChartProps
>(({ dataSource, state, chartConfig, configKey, children, ...rest }, ref) => {
return (
<ChartTablePreviewProvider key={chartConfig.key}>
<ChartWrapper
key={chartConfig.key}
layoutType={state.layout.type}
ref={ref}
{...rest}
>
<ChartPublishedInner
key={chartConfig.key}
dataSource={dataSource}
state={state}
chartConfig={chartConfig}
configKey={configKey}
>
{children}
</ChartPublishedInner>
</ChartWrapper>
</ChartTablePreviewProvider>
);
});
export const ChartPublished = (props: ChartPublishedProps) => {
const { configKey } = props;
const [state] = useConfiguratorState(isPublished);
const { dataSource } = state;
const locale = useLocale();
const renderChart = useCallback(
(chartConfig: ChartConfig) => (
<ChartPublishedIndividualChart
key={chartConfig.key}
dataSource={dataSource}
state={state}
chartConfig={chartConfig}
configKey={configKey}
/>
),
[configKey, dataSource, state]
);
return (
<InteractiveFiltersProvider chartConfigs={state.chartConfigs}>
{state.layout.type === "dashboard" ? (
<>
<Box
sx={{
mb:
state.layout.meta.title[locale] ||
state.layout.meta.description[locale]
? 4
: 0,
}}
>
{state.layout.meta.title[locale] && (
<Title text={state.layout.meta.title[locale]} />
)}
{state.layout.meta.description[locale] && (
<Description text={state.layout.meta.description[locale]} />
)}
</Box>
<ChartPanelLayout
layoutType={state.layout.layout}
chartConfigs={state.chartConfigs}
renderChart={renderChart}
/>
</>
) : (
<>
<Flex
sx={{
flexDirection: "column",
mb:
state.layout.meta.title[locale] ||
state.layout.meta.description[locale]
? 4
: 0,
}}
>
{state.layout.meta.title[locale] && (
<Title text={state.layout.meta.title[locale]} />
)}
{state.layout.meta.description[locale] && (
<Description text={state.layout.meta.description[locale]} />
)}
</Flex>
<ChartTablePreviewProvider>
<DashboardInteractiveFilters />
<ChartWrapper layoutType={state.layout.type}>
<ChartPublishedInner
dataSource={dataSource}
state={state}
chartConfig={getChartConfig(state)}
configKey={configKey}
/>
</ChartWrapper>
</ChartTablePreviewProvider>
</>
)}
</InteractiveFiltersProvider>
);
};
const usePublishedChartStyles = makeStyles<Theme, { shrink: boolean }>(
(theme) => ({
root: {
// Needed for the metadata panel to be contained inside the root.
position: "relative",
paddingLeft: ({ shrink }) =>
`calc(${theme.spacing(5)} + ${shrink ? DRAWER_WIDTH : 0}px)`,
transition: "padding 0.25s ease",
},
})
);
type ChartPublishInnerProps = {
dataSource: DataSource | undefined;
state: ConfiguratorStatePublished;
chartConfig: ChartConfig;
configKey: string | undefined;
className?: string;
children?: React.ReactNode;
};
const ChartPublishedInner = (props: ChartPublishInnerProps) => {
const {
dataSource = DEFAULT_DATA_SOURCE,
state,
chartConfig,
configKey,
className,
children,
} = props;
const { meta } = chartConfig;
const rootRef = useRef<HTMLDivElement>(null);
const {
state: isTablePreview,
setState: setIsTablePreview,
containerRef,
containerHeight,
computeContainerHeight,
} = useChartTablePreview();
const handleToggleTableView = useEvent(() => setIsTablePreview((c) => !c));
const [{ showDownload }] = useEmbedOptions();
const metadataPanelStore = useMemo(() => createMetadataPanelStore(), []);
const metadataPanelOpen = useStore(metadataPanelStore, (state) => state.open);
const shouldShrink = useMemo(() => {
const rootWidth = rootRef.current?.getBoundingClientRect().width;
if (!rootWidth) {
return false;
}
return metadataPanelOpen && rootWidth > DRAWER_WIDTH * 2;
}, [metadataPanelOpen]);
useEffect(() => {
const unsubscribe = metadataPanelStore.subscribe(() => {
computeContainerHeight();
});
return () => unsubscribe();
});
const chartClasses = useChartStyles();
const publishedChartClasses = usePublishedChartStyles({
shrink: shouldShrink,
});
const locale = useLocale();
const isTrustedDataSource = useIsTrustedDataSource(dataSource);
const commonQueryVariables = {
sourceType: dataSource.type,
sourceUrl: dataSource.url,
locale,
};
const [{ data: metadataData }] = useDataCubesMetadataQuery({
variables: {
sourceType: dataSource.type,
sourceUrl: dataSource.url,
locale,
cubeFilters: chartConfig.cubes.map((cube) => ({ iri: cube.iri })),
},
});
const metadata = metadataData?.dataCubesMetadata;
const componentIris = extractChartConfigsComponentIris(state.chartConfigs);
const [{ data: componentsData }] = useDataCubesComponentsQuery({
variables: {
...commonQueryVariables,
cubeFilters: chartConfig.cubes.map((cube) => ({
iri: cube.iri,
componentIris,
joinBy: cube.joinBy,
})),
},
});
const components = componentsData?.dataCubesComponents;
const dimensions = components?.dimensions;
const measures = components?.measures;
const allComponents = useMemo(() => {
if (!dimensions || !measures) {
return [];
}
return [...dimensions, ...measures];
}, [dimensions, measures]);
return (
<MetadataPanelStoreContext.Provider value={metadataPanelStore}>
<Box
className={clsx(
chartClasses.root,
publishedChartClasses.root,
className
)}
ref={rootRef}
>
{children}
<ChartErrorBoundary resetKeys={[chartConfig]}>
<div>
{metadata?.some(
(d) => d.publicationStatus === DataCubePublicationStatus.Draft
) && (
<Box sx={{ mb: 4 }}>
<HintRed iconName="datasetError" iconSize={64}>
<Trans id="dataset.publicationStatus.draft.warning">
Careful, this dataset is only a draft.
<br />
<strong>Don't use for reporting!</strong>
</Trans>
</HintRed>
</Box>
)}
{metadata?.some((d) => d.expires) && (
<Box sx={{ mb: 4 }}>
<HintRed iconName="datasetError" iconSize={64}>
<Trans id="dataset.publicationStatus.expires.warning">
Careful, the data for this chart has expired.
<br />
<strong>Don't use for reporting!</strong>
</Trans>
</HintRed>
</Box>
)}
{!isTrustedDataSource && (
<Box sx={{ mb: 4 }}>
<HintYellow iconName="hintWarning">
<Trans id="data.source.notTrusted">
This chart is not using a trusted data source.
</Trans>
</HintYellow>
</Box>
)}
{isUsingImputation(chartConfig) && (
<Box sx={{ mb: 4 }}>
<HintBlue iconName="hintWarning">
<Trans id="dataset.hasImputedValues">
Some data in this dataset is missing and has been
interpolated to fill the gaps.
</Trans>
</HintBlue>
</Box>
)}
</div>
<LoadingStateProvider>
<InteractiveFiltersChartProvider chartConfigKey={chartConfig.key}>
<Flex
sx={{
justifyContent: meta.title[locale]
? "space-between"
: "flex-end",
gap: 2,
}}
>
{meta.title[locale] ? (
<Title text={meta.title[locale]} />
) : (
// We need to have a span here to keep the space between the
// title and the chart (subgrid layout)
<span />
)}
</Flex>
{meta.description[locale] ? (
<Description text={meta.description[locale]} />
) : (
// We need to have a span here to keep the space between the
// title and the chart (subgrid layout)
<span />
)}
<ChartControls
dataSource={dataSource}
chartConfig={chartConfig}
metadataPanelProps={{
dimensions: allComponents,
container: rootRef.current,
}}
/>
<div
ref={containerRef}
style={{
// TODO before merging, Align with chart-preview
minWidth: 0,
height: containerHeight.current,
marginTop: 16,
flexGrow: 1,
}}
>
{isTablePreview ? (
<DataSetTable
sx={{ maxHeight: "100%" }}
dataSource={dataSource}
chartConfig={chartConfig}
/>
) : (
<ChartWithFilters
dataSource={dataSource}
componentIris={componentIris}
chartConfig={chartConfig}
/>
)}
</div>
<ChartFootnotes
dataSource={dataSource}
chartConfig={chartConfig}
dimensions={dimensions}
configKey={configKey}
onToggleTableView={handleToggleTableView}
visualizeLinkText={
showDownload === false ? (
<Trans id="metadata.link.created.with.visualize.alternate">
visualize.admin.ch
</Trans>
) : undefined
}
/>
</InteractiveFiltersChartProvider>
</LoadingStateProvider>
</ChartErrorBoundary>
</Box>
</MetadataPanelStoreContext.Provider>
);
};