|
| 1 | +// VirtualDiffGrid.tsx |
| 2 | +import type { InlineDiffOptions } from "json-diff-kit"; |
| 3 | +import type { Dispatch } from "react"; |
| 4 | +import type { ListOnScrollProps } from "react-window"; |
| 5 | + |
| 6 | +import React, { useCallback, useEffect, useMemo } from "react"; |
| 7 | +import { VariableSizeList as List } from "react-window"; |
| 8 | + |
| 9 | +import type { DiffRowOrCollapsed } from "../types"; |
| 10 | + |
| 11 | +import { useRowHeights } from "../hooks/useRowHeights"; |
| 12 | +import { COLLAPSED_ROW_HEIGHT, getRowHeightFromCSS, isCollapsed } from "../utils/constants"; |
| 13 | +import RowRendererGrid from "../utils/json-diff/row-renderer-grid"; |
| 14 | + |
| 15 | +type ListDataType = { |
| 16 | + leftDiff: DiffRowOrCollapsed[]; |
| 17 | + rightDiff: DiffRowOrCollapsed[]; |
| 18 | + onExpand: (segmentIndex: number) => void; |
| 19 | + inlineDiffOptions?: InlineDiffOptions; |
| 20 | +}; |
| 21 | + |
| 22 | +type VirtualDiffGridProps = { |
| 23 | + leftDiff: DiffRowOrCollapsed[]; |
| 24 | + rightDiff: DiffRowOrCollapsed[]; |
| 25 | + outerRef: React.RefObject<Node | null>; |
| 26 | + listRef: React.RefObject<List<ListDataType>>; |
| 27 | + height: number; |
| 28 | + inlineDiffOptions?: InlineDiffOptions; |
| 29 | + className?: string; |
| 30 | + setScrollTop: Dispatch<React.SetStateAction<number>>; |
| 31 | + onExpand: (segmentIndex: number) => void; |
| 32 | + overScanCount?: number; |
| 33 | +}; |
| 34 | + |
| 35 | +const VirtualDiffGrid: React.FC<VirtualDiffGridProps> = ({ |
| 36 | + leftDiff, |
| 37 | + rightDiff, |
| 38 | + outerRef, |
| 39 | + listRef, |
| 40 | + height, |
| 41 | + inlineDiffOptions, |
| 42 | + className, |
| 43 | + setScrollTop, |
| 44 | + onExpand, |
| 45 | + overScanCount = 10, |
| 46 | +}) => { |
| 47 | + // Virtual List Data |
| 48 | + const listData = useMemo( |
| 49 | + () => ({ |
| 50 | + leftDiff, |
| 51 | + rightDiff, |
| 52 | + onExpand, |
| 53 | + inlineDiffOptions, |
| 54 | + }), |
| 55 | + [leftDiff, rightDiff, onExpand, inlineDiffOptions], |
| 56 | + ); |
| 57 | + |
| 58 | + const classes = [ |
| 59 | + "json-diff-viewer", |
| 60 | + `json-diff-viewer-theme-custom`, |
| 61 | + className, |
| 62 | + ] |
| 63 | + .filter(Boolean) |
| 64 | + .join(" "); |
| 65 | + |
| 66 | + // ROW HEIGHT CALCULATION |
| 67 | + const ROW_HEIGHT = useMemo(() => getRowHeightFromCSS(), []); |
| 68 | + const rowHeights = useRowHeights(leftDiff); |
| 69 | + const dynamicRowHeights = useCallback( |
| 70 | + (index: number) => { |
| 71 | + const leftLine = leftDiff[index]; |
| 72 | + if (isCollapsed(leftLine)) |
| 73 | + return COLLAPSED_ROW_HEIGHT; |
| 74 | + return (rowHeights[index] ?? 1) * ROW_HEIGHT; |
| 75 | + }, |
| 76 | + [leftDiff, rowHeights], |
| 77 | + ); |
| 78 | + |
| 79 | + useEffect(() => { |
| 80 | + listRef.current?.resetAfterIndex(0, true); |
| 81 | + }, [rowHeights]); |
| 82 | + |
| 83 | + return ( |
| 84 | + <div className={classes}> |
| 85 | + <List |
| 86 | + height={height} |
| 87 | + width="100%" |
| 88 | + style={{ alignItems: "start" }} |
| 89 | + outerRef={outerRef} |
| 90 | + ref={listRef} |
| 91 | + className="virtual-json-diff-list-container" |
| 92 | + itemCount={Math.max(leftDiff.length, rightDiff.length)} |
| 93 | + itemSize={dynamicRowHeights} |
| 94 | + overscanCount={overScanCount} |
| 95 | + itemData={listData} |
| 96 | + onScroll={({ scrollOffset }: ListOnScrollProps) => setScrollTop(scrollOffset)} |
| 97 | + > |
| 98 | + {RowRendererGrid} |
| 99 | + </List> |
| 100 | + </div> |
| 101 | + ); |
| 102 | +}; |
| 103 | + |
| 104 | +export default VirtualDiffGrid; |
0 commit comments