Skip to content

Commit

Permalink
refactor: BudgetInsightInfo - return template + data
Browse files Browse the repository at this point in the history
  • Loading branch information
vio committed Mar 12, 2022
1 parent de40ce9 commit e323da3
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 38 deletions.
15 changes: 14 additions & 1 deletion packages/ui/src/components/budget-info/budget-info.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,22 @@ export const BudgetInfo = ({ className = '', metricId, budgetInsight }) => {
const { className: insightTypeClassName, glyph } = INSIGHT_TYPE_MAP[budgetInsightInfo.type];

const rootClassName = cx(css.root, insightTypeClassName, className);
const { data: messageData } = budgetInsightInfo.message;

return (
<Tooltip className={rootClassName} title={budgetInsightInfo.data.text}>
<Tooltip
className={rootClassName}
title={
<>
<strong>{messageData.metricLabel}</strong>
{` value (`}
<strong>{messageData.currentValue}</strong>
{`) is ${messageData.diffLabel} `}
<strong>{messageData.budgetValue}</strong>
{` budget `}
</>
}
>
<Icon className={css.icon} glyph={glyph} size="medium" />
</Tooltip>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ const Budget = (props) => {
const { className, metricId, budgetInsight, CustomLink } = props;
const componentLinkOptions = METRIC_COMPONENT_LINKS.get(metricId);
const budgetInsightInfo = budgetsInsightsTransformer.getInfo(metricId, budgetInsight);
const { data: messageData } = budgetInsightInfo.message;

return (
<CustomLink className={cx(css.budget, className)} {...componentLinkOptions?.link}>
{budgetInsightInfo.data.text}
<strong>{messageData.metricLabel}</strong>
{` value (`}
<strong>{messageData.currentValue}</strong>
{`) is ${messageData.diffLabel} `}
<strong>{messageData.budgetValue}</strong>
{` budget `}
</CustomLink>
);
};
Expand Down
62 changes: 43 additions & 19 deletions packages/utils/src/transformers/__tests__/budgets-insights.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getInfo, getExtract } from '../budgets-insights';
import { INFO_MESSAGE_TEMPLATE, getInfo, getExtract } from '../budgets-insights';

describe('transformers/budgetsInsights', () => {
describe('extract', () => {
Expand Down Expand Up @@ -91,14 +91,22 @@ describe('transformers/budgetsInsights', () => {
}),
).toEqual({
type: 'ERROR',
data: {
md: '**Bundle Size** is over **256KB** budget',
text: 'Bundle Size is over 256KB budget',
message: {
template: INFO_MESSAGE_TEMPLATE,
data: {
metricLabel: 'Bundle Size',
diffLabel: 'over',
currentValue: '512KB',
budgetValue: '256KB',
},
},
source: {
currentValue: 512 * 1024,
budgetValue: 256 * 1024,
failed: true,
metricId: 'webpack.totalSizeByTypeALL',
budgetInsight: {
currentValue: 512 * 1024,
budgetValue: 256 * 1024,
failed: true,
},
},
});
});
Expand All @@ -112,14 +120,22 @@ describe('transformers/budgetsInsights', () => {
}),
).toEqual({
type: 'SUCCESS',
data: {
md: '**Bundle Size** is under **1MB** budget',
text: 'Bundle Size is under 1MB budget',
message: {
template: INFO_MESSAGE_TEMPLATE,
data: {
metricLabel: 'Bundle Size',
diffLabel: 'under',
currentValue: '512KB',
budgetValue: '1MB',
},
},
source: {
currentValue: 512 * 1024,
budgetValue: 1024 * 1024,
failed: false,
metricId: 'webpack.totalSizeByTypeALL',
budgetInsight: {
currentValue: 512 * 1024,
budgetValue: 1024 * 1024,
failed: false,
},
},
});
});
Expand All @@ -133,14 +149,22 @@ describe('transformers/budgetsInsights', () => {
}),
).toEqual({
type: 'WARNING',
data: {
md: '**Bundle Size** is equal with **512KB** budget',
text: 'Bundle Size is equal with 512KB budget',
message: {
template: INFO_MESSAGE_TEMPLATE,
data: {
metricLabel: 'Bundle Size',
diffLabel: 'equal with',
currentValue: '512KB',
budgetValue: '512KB',
},
},
source: {
currentValue: 512 * 1024,
budgetValue: 512 * 1024,
failed: false,
metricId: 'webpack.totalSizeByTypeALL',
budgetInsight: {
currentValue: 512 * 1024,
budgetValue: 512 * 1024,
failed: false,
},
},
});
});
Expand Down
43 changes: 26 additions & 17 deletions packages/utils/src/transformers/budgets-insights.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import get from 'lodash/get';
import template from 'lodash/template';

import { InsightType } from '../constants';
import { getGlobalMetricType } from '../utils';
Expand All @@ -21,11 +20,14 @@ interface BudgetInsight {

interface BudgetInsightInfo {
type: InsightType;
data: {
text: string;
md: string;
message: {
template: string;
data: Record<string, unknown>;
};
source: {
metricId: string;
budgetInsight: BudgetInsight;
};
source: BudgetInsight;
}

interface BudgetInsights {
Expand Down Expand Up @@ -84,8 +86,6 @@ export const getExtract =
};
};

const infoTemplate = template('<%= metric %> is <%= diff %> <%= value %> budget');

const resolveDiffLabel = (budgetInsight: BudgetInsight): string => {
const { currentValue, budgetValue } = budgetInsight;

Expand All @@ -112,23 +112,32 @@ const resolveType = (budgetInsight: BudgetInsight): InsightType => {
return InsightType.SUCCESS;
};

// Add escapes for (|) to avoid lodash.template syntax errors
// eslint-disable-next-line no-useless-escape
export const INFO_MESSAGE_TEMPLATE = '<%= metricLabel %> value \(<%= currentValue %>\) is <%= diffLabel %> <%= budgetValue %> budget';

export const getInfo = (globalMetricId: string, budgetInsight: BudgetInsight): BudgetInsightInfo => {
const metric = getGlobalMetricType(globalMetricId);

const diff = resolveDiffLabel(budgetInsight);
const diffLabel = resolveDiffLabel(budgetInsight);
const type = resolveType(budgetInsight);
const value = metric.formatter(budgetInsight.budgetValue);
const currentValue = metric.formatter(budgetInsight.currentValue);
const budgetValue = metric.formatter(budgetInsight.budgetValue);

return {
type,
data: {
text: infoTemplate({ metric: metric.label, diff, value }),
md: infoTemplate({
metric: `**${metric.label}**`,
diff,
value: `**${value}**`,
}),
message: {
template: INFO_MESSAGE_TEMPLATE,
data: {
metricLabel: metric.label,
diffLabel,
currentValue,
budgetValue,
},
},
source: {
metricId: globalMetricId,
budgetInsight,
},
source: budgetInsight,
};
};

0 comments on commit e323da3

Please sign in to comment.