'use client' import type { CSSProperties } from 'react' import type { DiffFileModel, RenderHunk, RenderLine } from './build-model' import type { TokenLines } from './use-diff-tokens' import { cn } from '../../lib/utils' import { Skeleton } from '../ui/skeleton' import { buildSegments } from './render-segments' import { useDiffTokens } from './use-diff-tokens' const NUMBER_CELL = 'w-10 shrink-0 select-none px-1.5 text-right tabular-nums color-faint' /** A line's content, split into syntax-colored segments with intra-line emphasis. */ function LineContent({ line, tokens }: { line: RenderLine, tokens: TokenLines | null }) { const segments = buildSegments(tokens?.[line.tokenLine], line.content, line.wordRanges) const changedBg = line.type === 'add' ? 'bg-success/25' : 'bg-error/25' return ( <> {segments.map((segment, i) => ( {segment.text} ))} ) } /** One diff row: old/new line-number gutters, the +/- marker, and the code. */ function DiffLine({ line, tokens }: { line: RenderLine, tokens: TokenLines | null }) { const bg = line.type === 'add' ? 'bg-success/10' : line.type === 'del' ? 'bg-error/10' : '' const marker = line.type === 'add' ? '+' : line.type === 'del' ? '−' : ' ' const markerColor = line.type === 'add' ? 'text-success' : line.type === 'del' ? 'text-error' : 'color-faint' return (
{line.oldNumber ?? ''} {line.newNumber ?? ''} {marker}
) } /** A hunk: its `@@` header row followed by the hunk's lines. */ function DiffHunk({ hunk, oldTokens, newTokens }: { hunk: RenderHunk, oldTokens: TokenLines | null, newTokens: TokenLines | null }) { return (
{hunk.header}
{hunk.lines.map((line, i) => ( ))}
) } /** * Render a single file's diff: highlight its reconstructed sides through the * shiki service (skeleton until the tokens land) and lay out the hunks. Files * with no textual hunks (binary or metadata-only) show a short note; when the * highlight service is unavailable the diff renders plain (un-highlighted). */ export function DiffFile({ model }: { model: DiffFileModel }) { const hasHunks = model.file.hunks.length > 0 const { oldTokens, newTokens, loading, unavailable } = useDiffTokens(model.oldText, model.newText, model.file.lang, hasHunks) if (!hasHunks) return

No textual diff (binary or metadata-only change).

if (loading && !unavailable) return const oldT = unavailable ? null : oldTokens const newT = unavailable ? null : newTokens return (
{model.hunks.map((hunk, i) => ( ))}
) }