|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useMemo, useState } from 'react' |
| 4 | +import { |
| 5 | + Button, |
| 6 | + cn, |
| 7 | + Table, |
| 8 | + TableBody, |
| 9 | + TableCell, |
| 10 | + TableHead, |
| 11 | + TableHeader, |
| 12 | + TableRow, |
| 13 | +} from '@sim/emcn' |
| 14 | +import { toRecord } from '@sim/utils/object' |
| 15 | +import { |
| 16 | + DIFF_SIGN, |
| 17 | + DIFF_SIGN_CLASS, |
| 18 | +} from '@/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/diff-signs' |
| 19 | +import { diffOrderedRows } from '@/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/ordered-row-diff' |
| 20 | +import { ReadableValue } from '@/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/readable-value' |
| 21 | +import type { StructuredValuePresentation } from '@/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/value-presentation' |
| 22 | +import { maskSecretsDeep } from '@/app/workspace/[workspaceId]/w/components/workflow-diff/utils' |
| 23 | +import type { SubBlockConfig } from '@/blocks/types' |
| 24 | + |
| 25 | +const ROWS_PER_PAGE = 30 |
| 26 | + |
| 27 | +interface StructuredValueDiffProps { |
| 28 | + before: StructuredValuePresentation |
| 29 | + after: StructuredValuePresentation |
| 30 | + config?: SubBlockConfig |
| 31 | + label: string |
| 32 | +} |
| 33 | + |
| 34 | +/** Declared columns and ordered rows, with masking applied only after the comparison. */ |
| 35 | +export function StructuredValueDiff({ before, after, config, label }: StructuredValueDiffProps) { |
| 36 | + const rows = useMemo(() => diffOrderedRows(before.rows, after.rows), [before, after]) |
| 37 | + const columns = [...new Set([...after.columns, ...before.columns])] |
| 38 | + const [visibleCount, setVisibleCount] = useState(ROWS_PER_PAGE) |
| 39 | + const tabular = config?.type === 'table' |
| 40 | + const maskTableValues = tabular && config.password |
| 41 | + const keyColumn = config?.columns?.[0] |
| 42 | + const representationChanged = |
| 43 | + before.sourceKey !== after.sourceKey && rows.every((row) => row.kind === 'context') |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className='min-w-0'> |
| 47 | + {representationChanged && ( |
| 48 | + <span className='text-[var(--text-muted)] text-small'> |
| 49 | + {before.encoding === 'json-text' && after.encoding === 'json-text' |
| 50 | + ? 'Formatting changed' |
| 51 | + : 'Value type changed'} |
| 52 | + </span> |
| 53 | + )} |
| 54 | + <Table aria-label={`${label} changes`} className='table-fixed'> |
| 55 | + <colgroup> |
| 56 | + <col className='w-10' /> |
| 57 | + <col span={tabular ? columns.length : 1} /> |
| 58 | + </colgroup> |
| 59 | + {tabular && ( |
| 60 | + <TableHeader> |
| 61 | + <TableRow> |
| 62 | + <TableHead className='w-10'> |
| 63 | + <span className='sr-only'>Change</span> |
| 64 | + </TableHead> |
| 65 | + {columns.map((column) => ( |
| 66 | + <TableHead key={column}>{column}</TableHead> |
| 67 | + ))} |
| 68 | + </TableRow> |
| 69 | + </TableHeader> |
| 70 | + )} |
| 71 | + <TableBody> |
| 72 | + {rows.slice(0, visibleCount).map((row, index) => { |
| 73 | + const masked = toRecord(maskSecretsDeep({ cells: row.value.cells })) |
| 74 | + const cells = toRecord(masked.cells) |
| 75 | + const sign = row.kind === 'context' ? null : row.kind |
| 76 | + return ( |
| 77 | + <TableRow |
| 78 | + key={index} |
| 79 | + className={cn( |
| 80 | + row.kind === 'added' && 'bg-[var(--badge-success-bg)]', |
| 81 | + row.kind === 'removed' && 'bg-[var(--badge-error-bg)]' |
| 82 | + )} |
| 83 | + > |
| 84 | + <TableCell className='w-10 align-top'> |
| 85 | + {sign && ( |
| 86 | + <span className={cn('font-mono', DIFF_SIGN_CLASS[sign])}> |
| 87 | + <span className='sr-only'>{sign === 'added' ? 'Added' : 'Removed'}</span> |
| 88 | + <span aria-hidden='true'>{DIFF_SIGN[sign]}</span> |
| 89 | + </span> |
| 90 | + )} |
| 91 | + </TableCell> |
| 92 | + {tabular ? ( |
| 93 | + columns.map((column) => { |
| 94 | + const value = cells[column] |
| 95 | + const concealed = |
| 96 | + maskTableValues && column !== keyColumn && value != null && value !== '' |
| 97 | + return ( |
| 98 | + <TableCell key={column} className='align-top'> |
| 99 | + <ReadableValue value={concealed ? '•••' : value} /> |
| 100 | + </TableCell> |
| 101 | + ) |
| 102 | + }) |
| 103 | + ) : ( |
| 104 | + <TableCell className='align-top'> |
| 105 | + <ReadableValue value={cells} /> |
| 106 | + </TableCell> |
| 107 | + )} |
| 108 | + </TableRow> |
| 109 | + ) |
| 110 | + })} |
| 111 | + </TableBody> |
| 112 | + </Table> |
| 113 | + {visibleCount < rows.length && ( |
| 114 | + <Button |
| 115 | + variant='quiet' |
| 116 | + size='sm' |
| 117 | + onClick={() => setVisibleCount((count) => count + ROWS_PER_PAGE)} |
| 118 | + > |
| 119 | + Show more rows |
| 120 | + </Button> |
| 121 | + )} |
| 122 | + </div> |
| 123 | + ) |
| 124 | +} |
0 commit comments