import { diffLines } from 'diff' import { useMemo } from 'react' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog' import { usePointerFocusRing } from '@/hooks/usePointerFocusRing' import { cn } from '@/lib/utils' export function DiffView(props: { oldString: string newString: string filePath?: string variant?: 'preview' | 'inline' }) { const variant = props.variant ?? 'preview' const { suppressFocusRing, onTriggerPointerDown, onTriggerKeyDown, onTriggerBlur } = usePointerFocusRing() const stats = useMemo(() => { const oldChars = props.oldString.length const newChars = props.newString.length const oldLabel = `${oldChars.toLocaleString()} chars` const newLabel = `${newChars.toLocaleString()} chars` return { oldChars, newChars, label: `old: ${oldLabel} → new: ${newLabel}` } }, [props.oldString.length, props.newString.length]) const title = props.filePath ? props.filePath : 'Diff' const subtitle = props.filePath ? stats.label : `Diff • ${stats.label}` const DiffInline = ( ) if (variant === 'inline') { return DiffInline } return ( {title} {stats.label}
{DiffInline}
) } function DiffInlineView(props: { oldString: string newString: string filePath?: string }) { const diff = useMemo(() => diffLines(props.oldString, props.newString), [props.oldString, props.newString]) return (
{props.filePath ? (
{props.filePath}
) : null}
{diff.map((part, i) => { const lines = part.value.split('\n') if (lines.length > 0 && lines[lines.length - 1] === '') { lines.pop() } const prefix = part.added ? '+' : part.removed ? '-' : ' ' const className = cn( part.added && 'bg-[var(--app-diff-added-bg)] text-[var(--app-diff-added-text)]', part.removed && 'bg-[var(--app-diff-removed-bg)] text-[var(--app-diff-removed-text)]' ) return (
{lines.map((line, j) => (
{prefix} {line}
))}
) })}
) }