Skip to content

Commit 7049e47

Browse files
committed
refactor: BudgetInsightInfo - return template + data
1 parent ee45926 commit 7049e47

File tree

4 files changed

+90
-38
lines changed

4 files changed

+90
-38
lines changed

packages/ui/src/components/budget-info/budget-info.jsx

+14-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,22 @@ export const BudgetInfo = ({ className = '', metricId, budgetInsight }) => {
2929
const { className: insightTypeClassName, glyph } = INSIGHT_TYPE_MAP[budgetInsightInfo.type];
3030

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

3334
return (
34-
<Tooltip className={rootClassName} title={budgetInsightInfo.data.text}>
35+
<Tooltip
36+
className={rootClassName}
37+
title={
38+
<>
39+
<strong>{messageData.metricLabel}</strong>
40+
{` value (`}
41+
<strong>{messageData.currentValue}</strong>
42+
{`) is ${messageData.diffLabel} `}
43+
<strong>{messageData.budgetValue}</strong>
44+
{` budget `}
45+
</>
46+
}
47+
>
3548
<Icon className={css.icon} glyph={glyph} size="medium" />
3649
</Tooltip>
3750
);

packages/ui/src/components/budget-insights/budget-insights.jsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ const Budget = (props) => {
1616
const { className, metricId, budgetInsight, CustomLink } = props;
1717
const componentLinkOptions = METRIC_COMPONENT_LINKS.get(metricId);
1818
const budgetInsightInfo = budgetsInsightsTransformer.getInfo(metricId, budgetInsight);
19+
const { data: messageData } = budgetInsightInfo.message;
1920

2021
return (
2122
<CustomLink className={cx(css.budget, className)} {...componentLinkOptions?.link}>
22-
{budgetInsightInfo.data.text}
23+
<strong>{messageData.metricLabel}</strong>
24+
{` value (`}
25+
<strong>{messageData.currentValue}</strong>
26+
{`) is ${messageData.diffLabel} `}
27+
<strong>{messageData.budgetValue}</strong>
28+
{` budget `}
2329
</CustomLink>
2430
);
2531
};

packages/utils/src/transformers/__tests__/budgets-insights.ts

+43-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getInfo, getExtract } from '../budgets-insights';
1+
import { INFO_MESSAGE_TEMPLATE, getInfo, getExtract } from '../budgets-insights';
22

33
describe('transformers/budgetsInsights', () => {
44
describe('extract', () => {
@@ -91,14 +91,22 @@ describe('transformers/budgetsInsights', () => {
9191
}),
9292
).toEqual({
9393
type: 'ERROR',
94-
data: {
95-
md: '**Bundle Size** is over **256KB** budget',
96-
text: 'Bundle Size is over 256KB budget',
94+
message: {
95+
template: INFO_MESSAGE_TEMPLATE,
96+
data: {
97+
metricLabel: 'Bundle Size',
98+
diffLabel: 'over',
99+
currentValue: '512KB',
100+
budgetValue: '256KB',
101+
},
97102
},
98103
source: {
99-
currentValue: 512 * 1024,
100-
budgetValue: 256 * 1024,
101-
failed: true,
104+
metricId: 'webpack.totalSizeByTypeALL',
105+
budgetInsight: {
106+
currentValue: 512 * 1024,
107+
budgetValue: 256 * 1024,
108+
failed: true,
109+
},
102110
},
103111
});
104112
});
@@ -112,14 +120,22 @@ describe('transformers/budgetsInsights', () => {
112120
}),
113121
).toEqual({
114122
type: 'SUCCESS',
115-
data: {
116-
md: '**Bundle Size** is under **1MB** budget',
117-
text: 'Bundle Size is under 1MB budget',
123+
message: {
124+
template: INFO_MESSAGE_TEMPLATE,
125+
data: {
126+
metricLabel: 'Bundle Size',
127+
diffLabel: 'under',
128+
currentValue: '512KB',
129+
budgetValue: '1MB',
130+
},
118131
},
119132
source: {
120-
currentValue: 512 * 1024,
121-
budgetValue: 1024 * 1024,
122-
failed: false,
133+
metricId: 'webpack.totalSizeByTypeALL',
134+
budgetInsight: {
135+
currentValue: 512 * 1024,
136+
budgetValue: 1024 * 1024,
137+
failed: false,
138+
},
123139
},
124140
});
125141
});
@@ -133,14 +149,22 @@ describe('transformers/budgetsInsights', () => {
133149
}),
134150
).toEqual({
135151
type: 'WARNING',
136-
data: {
137-
md: '**Bundle Size** is equal with **512KB** budget',
138-
text: 'Bundle Size is equal with 512KB budget',
152+
message: {
153+
template: INFO_MESSAGE_TEMPLATE,
154+
data: {
155+
metricLabel: 'Bundle Size',
156+
diffLabel: 'equal with',
157+
currentValue: '512KB',
158+
budgetValue: '512KB',
159+
},
139160
},
140161
source: {
141-
currentValue: 512 * 1024,
142-
budgetValue: 512 * 1024,
143-
failed: false,
162+
metricId: 'webpack.totalSizeByTypeALL',
163+
budgetInsight: {
164+
currentValue: 512 * 1024,
165+
budgetValue: 512 * 1024,
166+
failed: false,
167+
},
144168
},
145169
});
146170
});

