-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathuseRunDiff.ts
More file actions
244 lines (218 loc) · 10.5 KB
/
Copy pathuseRunDiff.ts
File metadata and controls
244 lines (218 loc) · 10.5 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
import { useEffect, useMemo, useState } from 'react';
import { diffLines, Change } from 'diff';
import { getRunDiff, RunDiffResponse } from '../api/storage';
export type TextDiffFormat = 'text' | 'markdown' | 'html';
export const TEXT_DIFF_FORMATS: TextDiffFormat[] = ['text', 'markdown', 'html'];
export const DIFF_FORMAT_LABELS: Record<string, string> = {
text: 'Text Content',
markdown: 'Markdown',
html: 'HTML',
};
export type CapturedListRow = Record<string, any>;
export type CapturedListDiffRow = {
key: string;
previous?: CapturedListRow;
current?: CapturedListRow;
status: 'added' | 'removed' | 'modified' | 'unchanged';
};
const stableRowValue = (value: any): any => Array.isArray(value)
? value.map(stableRowValue)
: value && typeof value === 'object'
? Object.keys(value).sort().reduce((result, key) => ({ ...result, [key]: stableRowValue(value[key]) }), {})
: value;
const rowSignature = (row: CapturedListRow) => JSON.stringify(stableRowValue(row));
export const valuesEqual = (left: any, right: any) => JSON.stringify(stableRowValue(left)) === JSON.stringify(stableRowValue(right));
const matchCapturedListRows = (previous: CapturedListRow[], current: CapturedListRow[]): CapturedListDiffRow[] => {
const unmatchedPrevious = new Set(previous.map((_, index) => index));
const unmatchedCurrent = new Set(current.map((_, index) => index));
const matches: Array<{ previousIndex?: number; currentIndex?: number }> = [];
current.forEach((row, currentIndex) => {
const signature = rowSignature(row);
const previousIndex = Array.from(unmatchedPrevious).find((index) => rowSignature(previous[index]) === signature);
if (previousIndex === undefined) return;
matches.push({ previousIndex, currentIndex });
unmatchedPrevious.delete(previousIndex);
unmatchedCurrent.delete(currentIndex);
});
const columns = Array.from(new Set([...previous, ...current].flatMap((row) => Object.keys(row || {}))));
const identityColumn = columns.find((column) => {
if (!/(^id$|url|href|sku|email|name|title)/i.test(column)) return false;
const previousValues = previous.map((row) => row?.[column]).filter((value) => value != null && value !== '').map(String);
const currentValues = current.map((row) => row?.[column]).filter((value) => value != null && value !== '').map(String);
return new Set(previousValues).size === previousValues.length
&& new Set(currentValues).size === currentValues.length
&& previousValues.some((value) => currentValues.includes(value));
});
if (identityColumn) {
Array.from(unmatchedCurrent).forEach((currentIndex) => {
const identity = current[currentIndex]?.[identityColumn];
if (identity == null || identity === '') return;
const previousIndex = Array.from(unmatchedPrevious).find(
(index) => String(previous[index]?.[identityColumn]) === String(identity),
);
if (previousIndex === undefined) return;
matches.push({ previousIndex, currentIndex });
unmatchedPrevious.delete(previousIndex);
unmatchedCurrent.delete(currentIndex);
});
}
while (unmatchedPrevious.size && unmatchedCurrent.size) {
const previousIndex = unmatchedPrevious.values().next().value as number;
const currentIndex = unmatchedCurrent.values().next().value as number;
matches.push({ previousIndex, currentIndex });
unmatchedPrevious.delete(previousIndex);
unmatchedCurrent.delete(currentIndex);
}
unmatchedPrevious.forEach((previousIndex) => matches.push({ previousIndex }));
unmatchedCurrent.forEach((currentIndex) => matches.push({ currentIndex }));
return matches
.sort((left, right) => (left.currentIndex ?? Number.MAX_SAFE_INTEGER) - (right.currentIndex ?? Number.MAX_SAFE_INTEGER))
.map(({ previousIndex, currentIndex }, index) => {
const previousRow = previousIndex === undefined ? undefined : previous[previousIndex];
const currentRow = currentIndex === undefined ? undefined : current[currentIndex];
const status = !previousRow ? 'added' : !currentRow ? 'removed' : valuesEqual(previousRow, currentRow) ? 'unchanged' : 'modified';
return { key: `${previousIndex ?? 'new'}-${currentIndex ?? 'removed'}-${index}`, previous: previousRow, current: currentRow, status };
});
};
export const displayCapturedValue = (value: any) => value == null
? '—'
: typeof value === 'object' ? JSON.stringify(value) : String(value);
export function useRunDiff(runId: string | undefined) {
const [diffData, setDiffData] = useState<RunDiffResponse | null>(null);
const [isDiffLoading, setIsDiffLoading] = useState(false);
const [selectedDiffFormat, setSelectedDiffFormat] = useState('text');
const [selectedCapturedGroup, setSelectedCapturedGroup] = useState('');
const [selectedCapturedList, setSelectedCapturedList] = useState('');
useEffect(() => {
if (!runId) return;
let isCancelled = false;
setIsDiffLoading(true);
getRunDiff(runId).then((data) => {
if (isCancelled) return;
setDiffData(data);
const availableFormats = data ? [
...TEXT_DIFF_FORMATS.filter((format) => data.formats?.[format]),
...(data.capturedText ? ['captured-text'] : []),
...(data.capturedLists ? ['captured-list'] : []),
] : [];
const firstChangedFormat = availableFormats.find((format) => {
return data?.changedFormats?.includes(format);
});
const initialFormat = firstChangedFormat || availableFormats[0];
if (initialFormat) setSelectedDiffFormat(initialFormat);
if (data?.capturedText) {
try {
const currentGroups = JSON.parse(data.capturedText.current || '{}');
const previousGroups = JSON.parse(data.capturedText.previous || '{}');
setSelectedCapturedGroup(Object.keys(currentGroups)[0] || Object.keys(previousGroups)[0] || '');
} catch {
setSelectedCapturedGroup('');
}
}
if (data?.capturedLists) {
setSelectedCapturedList(
Object.keys(data.capturedLists.current || {})[0]
|| Object.keys(data.capturedLists.previous || {})[0]
|| '',
);
}
}).finally(() => {
if (!isCancelled) setIsDiffLoading(false);
});
return () => { isCancelled = true; };
}, [runId]);
const diffParts = useMemo<Change[]>(() => {
if (!diffData) return [];
const selectedOutput = selectedDiffFormat === 'captured-text'
? diffData.capturedText
: diffData.formats?.[selectedDiffFormat as TextDiffFormat];
const previous = selectedOutput?.previous ?? (selectedDiffFormat === 'text' ? diffData.previousText : '');
const current = selectedOutput?.current ?? (selectedDiffFormat === 'text' ? diffData.currentText : '');
return diffLines(previous, current, { ignoreWhitespace: true });
}, [diffData, selectedDiffFormat]);
const hasDiff = diffParts.some((part) => part.added || part.removed);
const diffOptions = useMemo(() => {
if (!diffData) return [];
return [
...TEXT_DIFF_FORMATS.filter((format) => diffData.formats?.[format]).map((format) => ({ key: format, label: DIFF_FORMAT_LABELS[format] })),
...(diffData.capturedText ? [{ key: 'captured-text', label: 'Captured Text' }] : []),
...(diffData.capturedLists ? [{ key: 'captured-list', label: 'Captured Lists' }] : []),
];
}, [diffData]);
const capturedGroups = useMemo(() => {
if (!diffData?.capturedText) return {} as Record<string, { previous: any[]; current: any[] }>;
try {
const previous = JSON.parse(diffData.capturedText.previous || '{}');
const current = JSON.parse(diffData.capturedText.current || '{}');
return Array.from(new Set([...Object.keys(previous), ...Object.keys(current)])).reduce((groups, name) => {
groups[name] = {
previous: Array.isArray(previous[name]) ? previous[name] : previous[name] == null ? [] : [previous[name]],
current: Array.isArray(current[name]) ? current[name] : current[name] == null ? [] : [current[name]],
};
return groups;
}, {} as Record<string, { previous: any[]; current: any[] }>);
} catch {
return {} as Record<string, { previous: any[]; current: any[] }>;
}
}, [diffData]);
const capturedTableRows = useMemo(() => {
const group = capturedGroups[selectedCapturedGroup];
if (!group) return [];
const rowCount = Math.max(group.previous.length, group.current.length);
const rows: Array<{ key: string; label: string; previous: any; current: any; changed: boolean }> = [];
for (let rowIndex = 0; rowIndex < rowCount; rowIndex += 1) {
const previousRow = group.previous[rowIndex] || {};
const currentRow = group.current[rowIndex] || {};
Array.from(new Set([...Object.keys(previousRow), ...Object.keys(currentRow)])).forEach((label) => {
const previous = previousRow[label];
const current = currentRow[label];
rows.push({
key: `${rowIndex}-${label}`,
label: rowCount > 1 ? `${label} (row ${rowIndex + 1})` : label,
previous,
current,
changed: JSON.stringify(previous) !== JSON.stringify(current),
});
});
}
return rows;
}, [capturedGroups, selectedCapturedGroup]);
const capturedListGroups = useMemo(() => {
const previous = diffData?.capturedLists?.previous || {};
const current = diffData?.capturedLists?.current || {};
return Array.from(new Set([...Object.keys(previous), ...Object.keys(current)])).reduce((groups, name) => {
groups[name] = {
previous: Array.isArray(previous[name]) ? previous[name] : [],
current: Array.isArray(current[name]) ? current[name] : [],
};
return groups;
}, {} as Record<string, { previous: CapturedListRow[]; current: CapturedListRow[] }>);
}, [diffData]);
const capturedListRows = useMemo(() => {
const group = capturedListGroups[selectedCapturedList];
return group ? matchCapturedListRows(group.previous, group.current) : [];
}, [capturedListGroups, selectedCapturedList]);
const capturedListColumns = useMemo(() => {
const group = capturedListGroups[selectedCapturedList];
if (!group) return [];
return Array.from(new Set([...group.previous, ...group.current].flatMap((row) => Object.keys(row || {}))));
}, [capturedListGroups, selectedCapturedList]);
return {
diffData,
isDiffLoading,
selectedDiffFormat,
setSelectedDiffFormat,
selectedCapturedGroup,
setSelectedCapturedGroup,
selectedCapturedList,
setSelectedCapturedList,
diffParts,
hasDiff,
diffOptions,
capturedGroups,
capturedTableRows,
capturedListGroups,
capturedListRows,
capturedListColumns,
};
}