import classNames from 'classnames'; import { useCallback, useMemo, useState, type ReactNode } from 'react'; import { Text } from 'folds'; import { scaleSystemEmoji } from '$plugins/react-custom-html-parser'; import { buildMessageDiffDisplay, type MessageDiffRow, type TextDiffPart } from '$utils/textDiff'; import * as css from './InlineTextDiff.css'; const SKIP_EXPAND_CHUNK = 20; type InlineTextDiffProps = { oldText: string; newText: string; }; type CodeDiffRow = Exclude; const renderInlineParts = (parts: TextDiffPart[]) => parts.map((part, index) => { const content = scaleSystemEmoji(part.value); const key = `diff-${index}-${part.type}`; if (part.type === 'delete') { return ( {content} ); } if (part.type === 'insert') { return ( {content} ); } return {content}; }); const renderCodeLine = (row: CodeDiffRow, key: string) => { const prefix = row.type === 'delete' ? '-' : row.type === 'insert' ? '+' : ' '; const lineClass = row.type === 'delete' ? css.DiffLineDelete : row.type === 'insert' ? css.DiffLineInsert : css.DiffLineEqual; return (
{prefix} {scaleSystemEmoji(row.text)}
); }; type DiffSkipRowsProps = { skipIndex: number; lines: string[]; placement: 'before' | 'after' | 'between'; revealed: number; onReveal: (skipIndex: number, amount: number) => void; }; function DiffSkipRows({ skipIndex, lines, placement, revealed, onReveal }: DiffSkipRowsProps) { const remaining = lines.length - revealed; const nextReveal = Math.min(SKIP_EXPAND_CHUNK, remaining); const expandUp = placement === 'before'; const arrow = expandUp ? '↑' : '↓'; const revealedLines = placement === 'before' ? lines.slice(Math.max(0, lines.length - revealed)) : lines.slice(0, revealed); const label = `Expand ${remaining <= SKIP_EXPAND_CHUNK ? `all ${remaining}` : nextReveal} unchanged ${remaining === 1 ? 'line' : 'lines'}`; const expandRow = remaining > 0 ? ( ) : null; const revealedRows = revealedLines.map((line, lineIndex) => renderCodeLine({ type: 'equal', text: line }, `skip-${skipIndex}-line-${lineIndex}`) ); if (expandUp) { return ( <> {expandRow} {revealedRows} ); } return ( <> {revealedRows} {expandRow} ); } function InlineDiffBlock({ children }: { children: ReactNode }) { return ( {children} ); } export function InlineTextDiff({ oldText, newText }: InlineTextDiffProps) { const display = useMemo(() => buildMessageDiffDisplay(oldText, newText), [oldText, newText]); const [revealedBySkip, setRevealedBySkip] = useState>({}); const revealSkip = useCallback((skipIndex: number, amount: number) => { setRevealedBySkip((prev) => ({ ...prev, [skipIndex]: (prev[skipIndex] ?? 0) + amount, })); }, []); if (display.mode === 'inline') { if (display.parts.length === 0) { return (empty); } return (
{' '} {renderInlineParts(display.parts)}
); } if (display.rows.length === 0) { return (empty); } let skipIndex = 0; return ( {display.rows.map((row, index) => { if (row.type === 'skip') { const currentSkipIndex = skipIndex; skipIndex += 1; return ( ); } return renderCodeLine(row, `${row.type}-${row.text}-${index}`); })} ); }