From bf9d87dc16336cfe83f1c0855f46239ff026557d Mon Sep 17 00:00:00 2001 From: Brenda Sukh Date: Tue, 24 Nov 2020 16:56:20 -0500 Subject: [PATCH] Move cost overview chart legend to separate component --- .../CostOverviewCard/CostOverviewCard.tsx | 75 +++-------- .../CostOverviewCard/CostOverviewChart.tsx | 120 ++++++++++-------- .../CostOverviewCard/CostOverviewLegend.tsx | 84 ++++++++++++ 3 files changed, 163 insertions(+), 116 deletions(-) create mode 100644 plugins/cost-insights/src/components/CostOverviewCard/CostOverviewLegend.tsx diff --git a/plugins/cost-insights/src/components/CostOverviewCard/CostOverviewCard.tsx b/plugins/cost-insights/src/components/CostOverviewCard/CostOverviewCard.tsx index 742b89af7a..c385d5d424 100644 --- a/plugins/cost-insights/src/components/CostOverviewCard/CostOverviewCard.tsx +++ b/plugins/cost-insights/src/components/CostOverviewCard/CostOverviewCard.tsx @@ -24,24 +24,15 @@ import { Tab, Tabs, } from '@material-ui/core'; -import { CostGrowth } from '../CostGrowth'; import { CostOverviewChart } from './CostOverviewChart'; -import { CostOverviewByProduct } from './CostOverviewByProduct'; +import { CostOverviewByProductChart } from './CostOverviewByProductChart'; import { CostOverviewHeader } from './CostOverviewHeader'; -import { LegendItem } from '../LegendItem'; import { MetricSelect } from '../MetricSelect'; import { PeriodSelect } from '../PeriodSelect'; -import { - useScroll, - useFilters, - useConfig, - useLastCompleteBillingDate, -} from '../../hooks'; +import { useScroll, useFilters, useConfig } from '../../hooks'; import { mapFiltersToProps } from './selector'; import { DefaultNavigation } from '../../utils/navigation'; -import { formatPercent } from '../../utils/formatters'; import { findAlways } from '../../utils/assert'; -import { getComparedChange } from '../../utils/change'; import { Cost, CostInsightsTheme, MetricData } from '../../types'; import { useOverviewTabsStyles } from '../../utils/styles'; @@ -56,7 +47,6 @@ export const CostOverviewCard = ({ }: CostOverviewCardProps) => { const theme = useTheme(); const config = useConfig(); - const lastCompleteBillingDate = useLastCompleteBillingDate(); const [tabIndex, setTabIndex] = useState(0); const { ScrollAnchor } = useScroll(DefaultNavigation.CostOverviewCard); @@ -67,19 +57,11 @@ export const CostOverviewCard = ({ const metric = filters.metric ? findAlways(config.metrics, m => m.kind === filters.metric) : null; - const comparedChange = metricData - ? getComparedChange( - dailyCostData, - metricData, - filters.duration, - lastCompleteBillingDate, - ) - : null; const styles = useOverviewTabsStyles(theme); const tabs = [ - { id: 'overview', label: 'OVERVIEW' }, - { id: 'breakdown', label: 'BREAKDOWN BY PRODUCT' }, + { id: 'overview', label: 'Total cost' }, + { id: 'breakdown', label: 'Breakdown by product' }, ]; const OverviewTabs = () => { @@ -104,34 +86,8 @@ export const CostOverviewCard = ({ ); }; - const OverviewLegend = () => { - return ( - - - - {formatPercent(dailyCostData.change!.ratio)} - - - {metric && metricData && comparedChange && ( - <> - - - {formatPercent(metricData.change.ratio)} - - - - - - - )} - - ); - }; + // Metrics can only be selected on the total cost graph + const showMetricSelect = config.metrics.length && tabIndex === 0; return ( @@ -146,20 +102,19 @@ export const CostOverviewCard = ({ {tabIndex === 0 ? ( - <> - - - + ) : ( - + )} - {config.metrics.length && tabIndex === 0 && ( + {showMetricSelect && ( - - - - 0, 'dataMax']} - tick={{ fill: styles.axis.fill }} - tickFormatter={formatGraphValue} - width={styles.yAxis.width} - yAxisId={data.dailyCost.dataKey} - /> - {metric && ( - 0, toDataMax(data.metric.dataKey, chartData)]} - width={styles.yAxis.width} - yAxisId={data.metric.dataKey} + + + + + + + 0, 'dataMax']} + tick={{ fill: styles.axis.fill }} + tickFormatter={formatGraphValue} + width={styles.yAxis.width} + yAxisId={data.dailyCost.dataKey} + /> + {metric && ( + 0, toDataMax(data.metric.dataKey, chartData)]} + width={styles.yAxis.width} + yAxisId={data.metric.dataKey} + /> + )} + - )} - - - {metric && ( - )} - - - + {metric && ( + + )} + + + + ); }; diff --git a/plugins/cost-insights/src/components/CostOverviewCard/CostOverviewLegend.tsx b/plugins/cost-insights/src/components/CostOverviewCard/CostOverviewLegend.tsx new file mode 100644 index 0000000000..ebba5173a9 --- /dev/null +++ b/plugins/cost-insights/src/components/CostOverviewCard/CostOverviewLegend.tsx @@ -0,0 +1,84 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React, { PropsWithChildren } from 'react'; +import { Box, useTheme } from '@material-ui/core'; +import { LegendItem } from '../LegendItem'; +import { + CostInsightsTheme, + MetricData, + Maybe, + Cost, + Metric, +} from '../../types'; +import { useLastCompleteBillingDate, useFilters } from '../../hooks'; +import { getComparedChange } from '../../utils/change'; +import { mapFiltersToProps } from './selector'; +import { formatPercent } from '../../utils/formatters'; +import { CostGrowth } from '../CostGrowth'; + +type CostOverviewLegendProps = { + metric: Maybe; + metricData: Maybe; + dailyCostData: Cost; +}; + +export const CostOverviewLegend = ({ + dailyCostData, + metric, + metricData, +}: PropsWithChildren) => { + const theme = useTheme(); + + const lastCompleteBillingDate = useLastCompleteBillingDate(); + const { duration } = useFilters(mapFiltersToProps); + + const comparedChange = metricData + ? getComparedChange( + dailyCostData, + metricData, + duration, + lastCompleteBillingDate, + ) + : null; + + return ( + + + + {formatPercent(dailyCostData.change!.ratio)} + + + {metric && metricData && comparedChange && ( + <> + + + {formatPercent(metricData.change.ratio)} + + + + + + + )} + + ); +};