"use client"; import { Check, ChevronDown, Copy, FileCode2, LoaderCircle, } from "lucide-react"; import { AnimatePresence, motion, useReducedMotion } from "motion/react"; import { type ReactNode, useId, useLayoutEffect, useRef } from "react"; import { type AgentCodeLanguage, AgentCodeLine, useAgentCodeTokens, } from "@/components/agents/agent-code"; import { AgentDisclosure } from "@/components/agents/agent-disclosure"; import { IconSwap } from "@/components/agents/icon-swap"; import { Tooltip } from "@/components/motion/tooltip"; import { EASE_OUT, SPRING_PRESS, SPRING_SWAP } from "@/lib/ease"; const DIFFSTAT_BLOCKS = 5; // A streamed line settles quickly: lines can arrive many times a second. const LINE_IN = { duration: 0.18, ease: EASE_OUT } as const; import { useCopy } from "@/lib/hooks/use-copy"; import { useStatusOpen } from "@/lib/hooks/use-status-open"; import { formatNumber, useLocale, useMessages } from "@/lib/i18n"; import { cn } from "@/lib/utils"; export type FileDiffStatus = "streaming" | "complete"; export type FileDiffLineType = "added" | "removed" | "context"; export interface FileDiffLine { /** Stable key, so streamed lines keep their place. */ id: string; /** Marks the line added (green, +), removed (red, −) or unchanged. Default: "context" */ type?: FileDiffLineType; /** Line number in the old file, shown in the first gutter. Blank when not set. */ oldLine?: number; /** Line number in the new file, shown in the second gutter. Blank when not set. */ newLine?: number; /** The line's code, without the + or − marker. Syntax highlighted. */ content: string; } /** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */ export const fileDiffMessages = { applying: "Applying changes", applied: "Changes applied", changes: "File changes", copy: "Copy diff", copied: "Copied", }; export type FileDiffMessages = typeof fileDiffMessages; export interface FileDiffProps { /** File name or path shown in the header. */ file: ReactNode; /** Diff lines, each added, removed or context, with optional line numbers. */ lines: FileDiffLine[]; /** "streaming" while lines arrive, "complete" once the edit lands. Default: "streaming" */ status?: FileDiffStatus; /** Whether the diff is expanded, for controlled use. */ open?: boolean; /** Whether the diff starts expanded when uncontrolled. Default: true */ defaultOpen?: boolean; /** Called with the new state when the diff expands or collapses. */ onOpenChange?: (open: boolean) => void; /** Fold the diff when status moves from streaming to complete. Default: true */ collapseOnComplete?: boolean; /** Tallest the line viewport grows before it scrolls (px). Default: 220 */ maxHeight?: number; /** Language used for syntax highlighting. Default: "typescript" */ language?: AgentCodeLanguage; /** Text the copy button writes to the clipboard. Shows the button when set. */ copyText?: string; /** Runs instead of the clipboard write when the copy button is pressed. */ onCopy?: () => void | Promise; /** Override any built-in string. */ labels?: Partial; /** Classes merged onto the root element. */ className?: string; } function ChangeCount({ value, type }: { value: number; type: "added" | "removed" }) { const { locale } = useLocale(); if (!value) return null; return ( {type === "added" ? "+" : "−"} {formatNumber(value, locale)} ); } /** * Five blocks split between additions and deletions, so the size and shape * of the change reads at a glance. The counts beside it carry the numbers. */ function DiffStat({ additions, deletions }: { additions: number; deletions: number }) { const total = additions + deletions; if (!total) return null; const added = Math.round((additions / total) * DIFFSTAT_BLOCKS); return (