|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { |
| 4 | + CheckIcon, |
| 5 | + ChevronDownIcon, |
| 6 | + ChevronUpIcon, |
| 7 | + CopyIcon, |
| 8 | + ExternalLinkIcon, |
| 9 | +} from "lucide-react"; |
| 10 | +import { useState } from "react"; |
| 11 | +import type { BundledLanguage } from "shiki"; |
| 12 | +import { cn } from "../../lib/utils"; |
| 13 | +import { Button } from "../ui/button"; |
| 14 | +import { |
| 15 | + Tooltip, |
| 16 | + TooltipContent, |
| 17 | + TooltipProvider, |
| 18 | + TooltipTrigger, |
| 19 | +} from "../ui/tooltip"; |
| 20 | +import { ClickableFilePath } from "./clickable-file-path"; |
| 21 | +import { CodeBlock } from "./code-block"; |
| 22 | + |
| 23 | +/** Approximate line count threshold before the expand/collapse controls appear (~300px at ~20px/line). */ |
| 24 | +const DEFAULT_MAX_LINES = 15; |
| 25 | + |
| 26 | +export type ShowCodeProps = { |
| 27 | + code: string; |
| 28 | + /** Shiki language for syntax highlighting. Defaults to "text". */ |
| 29 | + language?: BundledLanguage; |
| 30 | + /** |
| 31 | + * When provided, displays a filename header instead of a plain language label. |
| 32 | + * The filename is made clickable if `onOpen` is also provided. |
| 33 | + */ |
| 34 | + filename?: string; |
| 35 | + /** Line range label shown after the filename, e.g. "1–142". */ |
| 36 | + lineRange?: string; |
| 37 | + /** Whether to render line numbers. Default: true. */ |
| 38 | + showLineNumbers?: boolean; |
| 39 | + /** |
| 40 | + * Starting line number for offset display (e.g. when showing a partial file). |
| 41 | + * Default: 1. |
| 42 | + */ |
| 43 | + startLine?: number; |
| 44 | + /** When false, renders all tokens in the foreground color (no syntax colors). Default: true. */ |
| 45 | + colorize?: boolean; |
| 46 | + /** |
| 47 | + * Number of lines before expand/collapse controls appear. |
| 48 | + * Default: 15 (roughly 300px). |
| 49 | + */ |
| 50 | + maxLines?: number; |
| 51 | + /** |
| 52 | + * When provided, shows an "Open" icon button in the header. |
| 53 | + * Only rendered when `filename` is also provided. |
| 54 | + */ |
| 55 | + onOpen?: () => void; |
| 56 | + className?: string; |
| 57 | +}; |
| 58 | + |
| 59 | +/** |
| 60 | + * Shared code display block used for both tool-call file views and |
| 61 | + * markdown code fences. Shows a header (language label or filename + line |
| 62 | + * range), action buttons (expand/collapse, open, copy), and syntax- |
| 63 | + * highlighted code with optional line numbers. |
| 64 | + * |
| 65 | + * When the code exceeds `maxLines`, a gradient overlay with a "Show more" |
| 66 | + * link floats at the bottom, and a chevron button appears in the header. |
| 67 | + */ |
| 68 | +export function ShowCode({ |
| 69 | + code, |
| 70 | + language = "text" as BundledLanguage, |
| 71 | + filename, |
| 72 | + lineRange, |
| 73 | + showLineNumbers = true, |
| 74 | + startLine, |
| 75 | + colorize = true, |
| 76 | + maxLines = DEFAULT_MAX_LINES, |
| 77 | + onOpen, |
| 78 | + className, |
| 79 | +}: ShowCodeProps) { |
| 80 | + const [isCopied, setIsCopied] = useState(false); |
| 81 | + const [isExpanded, setIsExpanded] = useState(false); |
| 82 | + |
| 83 | + const lineCount = code.split("\n").length; |
| 84 | + const isOverflowing = lineCount > maxLines; |
| 85 | + |
| 86 | + const handleCopy = async () => { |
| 87 | + try { |
| 88 | + await navigator.clipboard.writeText(code); |
| 89 | + setIsCopied(true); |
| 90 | + setTimeout(() => setIsCopied(false), 2000); |
| 91 | + } catch { |
| 92 | + // ignore — clipboard unavailable |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + return ( |
| 97 | + <div |
| 98 | + className={cn( |
| 99 | + "overflow-hidden rounded-md border border-border", |
| 100 | + className, |
| 101 | + )} |
| 102 | + > |
| 103 | + {/* Header */} |
| 104 | + <div className="flex items-center justify-between border-b border-border bg-muted/50 px-3 py-1.5"> |
| 105 | + {/* Left: language label or clickable filename + line range */} |
| 106 | + <div className="flex min-w-0 items-center gap-2 font-mono text-xs"> |
| 107 | + {filename ? ( |
| 108 | + <> |
| 109 | + <ClickableFilePath |
| 110 | + path={filename} |
| 111 | + onOpen={onOpen} |
| 112 | + className="text-foreground" |
| 113 | + /> |
| 114 | + {lineRange && ( |
| 115 | + <span className="shrink-0 text-muted-foreground"> |
| 116 | + {lineRange} |
| 117 | + </span> |
| 118 | + )} |
| 119 | + </> |
| 120 | + ) : ( |
| 121 | + <span className="text-muted-foreground">{language}</span> |
| 122 | + )} |
| 123 | + </div> |
| 124 | + {/* Right: action buttons */} |
| 125 | + <div className="ml-2 flex shrink-0 items-center gap-0.5"> |
| 126 | + {isOverflowing && ( |
| 127 | + <TooltipProvider> |
| 128 | + <Tooltip> |
| 129 | + <TooltipTrigger asChild> |
| 130 | + <Button |
| 131 | + className="h-6 w-6" |
| 132 | + onClick={() => setIsExpanded((prev) => !prev)} |
| 133 | + size="icon" |
| 134 | + variant="ghost" |
| 135 | + > |
| 136 | + {isExpanded ? ( |
| 137 | + <ChevronUpIcon className="h-3.5 w-3.5" /> |
| 138 | + ) : ( |
| 139 | + <ChevronDownIcon className="h-3.5 w-3.5" /> |
| 140 | + )} |
| 141 | + </Button> |
| 142 | + </TooltipTrigger> |
| 143 | + <TooltipContent> |
| 144 | + {isExpanded ? "Collapse" : "Expand"} |
| 145 | + </TooltipContent> |
| 146 | + </Tooltip> |
| 147 | + </TooltipProvider> |
| 148 | + )} |
| 149 | + {onOpen && filename && ( |
| 150 | + <TooltipProvider> |
| 151 | + <Tooltip> |
| 152 | + <TooltipTrigger asChild> |
| 153 | + <Button |
| 154 | + className="h-6 w-6" |
| 155 | + onClick={(e) => { |
| 156 | + e.stopPropagation(); |
| 157 | + onOpen(); |
| 158 | + }} |
| 159 | + size="icon" |
| 160 | + variant="ghost" |
| 161 | + > |
| 162 | + <ExternalLinkIcon className="h-3.5 w-3.5" /> |
| 163 | + </Button> |
| 164 | + </TooltipTrigger> |
| 165 | + <TooltipContent>Open</TooltipContent> |
| 166 | + </Tooltip> |
| 167 | + </TooltipProvider> |
| 168 | + )} |
| 169 | + <TooltipProvider> |
| 170 | + <Tooltip> |
| 171 | + <TooltipTrigger asChild> |
| 172 | + <Button |
| 173 | + className="h-6 w-6" |
| 174 | + onClick={handleCopy} |
| 175 | + size="icon" |
| 176 | + variant="ghost" |
| 177 | + > |
| 178 | + <div className="relative h-3.5 w-3.5"> |
| 179 | + <CopyIcon |
| 180 | + className={cn( |
| 181 | + "absolute inset-0 h-3.5 w-3.5 transition-[opacity,transform] duration-200 ease-out", |
| 182 | + isCopied |
| 183 | + ? "scale-50 opacity-0" |
| 184 | + : "scale-100 opacity-100", |
| 185 | + )} |
| 186 | + /> |
| 187 | + <CheckIcon |
| 188 | + className={cn( |
| 189 | + "absolute inset-0 h-3.5 w-3.5 transition-[opacity,transform] duration-200 ease-out", |
| 190 | + isCopied |
| 191 | + ? "scale-100 opacity-100" |
| 192 | + : "scale-50 opacity-0", |
| 193 | + )} |
| 194 | + /> |
| 195 | + </div> |
| 196 | + </Button> |
| 197 | + </TooltipTrigger> |
| 198 | + <TooltipContent>Copy</TooltipContent> |
| 199 | + </Tooltip> |
| 200 | + </TooltipProvider> |
| 201 | + </div> |
| 202 | + </div> |
| 203 | + |
| 204 | + {/* Code content */} |
| 205 | + <div className="relative"> |
| 206 | + <CodeBlock |
| 207 | + className={cn( |
| 208 | + "rounded-none border-0 [&_pre]:!p-2", |
| 209 | + !isExpanded && "[&>div>div]:max-h-[300px]", |
| 210 | + )} |
| 211 | + code={code} |
| 212 | + colorize={colorize} |
| 213 | + language={language} |
| 214 | + showLineNumbers={showLineNumbers} |
| 215 | + startLine={startLine} |
| 216 | + /> |
| 217 | + |
| 218 | + {/* Floating "Show more" overlay when truncated */} |
| 219 | + {isOverflowing && !isExpanded && ( |
| 220 | + <div className="pointer-events-none absolute inset-x-0 bottom-0 flex justify-center bg-gradient-to-t from-background pb-1.5 pt-8"> |
| 221 | + <button |
| 222 | + className="pointer-events-auto text-xs text-muted-foreground underline transition-colors hover:text-foreground" |
| 223 | + onClick={() => setIsExpanded(true)} |
| 224 | + type="button" |
| 225 | + > |
| 226 | + Show more |
| 227 | + </button> |
| 228 | + </div> |
| 229 | + )} |
| 230 | + |
| 231 | + {/* "Show less" link when fully expanded */} |
| 232 | + {isOverflowing && isExpanded && ( |
| 233 | + <div className="flex justify-center pb-1.5 pt-1"> |
| 234 | + <button |
| 235 | + className="text-xs text-muted-foreground underline transition-colors hover:text-foreground" |
| 236 | + onClick={() => setIsExpanded(false)} |
| 237 | + type="button" |
| 238 | + > |
| 239 | + Show less |
| 240 | + </button> |
| 241 | + </div> |
| 242 | + )} |
| 243 | + </div> |
| 244 | + </div> |
| 245 | + ); |
| 246 | +} |
0 commit comments