|
| 1 | +import type { DiffResult } from "json-diff-kit"; |
| 2 | +import type { ListChildComponentProps } from "react-window"; |
| 3 | + |
| 4 | +import { Viewer } from "json-diff-kit"; |
| 5 | + |
| 6 | +import type { DiffRowOrCollapsed } from "../types"; |
| 7 | + |
| 8 | +import { DIFF_VIEWER_CLASS, isCollapsed } from "../utils/constants"; |
| 9 | + |
| 10 | +function ViewerRow({ |
| 11 | + index, |
| 12 | + style, |
| 13 | + data, |
| 14 | +}: ListChildComponentProps<{ |
| 15 | + leftDiff: DiffRowOrCollapsed[]; |
| 16 | + rightDiff: DiffRowOrCollapsed[]; |
| 17 | + onExpand: (segmentIndex: number) => void; |
| 18 | + searchTerm?: string; |
| 19 | +}>) { |
| 20 | + const { onExpand, searchTerm } = data; |
| 21 | + const originalLeftLine = data.leftDiff[index]; |
| 22 | + const originalRightLine = data.rightDiff[index]; |
| 23 | + |
| 24 | + if (isCollapsed(originalLeftLine)) { |
| 25 | + return ( |
| 26 | + <div className="collapsed-button" style={style}> |
| 27 | + <button onClick={() => onExpand(originalLeftLine.segmentIndex)} className="text-blue-500 underline"> |
| 28 | + Show Hidden Lines |
| 29 | + </button> |
| 30 | + </div> |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + const leftLine = { ...originalLeftLine } as DiffResult; |
| 35 | + const rightLine = { ...originalRightLine } as DiffResult; |
| 36 | + |
| 37 | + return ( |
| 38 | + <div style={style}> |
| 39 | + <Viewer |
| 40 | + indent={1} |
| 41 | + className={`${DIFF_VIEWER_CLASS} ${searchTerm ? "has-search" : ""}`} |
| 42 | + lineNumbers |
| 43 | + diff={[[leftLine], [rightLine]]} |
| 44 | + highlightInlineDiff |
| 45 | + inlineDiffOptions={{ mode: "word" }} |
| 46 | + syntaxHighlight={{ theme: "monokai" }} |
| 47 | + /> |
| 48 | + </div> |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +export default ViewerRow; |
0 commit comments