Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | 10x 10x 10x 70x 35x 34x 16x 34x 5x 4x 4x 4x 8x 23x 14x 88x 5x 4x 4x 4x 4x 4x 4x 10x 42x 13x 21x 10x 10x 15x 30x 10x 10x 10x 10x 4x 4x 4x | import { Badge } from "@/ahuora-design-system/ui/badge";
import type {
EconomicsResultRunRead,
EconomicsStudyFullRead,
ResultStatePayload,
WarningSummary,
} from "@/api/apiStore.gen";
import {
economicsResultIsStale,
staleReasonLabel,
} from "../../shared/model/economicsStaleReasons";
import { EconomicsWarningNotice } from "../../shared/ui/EconomicsWarningNotice";
import {
visibleResultWarnings,
warningRefsFromPayload,
} from "../model/resultWarnings";
export function StatusBadge({
resultState,
result,
}: {
resultState: ResultStatePayload;
result?: EconomicsResultRunRead;
}) {
const displayState = resultDisplayState(resultState, result);
return (
<Badge variant="secondary" size="xs" borderRadius="round">
{resultStatusLabel(displayState.status)}
</Badge>
);
}
export function EmptyResultsState() {
return (
<div
className="rounded-md border bg-background p-3 text-sm text-muted-foreground"
aria-label="No economics results"
>
No calculated results are available for this study.
</div>
);
}
export function ResultStateNotice({
resultState,
result,
}: {
resultState: ResultStatePayload;
result: EconomicsResultRunRead;
}) {
const displayState = resultDisplayState(resultState, result);
const stale = economicsResultIsStale(displayState);
const staleReason = displayState.latest_stale_reason;
Iif (!stale) return null;
return (
<EconomicsWarningNotice aria-label="Economics stale result state">
<div className="font-medium">Result state</div>
<div className="flex flex-wrap gap-2">
<Badge variant="secondary" size="xs" borderRadius="round">
{resultStatusLabel(displayState.status)}
</Badge>
<Badge variant="secondary" size="xs" borderRadius="round">
{resultStatusLabel(displayState.classification)}
</Badge>
{displayState.requires_solve && (
<Badge variant="secondary" size="xs" borderRadius="round">
recalculation needed
</Badge>
)}
</div>
{staleReason && (
<p className="mt-2">Changed: {staleReasonLabel(staleReason)}</p>
)}
</EconomicsWarningNotice>
);
}
function resultDisplayState(
resultState: ResultStatePayload,
result?: EconomicsResultRunRead,
): ResultStatePayload | EconomicsResultRunRead {
if (economicsResultIsStale(resultState)) {
return resultState;
}
return result ?? resultState;
}
export function WarningsPanel({
result,
fullStudy,
}: {
result: EconomicsResultRunRead;
fullStudy?: EconomicsStudyFullRead;
}) {
const lineWarnings = result.lines.flatMap((line) =>
warningRefsFromPayload(line.warning_payload, line.row_key),
);
const warnings = visibleResultWarnings([
...result.warnings,
...lineWarnings.map((warning) => ({
code: warning.code,
severity: warning.severity,
message: warning.message,
})),
]);
Iif (warnings.length === 0) return null;
return (
<EconomicsWarningNotice aria-label="Economics warning list">
<div className="font-medium">Warnings</div>
<div className="space-y-1">
{warnings.map((warning) => (
<WarningMessage
key={`${warning.code}-${warning.message}`}
warning={warning}
fullStudy={fullStudy}
/>
))}
</div>
</EconomicsWarningNotice>
);
}
function WarningMessage({
warning,
fullStudy,
}: {
warning: WarningSummary;
fullStudy?: EconomicsStudyFullRead;
}) {
const blockedChildren = blockedChildWarnings(warning, fullStudy);
return (
<div className="space-y-1">
<p>{warning.message}</p>
{blockedChildren.length > 0 && (
<div className="space-y-1 leading-snug">
<ul className="list-disc space-y-1 pl-5 text-sm">
{blockedChildren.map((child) => (
<li
key={`${child.label}-${child.reason}`}
className="text-sm font-normal"
>
<span className="font-medium">{child.label}</span>
{child.reason ? `: ${child.reason}` : ""}
</li>
))}
</ul>
</div>
)}
</div>
);
}
function blockedChildWarnings(
warning: WarningSummary,
fullStudy?: EconomicsStudyFullRead,
) {
const context = warning.context;
const blockedChildren = Array.isArray(context?.blocked_children)
? context.blocked_children
: [];
return blockedChildren
.map((child) => blockedChildWarning(child, fullStudy))
.filter((child) => child.label || child.reason);
}
function blockedChildWarning(
value: unknown,
fullStudy?: EconomicsStudyFullRead,
) {
const child = objectValue(value);
const key = stringValue(child?.key);
return {
label: operatingLineLabel(key, fullStudy) || key || "Blocked item",
reason: stringValue(child?.reason),
};
}
function operatingLineLabel(key: string, fullStudy?: EconomicsStudyFullRead) {
const match = /^operating_line:(\d+)$/.exec(key);
Iif (!match) return "";
const lineId = Number(match[1]);
return (
fullStudy?.operating_lines.find((line) => line.id === lineId)?.label ??
`Operating line ${lineId}`
);
}
function objectValue(value: unknown) {
return value && typeof value === "object"
? (value as Record<string, unknown>)
: null;
}
function stringValue(value: unknown) {
return typeof value === "string" ? value : "";
}
function resultStatusLabel(status: string) {
if (status === "not_calculated") return "Not calculated";
Iif (status === "stale") return "Needs recalculation";
Iif (status === "success") return "Calculated";
return status.replace(/_/g, " ");
}
|