|
| 1 | +import React, { useMemo } from 'react'; |
| 2 | +import { |
| 3 | + BudgetEvaluated, |
| 4 | + BudgetResult, |
| 5 | + BudgetStatus, |
| 6 | + ConditionOperator, |
| 7 | + MetricRunInfoDeltaType, |
| 8 | + getGlobalMetricType, |
| 9 | +} from '@bundle-stats/utils'; |
| 10 | + |
| 11 | +import { Stack } from '../../layout/stack'; |
| 12 | +import { Alert } from '../../ui/alert'; |
| 13 | +import { Delta } from '../delta'; |
| 14 | +import { Metric } from '../metric'; |
| 15 | +// @ts-ignore |
| 16 | +import css from './budgets.module.css'; |
| 17 | + |
| 18 | +const OPERATOR_MAP: Record<ConditionOperator, string> = { |
| 19 | + smallerThanInclusive: 'equal', |
| 20 | + smallerThan: 'bellow', |
| 21 | + equal: 'equal', |
| 22 | + notEqual: 'not equal', |
| 23 | + greaterThan: 'above', |
| 24 | + greaterThanInclusive: 'equal', |
| 25 | +}; |
| 26 | + |
| 27 | +const ALERT_MAP: Record<string, string> = { |
| 28 | + FAILURE: 'danger', |
| 29 | + WARNING: 'warning', |
| 30 | + SUCCESS: 'success', |
| 31 | +}; |
| 32 | + |
| 33 | +interface BudgetProps extends React.HTMLAttributes<HTMLElement> { |
| 34 | + budget: BudgetEvaluated; |
| 35 | +} |
| 36 | + |
| 37 | +const Budget = (props: BudgetProps) => { |
| 38 | + const { budget, ...restProps } = props; |
| 39 | + |
| 40 | + const metricSegments = budget.config.condition.fact.split('.'); |
| 41 | + const metricKey = metricSegments.slice(0, -1).join('.'); |
| 42 | + |
| 43 | + const field = metricSegments[metricSegments.length - 1]; |
| 44 | + |
| 45 | + let displayField = ''; |
| 46 | + let deltaDisplayField: string; |
| 47 | + |
| 48 | + switch (field) { |
| 49 | + case 'deltaPercentage': |
| 50 | + displayField = 'relative difference'; |
| 51 | + deltaDisplayField = budget.data.displayDeltaPercentage as string; |
| 52 | + break; |
| 53 | + case 'delta': |
| 54 | + displayField = 'absolute difference'; |
| 55 | + deltaDisplayField = budget.data.displayDelta as string; |
| 56 | + break; |
| 57 | + default: |
| 58 | + displayField = 'value'; |
| 59 | + deltaDisplayField = budget.data.displayDeltaPercentage as string; |
| 60 | + } |
| 61 | + |
| 62 | + const metric = getGlobalMetricType(metricKey); |
| 63 | + |
| 64 | + return ( |
| 65 | + <Alert kind={ALERT_MAP[budget.config.status]} {...restProps}> |
| 66 | + <p> |
| 67 | + <strong>{metric.label}</strong> |
| 68 | + {` ${displayField} `} |
| 69 | + <Metric inline value={budget.data.value} formatter={metric.formatter} className={css.checkMetric}> |
| 70 | + <Delta |
| 71 | + displayValue={deltaDisplayField} |
| 72 | + deltaType={budget.data.deltaType as MetricRunInfoDeltaType} |
| 73 | + inverted |
| 74 | + className={css.checkMetricDelta} |
| 75 | + /> |
| 76 | + </Metric> |
| 77 | + {` is ${OPERATOR_MAP[budget.config.condition.operator]} `} |
| 78 | + <Metric |
| 79 | + value={budget.config.condition.value} |
| 80 | + formatter={metric.formatter} |
| 81 | + inline |
| 82 | + className={css.checkThreshold} |
| 83 | + /> |
| 84 | + </p> |
| 85 | + </Alert> |
| 86 | + ); |
| 87 | +}; |
| 88 | + |
| 89 | +export interface BudgetsProps extends React.HTMLAttributes<HTMLElement> { |
| 90 | + budgets: Array<BudgetResult>; |
| 91 | +} |
| 92 | + |
| 93 | +export const Budgets = (props: BudgetsProps) => { |
| 94 | + const { budgets, ...restProps } = props; |
| 95 | + |
| 96 | + const [matchedBudgets] = useMemo(() => { |
| 97 | + const matched: Array<BudgetEvaluated> = []; |
| 98 | + const unmatched: Array<BudgetEvaluated> = []; |
| 99 | + |
| 100 | + budgets.forEach((budget) => { |
| 101 | + // @ts-expect-error |
| 102 | + if (typeof budget.data === 'undefined') { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + // @ts-expect-error |
| 107 | + if (typeof budget.matched === 'undefined') { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + // @ts-expect-error |
| 112 | + if (budget.matched === true) { |
| 113 | + matched.push(budget as BudgetEvaluated); |
| 114 | + // @ts-expect-error |
| 115 | + } else if (budget.matched === false) { |
| 116 | + unmatched.push(budget as BudgetEvaluated); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + return [matched, unmatched]; |
| 121 | + }, [budgets]); |
| 122 | + |
| 123 | + return ( |
| 124 | + <Stack space="xxsmall" {...restProps}> |
| 125 | + {matchedBudgets.map((budget) => ( |
| 126 | + <Budget budget={budget} /> |
| 127 | + ))} |
| 128 | + </Stack> |
| 129 | + ); |
| 130 | +}; |
0 commit comments