|
| 1 | +import React, { useState } from 'react'; |
| 2 | + |
| 3 | +// Renders the value distribution (e.g., "16 (2 times, in launches: 1-2)") |
| 4 | +const DistributionCell: React.FC<{ data: any }> = ({ data }) => { |
| 5 | + if (!data) return null; |
| 6 | + if (data.diff_type === 'summary') { |
| 7 | + return <span className="text-gray-500 italic">{data.summary_text}</span>; |
| 8 | + } |
| 9 | + if (data.diff_type === 'distribution' && data.values) { |
| 10 | + return ( |
| 11 | + <ul className="list-none m-0 p-0 space-y-1"> |
| 12 | + {data.values.map((item: any, index: number) => { |
| 13 | + const launchRanges = item.launches |
| 14 | + .map((r: any) => (r.start === r.end ? `${r.start + 1}` : `${r.start + 1}-${r.end + 1}`)) |
| 15 | + .join(', '); |
| 16 | + return ( |
| 17 | + <li key={index}> |
| 18 | + <span className="font-mono bg-gray-100 px-1 rounded">{JSON.stringify(item.value)}</span> |
| 19 | + <span className="text-gray-500 text-xs ml-2">({item.count} times, in launches: {launchRanges})</span> |
| 20 | + </li> |
| 21 | + ); |
| 22 | + })} |
| 23 | + </ul> |
| 24 | + ); |
| 25 | + } |
| 26 | + return <span className="font-mono">{JSON.stringify(data)}</span>; |
| 27 | +}; |
| 28 | + |
| 29 | +// Renders a single row in the ArgumentViewer table |
| 30 | +const ArgumentRow: React.FC<{ |
| 31 | + argName: string; |
| 32 | + argData: any; |
| 33 | + isDiffViewer?: boolean; |
| 34 | +}> = ({ argName, argData, isDiffViewer = false }) => { |
| 35 | + // Case 1: This is a complex argument with internal differences |
| 36 | + if (isDiffViewer && argData.diff_type === "argument_diff") { |
| 37 | + const [isCollapsed, setIsCollapsed] = useState(false); |
| 38 | + const { sames, diffs } = argData; |
| 39 | + const hasSames = Object.keys(sames).length > 0; |
| 40 | + const hasDiffs = Object.keys(diffs).length > 0; |
| 41 | + |
| 42 | + return ( |
| 43 | + <div className="bg-gray-50 border-b border-gray-200 last:border-b-0"> |
| 44 | + <div |
| 45 | + className="flex items-center space-x-4 p-2 cursor-pointer" |
| 46 | + onClick={() => setIsCollapsed(!isCollapsed)} |
| 47 | + > |
| 48 | + <div className="w-48 flex-shrink-0 font-semibold text-gray-800 break-all">{argName}</div> |
| 49 | + <div className="flex-1 text-gray-500 italic text-sm flex items-center"> |
| 50 | + Complex argument with internal differences |
| 51 | + {/* Dropdown arrow icon */} |
| 52 | + <svg className={`w-4 h-4 ml-2 transform transition-transform ${isCollapsed ? '' : 'rotate-90'}`} fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M9 5l7 7-7 7"></path></svg> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + {!isCollapsed && ( |
| 56 | + <div className="pb-2 px-4 space-y-3"> |
| 57 | + {hasSames && ( |
| 58 | + <div> |
| 59 | + <h6 className="text-sm font-semibold text-gray-600 mb-1">Unchanged Properties</h6> |
| 60 | + <div className="space-y-1 pl-4"> |
| 61 | + {Object.entries(sames).map(([key, value]) => ( |
| 62 | + <div key={key} className="flex items-start text-sm"> |
| 63 | + <span className="w-28 font-mono text-gray-500 flex-shrink-0">{key}:</span> |
| 64 | + <span className="font-mono">{JSON.stringify(value)}</span> |
| 65 | + </div> |
| 66 | + ))} |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + )} |
| 70 | + {hasDiffs && ( |
| 71 | + <div> |
| 72 | + <h6 className="text-sm font-semibold text-gray-600 mb-1">Differing Properties</h6> |
| 73 | + <div className="space-y-2 pl-4"> |
| 74 | + {Object.entries(diffs).map(([key, value]) => ( |
| 75 | + <div key={key} className="flex items-start text-sm"> |
| 76 | + <span className="w-28 font-mono text-gray-500 flex-shrink-0">{key}:</span> |
| 77 | + <div className="flex-1"><DistributionCell data={value} /></div> |
| 78 | + </div> |
| 79 | + ))} |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + )} |
| 83 | + </div> |
| 84 | + )} |
| 85 | + </div> |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + // Case 2: This is a simple argument (in the "Sames" table) |
| 90 | + return ( |
| 91 | + <div className="flex items-start space-x-4 p-2 border-b border-gray-200 last:border-b-0"> |
| 92 | + <div className="w-48 flex-shrink-0 font-semibold text-gray-800 break-all">{argName}</div> |
| 93 | + <div className="w-48 flex-shrink-0 text-gray-600">{argData.type}</div> |
| 94 | + <div className="flex-1 font-mono text-sm"> |
| 95 | + {typeof argData.value !== 'object' || argData.value === null ? |
| 96 | + <span>{String(argData.value)}</span> : |
| 97 | + <pre className="text-xs whitespace-pre-wrap break-all">{JSON.stringify(argData, null, 2)}</pre> |
| 98 | + } |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + ); |
| 102 | +}; |
| 103 | + |
| 104 | +// Main container component |
| 105 | +const ArgumentViewer: React.FC<{ args: Record<string, any>; isDiffViewer?: boolean; }> = ({ args, isDiffViewer = false }) => { |
| 106 | + if (!args || Object.keys(args).length === 0) { |
| 107 | + return <div className="text-sm text-gray-500 p-2">No arguments to display.</div>; |
| 108 | + } |
| 109 | + |
| 110 | + // A "complex view" is needed if we are showing diffs and at least one of them is a complex argument_diff |
| 111 | + const isComplexView = isDiffViewer && Object.values(args).some(arg => arg.diff_type === 'argument_diff'); |
| 112 | + |
| 113 | + return ( |
| 114 | + <div className="border border-gray-200 rounded-md bg-white"> |
| 115 | + {/* Render header only for the simple, non-complex table view */} |
| 116 | + {!isComplexView && ( |
| 117 | + <div className="flex items-center space-x-4 p-2 bg-gray-100 font-bold text-gray-800 border-b border-gray-200"> |
| 118 | + <div className="w-48 flex-shrink-0">Argument Name</div> |
| 119 | + <div className="w-48 flex-shrink-0">Type</div> |
| 120 | + <div className="flex-1">Value</div> |
| 121 | + </div> |
| 122 | + )} |
| 123 | + |
| 124 | + {/* Rows */} |
| 125 | + <div> |
| 126 | + {Object.entries(args).map(([argName, argData]) => ( |
| 127 | + <ArgumentRow key={argName} argName={argName} argData={argData} isDiffViewer={isDiffViewer} /> |
| 128 | + ))} |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + ); |
| 132 | +}; |
| 133 | + |
| 134 | +export default ArgumentViewer; |
0 commit comments