'use client' import type { FileStatusCode } from '@devframes/service-git' import type { DiffChangeType, DiffFileChange } from './parse-patch' import { useMemo, useState } from 'react' import { cn } from '../../lib/utils' import { Badge } from '../ui/badge' import { FileIcon } from '../ui/file-icon' import { Icon } from '../ui/icon' import { ScrollArea } from '../ui/scroll-area' import { Skeleton } from '../ui/skeleton' import { StatusMark } from '../ui/status-mark' import { buildFileModel } from './build-model' import { DiffFile } from './diff-file' import { parseUnifiedPatch } from './parse-patch' /** How the changed files are laid out. */ type DiffLayout = 'flat' | 'collapsible' export interface DiffPatchViewProps { /** Raw unified/git patch text; `null` while unavailable. */ patch: string | null loading: boolean /** True when the patch was clipped server-side (a size cap). */ truncated: boolean /** Set `false` to render inline when a scrolling parent already wraps it. */ scroll?: boolean /** * `flat` lists every file's diff; `collapsible` puts each behind a disclosure * header (a scannable list that expands on demand). Defaults to `flat`. */ layout?: DiffLayout } /** Map a parsed file's change type to a git status code (for the status mark). */ function changeTypeStatus(type: DiffChangeType): FileStatusCode { switch (type) { case 'new': return 'added' case 'deleted': return 'deleted' case 'rename-pure': case 'rename-changed': return 'renamed' default: return 'modified' } } /** * A single file's diff behind a clickable disclosure header (filename, change * icon and +/- counts). The diff body mounts only while expanded, so a * many-file commit stays a scannable list (and does no highlight work) until * you open a file. */ function DiffFileSection({ file, defaultOpen }: { file: DiffFileChange, defaultOpen: boolean }) { const [open, setOpen] = useState(defaultOpen) const model = useMemo(() => buildFileModel(file), [file]) const label = file.prevName ? `${file.prevName} → ${file.name}` : file.name return (
{open && }
) } /** A file's diff rendered inline (flat layout), with its filename header. */ function DiffFileBlock({ file }: { file: DiffFileChange }) { const model = useMemo(() => buildFileModel(file), [file]) const label = file.prevName ? `${file.prevName} → ${file.name}` : file.name return (
{label} {`+${file.additions}`} {' '} {`−${file.deletions}`}
) } /** * Render a unified git patch with in-house diff rendering: the patch is parsed * client-side and each file's changed lines are syntax-highlighted through the * shared `@devframes/service-shiki` service (with intra-line word emphasis), so * the client ships no highlighter of its own. Set `scroll={false}` to render * inline; use `layout="collapsible"` for an expandable per-file list. */ export function DiffPatchView({ patch, loading, truncated, scroll = true, layout = 'flat' }: DiffPatchViewProps) { const files = useMemo(() => (patch ? parseUnifiedPatch(patch) : []), [patch]) if (loading) return if (!patch || files.length === 0) return

No textual diff available (binary or unchanged).

if (layout === 'collapsible') { return (
{files.map((file, i) => ( ))} {truncated &&

Patch truncated.

}
) } const body = ( <>
{files.map((file, i) => ( ))}
{truncated &&

Patch truncated.

} ) if (!scroll) return
{body}
return {body} }