-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessSummary.tsx
More file actions
383 lines (361 loc) · 11.2 KB
/
Copy pathProcessSummary.tsx
File metadata and controls
383 lines (361 loc) · 11.2 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import { Box, Button, Typography } from "@mui/material";
import type { PieceDetail, PieceState } from "../util/types";
import {
getProcessSummaryDefinition,
getProcessSummaryFieldOptions,
type WorkflowSummaryComputeDefinition,
type WorkflowSummaryCondition,
type WorkflowSummaryItem,
} from "../util/workflow";
import { useOpenPreferencesDialog, useCurrentUser } from "./CurrentUserContext";
type ProcessSummaryProps = {
piece: PieceDetail;
history: PieceState[];
};
type RenderedSummaryItem = {
label: string;
value: string;
description?: string;
};
type RenderedSelectedField = RenderedSummaryItem & {
ref: string;
};
export default function ProcessSummary({
piece,
history,
}: ProcessSummaryProps) {
const currentUser = useCurrentUser();
const openPreferencesDialog = useOpenPreferencesDialog();
const selectedRefs = currentUser?.preferences.process_summary_fields ?? [];
if (selectedRefs.length > 0) {
const fieldsByGroup = renderSelectedFields(piece, history, selectedRefs);
if (fieldsByGroup.length > 0) {
return (
<Box sx={{ display: "grid", gap: 2 }}>
{fieldsByGroup.map((section) => (
<Box key={section.title}>
<Typography
variant="subtitle2"
sx={{ mb: 1, color: "text.secondary", fontWeight: 700 }}
>
{section.title}
</Typography>
<Box
sx={{
display: "grid",
gap: 1,
gridTemplateColumns: "repeat(auto-fit, minmax(180px, 1fr))",
}}
>
{section.fields.map((field) => (
<Box
key={field.ref}
sx={{
minWidth: 0,
borderTop: "1px solid",
borderColor: "divider",
pt: 0.75,
}}
>
<Typography
variant="caption"
sx={{ color: "text.secondary" }}
>
{field.label}
</Typography>
<Typography
variant="body1"
sx={{ overflowWrap: "anywhere" }}
>
{field.value}
</Typography>
{field.description ? (
<Typography
variant="caption"
sx={{ color: "text.secondary" }}
>
{field.description}
</Typography>
) : null}
</Box>
))}
</Box>
</Box>
))}
</Box>
);
}
return (
<Box sx={{ py: 0.5 }}>
<Typography variant="body2" color="text.secondary">
No selected summary fields have values for this piece.
</Typography>
{openPreferencesDialog ? (
<Button
variant="text"
size="small"
onClick={() => openPreferencesDialog("process-summary")}
sx={{ mt: 0.5, px: 0, alignSelf: "flex-start" }}
>
Choose summary fields
</Button>
) : null}
</Box>
);
}
const sections = getProcessSummaryDefinition()
.map((section) => ({
title: section.title,
fields: section.fields
.map((item) => renderSummaryItem(item, history))
.filter((item): item is RenderedSummaryItem => item !== null),
}))
.filter((section) => section.fields.length > 0);
if (sections.length === 0) {
return null;
}
return (
<Box sx={{ display: "grid", gap: 2 }}>
{sections.map((section) => (
<Box key={section.title}>
<Typography
variant="subtitle2"
sx={{ mb: 1, color: "text.secondary", fontWeight: 700 }}
>
{section.title}
</Typography>
<Box
sx={{
display: "grid",
gap: 1,
gridTemplateColumns: "repeat(auto-fit, minmax(180px, 1fr))",
}}
>
{section.fields.map((field) => (
<Box
key={`${section.title}-${field.label}-${field.value}`}
sx={{
minWidth: 0,
borderTop: "1px solid",
borderColor: "divider",
pt: 0.75,
}}
>
<Typography variant="caption" sx={{ color: "text.secondary" }}>
{field.label}
</Typography>
<Typography variant="body1" sx={{ overflowWrap: "anywhere" }}>
{field.value}
</Typography>
{field.description ? (
<Typography
variant="caption"
sx={{ color: "text.secondary" }}
>
{field.description}
</Typography>
) : null}
</Box>
))}
</Box>
</Box>
))}
</Box>
);
}
function renderSelectedFields(
piece: PieceDetail,
history: PieceState[],
selectedRefs: string[],
): { title: string; fields: RenderedSelectedField[] }[] {
const options = new Map(
getProcessSummaryFieldOptions().map((option) => [option.ref, option]),
);
const grouped = new Map<string, RenderedSelectedField[]>();
for (const ref of selectedRefs) {
const option = options.get(ref);
if (!option) continue;
const value = formatRefValue(piece, history, ref);
if (!value) continue;
const fields = grouped.get(option.group) ?? [];
fields.push({
ref,
label: option.label,
value,
description: option.description,
});
grouped.set(option.group, fields);
}
return Array.from(grouped, ([title, fields]) => ({ title, fields }));
}
function renderSummaryItem(
item: WorkflowSummaryItem,
history: PieceState[],
): RenderedSummaryItem | null {
if (!conditionMatches(item.when, history)) {
return null;
}
if (item.kind === "text") {
return item.text
? { label: item.label, value: item.text, description: item.description }
: null;
}
if (item.kind === "value") {
const value = getFieldValue(history, item.stateId, item.fieldName);
const formatted = formatSummaryValue(value);
return formatted
? { label: item.label, value: formatted, description: item.description }
: null;
}
const value = computeValue(item.compute, history);
return value === null
? null
: {
label: item.label,
value: formatComputedValue(value, item.compute),
description: item.description,
};
}
function conditionMatches(
condition: WorkflowSummaryCondition | undefined,
history: PieceState[],
): boolean {
if (!condition) {
return true;
}
if (condition.state_exists) {
return history.some((state) => state.state === condition.state_exists);
}
if (condition.state_missing) {
return !history.some((state) => state.state === condition.state_missing);
}
return true;
}
function getFieldValue(
history: PieceState[],
stateId: string,
fieldName: string,
): unknown {
const state = [...history].reverse().find((entry) => entry.state === stateId);
return state?.custom_fields?.[fieldName];
}
function formatRefValue(
piece: PieceDetail,
history: PieceState[],
ref: string,
): string {
if (ref.startsWith("piece.")) {
return formatSummaryValue(getPieceFieldValue(piece, ref.slice(6)));
}
const [stateId, fieldName] = ref.split(".", 2);
if (!stateId || !fieldName) {
return "";
}
const state = [...history].reverse().find((entry) => entry.state === stateId);
if (!state) {
return "";
}
if (fieldName === "created" || fieldName === "last_modified") {
return formatDateValue(state[fieldName]);
}
if (fieldName === "notes") {
return formatSummaryValue(state.notes);
}
return formatSummaryValue(state.custom_fields?.[fieldName]);
}
function getPieceFieldValue(piece: PieceDetail, fieldName: string): unknown {
if (fieldName === "name") return piece.name;
if (fieldName === "created") return piece.created;
if (fieldName === "last_modified") return piece.last_modified;
if (fieldName === "current_location") return piece.current_location;
if (fieldName === "showcase_story") return piece.showcase_story;
return undefined;
}
function getNumericValue(
history: PieceState[],
ref: string | undefined,
): number | null {
if (!ref) {
return null;
}
const [stateId, fieldName] = ref.split(".", 2);
const value = getFieldValue(history, stateId, fieldName);
if (typeof value === "number" && Number.isFinite(value)) {
return value;
}
if (typeof value === "string" && value.trim() !== "") {
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : null;
}
return null;
}
function computeValue(
compute: WorkflowSummaryComputeDefinition,
history: PieceState[],
): number | null {
if (compute.op === "sum" || compute.op === "product") {
const operands = compute.operands?.map((ref) =>
getNumericValue(history, ref),
);
if (!operands || operands.some((value) => value === null)) {
return null;
}
const numericOperands = operands as number[];
return compute.op === "sum"
? numericOperands.reduce((total, value) => total + value, 0)
: numericOperands.reduce((total, value) => total * value, 1);
}
if (compute.op === "difference") {
const left = getNumericValue(history, compute.left);
const right = getNumericValue(history, compute.right);
return left === null || right === null ? null : left - right;
}
const numerator = getNumericValue(history, compute.numerator);
const denominator = getNumericValue(history, compute.denominator);
if (numerator === null || denominator === null || denominator === 0) {
return null;
}
return numerator / denominator;
}
function formatComputedValue(
value: number,
compute: WorkflowSummaryComputeDefinition,
): string {
const decimals = compute.decimals ?? 2;
const formatted = value.toLocaleString(undefined, {
maximumFractionDigits: decimals,
minimumFractionDigits: Number.isInteger(value) ? 0 : Math.min(decimals, 2),
});
return compute.unit ? `${formatted} ${compute.unit}` : formatted;
}
function formatDateValue(value: unknown): string {
if (!(value instanceof Date) || Number.isNaN(value.getTime())) {
return "";
}
return value.toLocaleString();
}
function formatSummaryValue(value: unknown): string {
if (value === null || value === undefined || value === "") {
return "";
}
if (value instanceof Date) {
return formatDateValue(value);
}
if (typeof value === "string" || typeof value === "number") {
return String(value);
}
if (typeof value === "boolean") {
return value ? "Yes" : "No";
}
if (typeof value === "object" && value !== null && "name" in value) {
const name = (value as { name?: unknown }).name;
return typeof name === "string" ? name : "";
}
if (Array.isArray(value)) {
return value
.map((item) => formatSummaryValue(item))
.filter(Boolean)
.join(", ");
}
return "";
}