🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
refactor(web): Cleanup `CodeBlock` component (#15381)
Ben Bachemc5d9f38fb822
diff --git a/web/.storybook/main.ts b/web/.storybook/main.tsindex 252542adee..94af2a7b44 100644--- a/web/.storybook/main.ts+++ b/web/.storybook/main.ts@@ -10,6 +10,7 @@ import {const STORY_EXTENSIONS = "@(js|jsx|mjs|ts|tsx)";const DESIGN_COMPONENT_STORIES = [+ "Codeblock/Codeblock","LangfuseIcon/LangfuseIcon","Progress/Progress","Spinner/Spinner",diff --git a/web/src/components/design-system/Codeblock/Codeblock.stories.tsx b/web/src/components/design-system/Codeblock/Codeblock.stories.tsxnew file mode 100644index 0000000000..650f042892--- /dev/null+++ b/web/src/components/design-system/Codeblock/Codeblock.stories.tsx@@ -0,0 +1,23 @@+import preview from "../../../../.storybook/preview";+import { CodeBlock } from "./Codeblock";++const meta = preview.meta({+ component: CodeBlock,+});++export const Default = meta.story({+ args: {+ language: "typescript",+ value: 'const greeting = "Hello, Langfuse!";',+ theme: "light",+ },+});++export const CardHeader = meta.story({+ args: {+ language: "typescript",+ value: 'const greeting = "Hello, Langfuse!";',+ theme: "light",+ variant: "card",+ },+});diff --git a/web/src/components/design-system/Codeblock/Codeblock.tsx b/web/src/components/design-system/Codeblock/Codeblock.tsxnew file mode 100644index 0000000000..36dd7b605d--- /dev/null+++ b/web/src/components/design-system/Codeblock/Codeblock.tsx@@ -0,0 +1,93 @@+import { cva, type VariantProps } from "class-variance-authority";+import { Button } from "@/src/components/ui/button";+import { copyTextToClipboard } from "@/src/utils/clipboard";+import { Check, Copy } from "lucide-react";+import { type FC, memo, useState } from "react";+import { Highlight, themes } from "prism-react-renderer";+import { useTheme } from "next-themes";++const headerVariants = cva("flex w-full items-center justify-between px-2", {+ variants: {+ variant: {+ default: "bg-secondary",+ card: "bg-card",+ },+ },+ defaultVariants: {+ variant: "default",+ },+});++interface Props extends VariantProps<typeof headerVariants> {+ language: string;+ value: string;+ theme?: "light" | "dark";+}++const CodeBlock: FC<Props> = memo(+ ({ language, value, theme, variant = "default" }) => {+ const [isCopied, setIsCopied] = useState(false);+ const { resolvedTheme } = useTheme();+ const appliedTheme = theme ?? resolvedTheme;+ const handleCopy = () => {+ setIsCopied(true);+ copyTextToClipboard(value ?? "");+ setTimeout(() => setIsCopied(false), 1000);+ };++ return (+ <div className="codeblock dark:bg-surface-code relative w-full overflow-hidden rounded border font-sans">+ <div className={headerVariants({ variant })}>+ <span className="text-xs lowercase">{language}</span>+ <div className="flex items-center py-1">+ <Button+ variant="ghost"+ size="xs"+ className="hover:bg-border text-xs focus-visible:ring-1 focus-visible:ring-offset-0"+ onClick={handleCopy}+ >+ {isCopied ? (+ <Check className="h-3 w-3" />+ ) : (+ <Copy className="h-3 w-3" />+ )}+ <span className="sr-only">Copy code</span>+ </Button>+ </div>+ </div>+ <Highlight+ theme={appliedTheme === "dark" ? themes.vsDark : themes.github}+ code={value}+ language={language}+ >+ {({ className, style, tokens, getLineProps, getTokenProps }) => (+ <pre+ className={className}+ style={{+ ...style,+ margin: 0,+ width: "100%",+ background: "transparent",+ padding: "0.5rem",+ fontSize: "0.75rem",+ fontFamily: "var(--font-mono)",+ overflow: "auto",+ }}+ >+ {tokens.map((line, i) => (+ <div key={i} {...getLineProps({ line })}>+ {line.map((token, key) => (+ <span key={key} {...getTokenProps({ token })} />+ ))}+ </div>+ ))}+ </pre>+ )}+ </Highlight>+ </div>+ );+ },+);+CodeBlock.displayName = "CodeBlock";++export { CodeBlock };diff --git a/web/src/components/trace/components/IOPreview/components/ChatMessage.tsx b/web/src/components/trace/components/IOPreview/components/ChatMessage.tsxindex decf805580..9e7ecdc2ba 100644--- a/web/src/components/trace/components/IOPreview/components/ChatMessage.tsx+++ b/web/src/components/trace/components/IOPreview/components/ChatMessage.tsx@@ -1,5 +1,4 @@import { useState } from "react";-import { cn } from "@/src/utils/tailwind";import { Button } from "@/src/components/ui/button";import { PrettyJsonView } from "@/src/components/ui/PrettyJsonView";import {@@ -92,7 +91,7 @@ export function ChatMessage({<MarkdownJsonViewtitle="Placeholder"content={message.name || "Unnamed placeholder"}- customCodeHeaderClassName="bg-card"+ customCodeHeaderVariant="card"/></div><div style={{ display: shouldRenderMarkdown ? "none" : "block" }}>@@ -183,6 +182,11 @@ export function ChatMessage({</>);+ let customCodeHeaderVariant: "card" | undefined;+ if (message.role === "system") {+ customCodeHeaderVariant = "card";+ }+return (<div className="hover:bg-muted transition-colors">{/* Markdown view */}@@ -190,10 +194,7 @@ export function ChatMessage({<MarkdownJsonViewtitle={title}content={message.content || ""}- customCodeHeaderClassName={cn(- message.role === "assistant" && "bg-secondary",- message.role === "system" && "bg-card",- )}+ customCodeHeaderVariant={customCodeHeaderVariant}audio={message.audio}controlButtons={passthroughToggleButton}afterHeader={thinkingBlocks}diff --git a/web/src/components/ui/Codeblock.tsx b/web/src/components/ui/Codeblock.tsxdeleted file mode 100644index 5f3d72cb68..0000000000--- a/web/src/components/ui/Codeblock.tsx+++ /dev/null@@ -1,116 +0,0 @@-import { Button } from "@/src/components/ui/button";-import { copyTextToClipboard } from "@/src/utils/clipboard";-import { cn } from "@/src/utils/tailwind";-import { Check, Copy } from "lucide-react";-import { type FC, memo, useState } from "react";-import { Highlight, themes } from "prism-react-renderer";-import { useTheme } from "next-themes";--interface Props {- language: string;- value: string;- theme?: string;- className?: string;-}--interface languageMap {- [key: string]: string;-}--export const programmingLanguages: languageMap = {- javascript: ".js",- python: ".py",- java: ".java",- c: ".c",- cpp: ".cpp",- "c++": ".cpp",- "c#": ".cs",- ruby: ".rb",- php: ".php",- swift: ".swift",- "objective-c": ".m",- kotlin: ".kt",- typescript: ".ts",- go: ".go",- perl: ".pl",- rust: ".rs",- scala: ".scala",- haskell: ".hs",- lua: ".lua",- shell: ".sh",- sql: ".sql",- html: ".html",- css: ".css",- // add more file extensions here, make sure the key is same as language prop in CodeBlock.tsx component-};--const CodeBlock: FC<Props> = memo(({ language, value, theme, className }) => {- const [isCopied, setIsCopied] = useState(false);- const { resolvedTheme } = useTheme();- const appliedTheme = theme ?? resolvedTheme;- const handleCopy = () => {- setIsCopied(true);- copyTextToClipboard(value ?? "");- setTimeout(() => setIsCopied(false), 1000);- };-- return (- <div className="codeblock dark:bg-surface-code relative w-full overflow-hidden rounded border font-sans">- <div- className={cn(- "bg-secondary flex w-full items-center justify-between px-2",- className,- )}- >- <span className="text-xs lowercase">{language}</span>- <div className="flex items-center py-1">- <Button- variant="ghost"- size="xs"- className="hover:bg-border text-xs focus-visible:ring-1 focus-visible:ring-offset-0"- onClick={handleCopy}- >- {isCopied ? (- <Check className="h-3 w-3" />- ) : (- <Copy className="h-3 w-3" />- )}- <span className="sr-only">Copy code</span>- </Button>- </div>- </div>- <Highlight- theme={appliedTheme === "dark" ? themes.vsDark : themes.github}- code={value}- language={language}- >- {({ className, style, tokens, getLineProps, getTokenProps }) => (- <pre- className={className}- style={{- ...style,- margin: 0,- width: "100%",- background: "transparent",- padding: "0.5rem",- fontSize: "0.75rem",- fontFamily: "var(--font-mono)",- overflow: "auto",- }}- >- {tokens.map((line, i) => (- <div key={i} {...getLineProps({ line })}>- {line.map((token, key) => (- <span key={key} {...getTokenProps({ token })} />- ))}- </div>- ))}- </pre>- )}- </Highlight>- </div>- );-});-CodeBlock.displayName = "CodeBlock";--export { CodeBlock };diff --git a/web/src/components/ui/MarkdownJsonView.tsx b/web/src/components/ui/MarkdownJsonView.tsxindex cef8ef2e92..5458a2450e 100644--- a/web/src/components/ui/MarkdownJsonView.tsx+++ b/web/src/components/ui/MarkdownJsonView.tsx@@ -83,7 +83,7 @@ export function MarkdownJsonView({title,titleIcon,className,- customCodeHeaderClassName,+ customCodeHeaderVariant,audio,media,controlButtons,@@ -94,7 +94,7 @@ export function MarkdownJsonView({title?: string;titleIcon?: React.ReactNode;className?: string;- customCodeHeaderClassName?: string;+ customCodeHeaderVariant?: "card";audio?: OpenAIOutputAudioType;media?: MediaReturnType[];controlButtons?: React.ReactNode;@@ -123,7 +123,7 @@ export function MarkdownJsonView({}title={title}titleIcon={titleIcon}- customCodeHeaderClassName={customCodeHeaderClassName}+ customCodeHeaderVariant={customCodeHeaderVariant}audio={audio}media={media}controlButtons={controlButtons}diff --git a/web/src/components/ui/MarkdownViewer.tsx b/web/src/components/ui/MarkdownViewer.tsxindex 2c67223897..ae10c0d1cc 100644--- a/web/src/components/ui/MarkdownViewer.tsx+++ b/web/src/components/ui/MarkdownViewer.tsx@@ -11,7 +11,7 @@ import {} from "react";import ReactMarkdown, { type Options } from "react-markdown";import remarkGfm from "remark-gfm";-import { CodeBlock } from "@/src/components/ui/Codeblock";+import { CodeBlock } from "@/src/components/design-system/Codeblock/Codeblock";import { useTheme } from "next-themes";import { ImageOff, Info } from "lucide-react";import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";@@ -221,12 +221,12 @@ function MarkdownRenderer({markdown,theme,className,- customCodeHeaderClassName,+ customCodeHeaderVariant,}: {markdown: string;theme?: string;className?: string;- customCodeHeaderClassName?: string;+ customCodeHeaderVariant?: "card";}) {const promptReferenceProjectId = usePromptReferenceProjectId();@@ -382,8 +382,8 @@ function MarkdownRenderer({key={Math.random()}language={language}value={codeContent}- theme={theme}- className={customCodeHeaderClassName}+ theme={theme === "dark" ? "dark" : "light"}+ variant={customCodeHeaderVariant}/>) : (// inline code@@ -479,7 +479,7 @@ export function MarkdownView({markdown,title,titleIcon,- customCodeHeaderClassName,+ customCodeHeaderVariant,audio,media,className,@@ -490,7 +490,7 @@ export function MarkdownView({markdown: string | z.infer<typeof OpenAIContentSchema>;title?: string;titleIcon?: React.ReactNode;- customCodeHeaderClassName?: string;+ customCodeHeaderVariant?: "card";audio?: OpenAIOutputAudioType;media?: MediaReturnType[];className?: string;@@ -601,7 +601,7 @@ export function MarkdownView({<MarkdownRenderermarkdown={isCollapsed ? truncatedContent : markdown}theme={theme}- customCodeHeaderClassName={customCodeHeaderClassName}+ customCodeHeaderVariant={customCodeHeaderVariant}/>{collapseToggle}</>@@ -613,7 +613,7 @@ export function MarkdownView({<MarkdownRenderermarkdown={truncatedContent}theme={theme}- customCodeHeaderClassName={customCodeHeaderClassName}+ customCodeHeaderVariant={customCodeHeaderVariant}/>) : ((markdown ?? []).map((content, index) => {@@ -623,7 +623,7 @@ export function MarkdownView({key={index}markdown={content.text}theme={theme}- customCodeHeaderClassName={customCodeHeaderClassName}+ customCodeHeaderVariant={customCodeHeaderVariant}/>);}@@ -672,7 +672,7 @@ export function MarkdownView({<MarkdownRenderermarkdown={audio.transcript ? "[Audio] \n" + audio.transcript : ""}theme={theme}- customCodeHeaderClassName={customCodeHeaderClassName}+ customCodeHeaderVariant={customCodeHeaderVariant}/><LangfuseMediaViewmediaReferenceString={audio.data.referenceString}diff --git a/web/src/features/developer-tools/components/DeveloperToolsSettings.tsx b/web/src/features/developer-tools/components/DeveloperToolsSettings.tsxindex 11475ffeaf..61d5ed30cb 100644--- a/web/src/features/developer-tools/components/DeveloperToolsSettings.tsx+++ b/web/src/features/developer-tools/components/DeveloperToolsSettings.tsx@@ -1,7 +1,7 @@import Header from "@/src/components/layouts/header";import { Button } from "@/src/components/ui/button";import { Card } from "@/src/components/ui/card";-import { CodeBlock } from "@/src/components/ui/Codeblock";+import { CodeBlock } from "@/src/components/design-system/Codeblock/Codeblock";import Link from "next/link";import { Bot, SquareTerminal, Sparkles } from "lucide-react";