"use client"; import { Check, Copy, FileCode2, LoaderCircle } from "lucide-react"; import { motion, useReducedMotion } from "motion/react"; import { type ReactNode, useLayoutEffect, useMemo, useRef } from "react"; import { type AgentCodeLanguage, AgentCodeLine, useAgentCodeTokens, } from "@/components/agents/agent-code"; import { IconSwap } from "@/components/agents/icon-swap"; import { ActionSwapRollText } from "@/components/motion/action-swap-roll"; import { Tooltip } from "@/components/motion/tooltip"; import { SPRING_PRESS } from "@/lib/ease"; import { useCopy } from "@/lib/hooks/use-copy"; import { useMessages } from "@/lib/i18n"; import { cn } from "@/lib/utils"; export const codeBlockMessages = { writing: "Writing", ready: "Ready", copy: "Copy code", copied: "Copied", }; export type CodeBlockMessages = typeof codeBlockMessages; export type CodeBlockStatus = "streaming" | "complete"; /** One identity for the default, so the highlighted-line Set stays memoized. */ const NO_LINES: number[] = []; export interface CodeBlockProps { /** The source code to show. May grow while `status` is "streaming". */ code: string; /** Language for syntax highlighting, also shown in the header. Default: "typescript" */ language?: AgentCodeLanguage; /** File name shown in the header. */ filename?: ReactNode; /** "streaming" shows Writing and follows new lines, "complete" shows Ready. Default: "complete" */ status?: CodeBlockStatus; /** Shows a line number gutter. Default: true */ showLineNumbers?: boolean; /** 1-based line numbers to tint. */ highlightLines?: number[]; /** Height in px before the code scrolls. Default: 280 */ maxHeight?: number; /** Wraps long lines instead of scrolling sideways. Default: false */ wrap?: boolean; /** Shows the copy button. Default: true */ copyable?: boolean; /** Runs in place of the clipboard write when copy is pressed. Shows the copy button. */ onCopy?: () => void | Promise; /** Classes merged onto the root element. */ className?: string; /** Override any built-in string. */ labels?: Partial; } export function CodeBlock({ code, language = "typescript", filename, status = "complete", showLineNumbers = true, highlightLines = NO_LINES, maxHeight = 280, wrap = false, copyable = true, onCopy, className, labels, }: CodeBlockProps) { const t = useMessages("codeBlock", codeBlockMessages, labels); const reduce = useReducedMotion() ?? false; const viewportRef = useRef(null); const { copied, copy: handleCopy } = useCopy({ text: code, onCopy }); const streaming = status === "streaming"; const tokens = useAgentCodeTokens(code, language); const highlighted = useMemo( () => new Set(highlightLines), [highlightLines], ); let offset = 0; const lines = code.split("\n").map((content) => { const line = { content, offset }; offset += content.length + 1; return line; }); useLayoutEffect(() => { const viewport = viewportRef.current; if (!viewport || !streaming) return; const frame = requestAnimationFrame(() => { if (viewport.scrollHeight <= viewport.clientHeight) return; if (typeof viewport.scrollTo === "function") { viewport.scrollTo({ top: viewport.scrollHeight, behavior: reduce ? "auto" : "smooth", }); } else { viewport.scrollTop = viewport.scrollHeight; } }); return () => cancelAnimationFrame(frame); }); return (
          
            {lines.map((line, index) => {
              const lineNumber = index + 1;
              return (
                
                  {showLineNumbers ? (
                    
                      {lineNumber}
                    
                  ) : null}
                  
                
              );
            })}
          
        
); }