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