feat(frontend): markdown and detail components

This commit is contained in:
2025-06-20 13:53:44 +01:00
parent 3b3a329e3d
commit 300129bd62
3 changed files with 404 additions and 9 deletions
+60 -9
View File
@@ -1,37 +1,88 @@
"use client";
import { useState } from "react";
import { useState, useCallback } from "react";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { oneDark } from "react-syntax-highlighter/dist/esm/styles/prism";
interface CodeBlockProps {
code: string;
language?: string;
label?: string;
}
export function CodeBlock({ code, language = "python" }: CodeBlockProps) {
export function CodeBlock({
code,
language = "python",
label,
}: CodeBlockProps) {
const [copied, setCopied] = useState(false);
const handleCopy = async () => {
const handleCopy = useCallback(async () => {
await navigator.clipboard.writeText(code);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
}, [code]);
const handleKeyDown = useCallback(
(e: React.KeyboardEvent) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
handleCopy();
}
},
[handleCopy]
);
const codeLabel = label || `${language} code example`;
// Map common language names to Prism language identifiers
const languageMap: Record<string, string> = {
python: "python",
javascript: "javascript",
typescript: "typescript",
java: "java",
cpp: "cpp",
c: "c",
go: "go",
rust: "rust",
};
const prismLanguage = languageMap[language.toLowerCase()] || language;
return (
<div className="relative group">
<div className="absolute right-2 top-2 opacity-0 group-hover:opacity-100 transition-opacity">
<div className="absolute right-2 top-2 z-10 opacity-0 group-hover:opacity-100 focus-within:opacity-100 transition-opacity">
<button
type="button"
onClick={handleCopy}
className="px-2 py-1 text-xs bg-[var(--secondary)] rounded hover:bg-[var(--muted)] transition-colors"
onKeyDown={handleKeyDown}
aria-label={copied ? "Code copied to clipboard" : "Copy code to clipboard"}
className="px-2 py-1 text-xs bg-[var(--muted)] rounded hover:bg-[var(--secondary)] focus:bg-[var(--secondary)] focus:outline-none focus:ring-2 focus:ring-[var(--primary)] transition-colors"
>
{copied ? "Copied!" : "Copy"}
</button>
<span role="status" aria-live="polite" className="sr-only">
{copied ? "Code copied to clipboard" : ""}
</span>
</div>
<div className="text-xs text-[var(--muted-foreground)] mb-1">
<div className="text-xs text-[var(--muted-foreground)] mb-1 capitalize">
{language}
</div>
<pre className="bg-[var(--secondary)] p-4 rounded-lg overflow-x-auto text-sm">
<code>{code}</code>
</pre>
<div aria-label={codeLabel} tabIndex={0} className="rounded-lg overflow-hidden">
<SyntaxHighlighter
language={prismLanguage}
style={oneDark}
customStyle={{
margin: 0,
padding: "1rem",
fontSize: "0.875rem",
borderRadius: "0.5rem",
}}
showLineNumbers={false}
>
{code.trim()}
</SyntaxHighlighter>
</div>
</div>
);
}
+62
View File
@@ -0,0 +1,62 @@
"use client";
import ReactMarkdown from "react-markdown";
import rehypeRaw from "rehype-raw";
interface MarkdownProps {
children: string;
className?: string;
}
export function Markdown({ children, className = "" }: MarkdownProps) {
return (
<div className={`prose prose-sm dark:prose-invert max-w-none ${className}`}>
<ReactMarkdown
rehypePlugins={[rehypeRaw]}
components={{
// Style inline code
code: ({ children, ...props }) => {
return (
<code
className="bg-[var(--secondary)] px-1.5 py-0.5 rounded text-sm font-mono"
{...props}
>
{children}
</code>
);
},
// Style code blocks
pre: ({ children }) => {
return (
<pre className="bg-[var(--secondary)] p-4 rounded-lg overflow-x-auto text-sm">
{children}
</pre>
);
},
// Style unordered lists
ul: ({ children }) => {
return (
<ul className="list-disc list-inside space-y-1">
{children}
</ul>
);
},
// Style ordered lists
ol: ({ children }) => {
return (
<ol className="list-decimal list-inside space-y-1">
{children}
</ol>
);
},
// Style paragraphs with spacing
p: ({ children }) => {
return <p className="mb-4 last:mb-0">{children}</p>;
},
}}
>
{children}
</ReactMarkdown>
</div>
);
}