Skip to content

Commit f44e42f

Browse files
committed
type narrowing
1 parent d65392b commit f44e42f

File tree

1 file changed

+23
-52
lines changed

1 file changed

+23
-52
lines changed

site/src/pages/DeploymentSettingsPage/LicensesSettingsPage/ManagedAgentsConsumption.tsx

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,9 @@ interface ManagedAgentsConsumptionProps {
1717
managedAgentFeature?: Feature;
1818
}
1919

20-
const validateFeature = (feature?: Feature): string | null => {
21-
if (!feature) {
22-
return null; // No feature is valid (will show disabled state)
23-
}
24-
25-
// If enabled, we need valid numeric data
26-
if (feature.enabled) {
27-
if (
28-
feature.actual === undefined ||
29-
feature.soft_limit === undefined ||
30-
feature.limit === undefined
31-
) {
32-
return "Managed agent feature is enabled but missing required usage data (actual, soft_limit, or limit).";
33-
}
34-
35-
if (feature.actual < 0 || feature.soft_limit < 0 || feature.limit < 0) {
36-
return "Managed agent feature contains invalid negative values for usage metrics.";
37-
}
38-
39-
if (feature.soft_limit > feature.limit) {
40-
return "Managed agent feature has invalid configuration: soft limit exceeds total limit.";
41-
}
42-
43-
// Validate usage period if present
44-
if (feature.usage_period) {
45-
const start = dayjs(feature.usage_period.start);
46-
const end = dayjs(feature.usage_period.end);
47-
48-
if (!start.isValid() || !end.isValid()) {
49-
return "Managed agent feature has invalid usage period dates.";
50-
}
51-
52-
if (end.isBefore(start)) {
53-
return "Managed agent feature has invalid usage period: end date is before start date.";
54-
}
55-
}
56-
}
57-
58-
return null; // Valid
59-
};
60-
6120
export const ManagedAgentsConsumption: FC<ManagedAgentsConsumptionProps> = ({
6221
managedAgentFeature,
6322
}) => {
64-
// Validate the feature data
65-
const validationError = validateFeature(managedAgentFeature);
66-
if (validationError) {
67-
return <ErrorAlert error={new Error(validationError)} />;
68-
}
69-
7023
// If no feature is provided or it's disabled, show disabled state
7124
if (!managedAgentFeature?.enabled) {
7225
return (
@@ -85,11 +38,29 @@ export const ManagedAgentsConsumption: FC<ManagedAgentsConsumptionProps> = ({
8538
);
8639
}
8740

88-
const usage = managedAgentFeature.actual || 0;
89-
const included = managedAgentFeature.soft_limit || 0;
90-
const limit = managedAgentFeature.limit || 0;
91-
const startDate = managedAgentFeature.usage_period?.start || "";
92-
const endDate = managedAgentFeature.usage_period?.end || "";
41+
const usage = managedAgentFeature.actual;
42+
const included = managedAgentFeature.soft_limit;
43+
const limit = managedAgentFeature.limit;
44+
const startDate = managedAgentFeature.usage_period?.start;
45+
const endDate = managedAgentFeature.usage_period?.end;
46+
47+
if (!usage || usage < 0) {
48+
return <ErrorAlert error="Invalid usage data" />;
49+
}
50+
51+
if (!included || included < 0 || !limit || limit < 0) {
52+
return <ErrorAlert error="Invalid license usage limits" />;
53+
}
54+
55+
if (!startDate || !endDate) {
56+
return <ErrorAlert error="Missing license usage period" />;
57+
}
58+
59+
const start = dayjs(startDate);
60+
const end = dayjs(endDate);
61+
if (!start.isValid() || !end.isValid() || !start.isBefore(end)) {
62+
return <ErrorAlert error="Invalid license usage period" />;
63+
}
9364

9465
const usagePercentage = Math.min((usage / limit) * 100, 100);
9566
const includedPercentage = Math.min((included / limit) * 100, 100);

0 commit comments

Comments
 (0)