packages/utils/src/transformers/budgets-insights.ts

+26-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import get from 'lodash/get';
2-
import template from 'lodash/template';
32

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

2221
interface BudgetInsightInfo {
2322
type: InsightType;
24-
data: {
25-
text: string;
26-
md: string;
23+
message: {
24+
template: string;
25+
data: Record<string, unknown>;
26+
};
27+
source: {
28+
metricId: string;
29+
budgetInsight: BudgetInsight;
2730
};
28-
source: BudgetInsight;
2931
}
3032

3133
interface BudgetInsights {
@@ -84,8 +86,6 @@ export const getExtract =
8486
};
8587
};
8688

87-
const infoTemplate = template('<%= metric %> is <%= diff %> <%= value %> budget');
88-
8989
const resolveDiffLabel = (budgetInsight: BudgetInsight): string => {
9090
const { currentValue, budgetValue } = budgetInsight;
9191

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

115+
// Add escapes for (|) to avoid lodash.template syntax errors
116+
// eslint-disable-next-line no-useless-escape
117+
export const INFO_MESSAGE_TEMPLATE = '<%= metricLabel %> value \(<%= currentValue %>\) is <%= diffLabel %> <%= budgetValue %> budget';
118+
115119
export const getInfo = (globalMetricId: string, budgetInsight: BudgetInsight): BudgetInsightInfo => {
116120
const metric = getGlobalMetricType(globalMetricId);
117121

118-
const diff = resolveDiffLabel(budgetInsight);
122+
const diffLabel = resolveDiffLabel(budgetInsight);
119123
const type = resolveType(budgetInsight);
120-
const value = metric.formatter(budgetInsight.budgetValue);
124+
const currentValue = metric.formatter(budgetInsight.currentValue);
125+
const budgetValue = metric.formatter(budgetInsight.budgetValue);
121126

122127
return {
123128
type,
124-
data: {
125-
text: infoTemplate({ metric: metric.label, diff, value }),
126-
md: infoTemplate({
127-
metric: `**${metric.label}**`,
128-
diff,
129-
value: `**${value}**`,
130-
}),
129+
message: {
130+
template: INFO_MESSAGE_TEMPLATE,
131+
data: {
132+
metricLabel: metric.label,
133+
diffLabel,
134+
currentValue,
135+
budgetValue,
136+
},
137+
},
138+
source: {
139+
metricId: globalMetricId,
140+
budgetInsight,
131141
},
132-
source: budgetInsight,
133142
};
134143
};

0 commit comments

Comments
 (0)