import React from 'react'; /** * ComponentCommitDiff - A component that renders git-style diffs with addition/deletion counts * * @param {Object} props * @param {string[]} props.diff - Array of diff lines, each prefixed with '+', '-', or nothing * @returns {JSX.Element} Rendered diff view with statistics */ const ComponentCommitDiff = ({ diff }) => { // Calculate counts of additions and deletions const counts = diff.reduce((acc, line) => { if (line.startsWith('+')) acc.additions++; if (line.startsWith('-')) acc.deletions++; return acc; }, { additions: 0, deletions: 0 }); /** * Determines the type, content, and styling for a diff line * @param {string} line - The diff line to analyze * @returns {Object} Object containing line type, content, and Tailwind classes */ const getLineInfo = (line) => { if (line.startsWith('+')) { return { type: 'addition', content: line.substring(1), className: 'bg-green-500/20 text-green-500' }; } else if (line.startsWith('-')) { return { type: 'deletion', content: line.substring(1), className: 'bg-red-500/20 text-red-500' }; } return { type: 'unchanged', content: line, className: '' }; }; return (
{diff.map((line, index) => {
const { type, content, className } = getLineInfo(line);
return (
{/* Line prefix indicator (+, -, or space) */}
{type === 'addition' && '+'}
{type === 'deletion' && '-'}
{type === 'unchanged' && ' '}
{/* Line content */}
{content}
);
})}