Spaces:
Sleeping
Sleeping
Download src/components/CodeBlock.tsx from ihatebaselines/purcar-chat-api: direct link, hf CLI and curl.
- Browser
- Download file 4.22 kB
-
https://huggingface.co/spaces/ihatebaselines/purcar-chat-api/resolve/main/src/components/CodeBlock.tsx
- Command line
-
hf download hf://spaces/ihatebaselines/purcar-chat-api/src/components/CodeBlock.tsx
-
curl -L -o CodeBlock.tsx https://huggingface.co/spaces/ihatebaselines/purcar-chat-api/resolve/main/src/components/CodeBlock.tsx
4.22 kB
| "use client"; | |
| import React, { useState } from "react"; | |
| import { Check, Copy, Terminal, Play } from "lucide-react"; | |
| interface CodeBlockProps { | |
| language: string; | |
| code: string; | |
| } | |
| export const CodeBlock: React.FC<CodeBlockProps> = ({ language, code }) => { | |
| const [copied, setCopied] = useState(false); | |
| const [output, setOutput] = useState<string | null>(null); | |
| const [isRunning, setIsRunning] = useState(false); | |
| const handleCopy = async () => { | |
| try { | |
| await navigator.clipboard.writeText(code); | |
| setCopied(true); | |
| setTimeout(() => setCopied(false), 2000); | |
| } catch { | |
| // ignore | |
| } | |
| }; | |
| const handleSimulateRun = () => { | |
| setIsRunning(true); | |
| setOutput(null); | |
| setTimeout(() => { | |
| setIsRunning(false); | |
| if (language === "python" || language === "py") { | |
| setOutput(">>> Process finished with exit code 0\n[Tensor shape: torch.Size([2, 64, 896]) | Latency: 4.2ms | Device: CUDA]"); | |
| } else { | |
| setOutput("✓ Compiled successfully in 12ms. Zero runtime warnings."); | |
| } | |
| }, 600); | |
| }; | |
| const lines = code.trim().split("\n"); | |
| return ( | |
| <div className="my-4 rounded-xl border border-white/[0.08] bg-[#070709] overflow-hidden shadow-2xl"> | |
| {/* Code Header Bar */} | |
| <div className="flex items-center justify-between px-3.5 py-2 bg-white/[0.03] border-b border-white/[0.06] text-xs font-mono"> | |
| <div className="flex items-center gap-2"> | |
| <div className="flex gap-1.5"> | |
| <div className="w-2.5 h-2.5 rounded-full bg-white/10" /> | |
| <div className="w-2.5 h-2.5 rounded-full bg-white/10" /> | |
| <div className="w-2.5 h-2.5 rounded-full bg-white/10" /> | |
| </div> | |
| <span className="text-zinc-400 uppercase text-[11px] font-semibold tracking-wider ml-1"> | |
| {language || "code"} | |
| </span> | |
| </div> | |
| <div className="flex items-center gap-2"> | |
| <button | |
| onClick={handleSimulateRun} | |
| disabled={isRunning} | |
| className="flex items-center gap-1.5 px-2.5 py-1 rounded-md bg-white/[0.05] hover:bg-white/[0.1] text-zinc-300 hover:text-white transition-all text-[11px]" | |
| title="Simulate Code Execution" | |
| > | |
| <Play className={`w-3 h-3 text-emerald-400 ${isRunning ? "animate-spin" : ""}`} /> | |
| <span>{isRunning ? "Running..." : "Run"}</span> | |
| </button> | |
| <button | |
| onClick={handleCopy} | |
| className="flex items-center gap-1.5 px-2.5 py-1 rounded-md bg-white/[0.05] hover:bg-white/[0.1] text-zinc-300 hover:text-white transition-all text-[11px]" | |
| title="Copy to clipboard" | |
| > | |
| {copied ? ( | |
| <> | |
| <Check className="w-3 h-3 text-emerald-400" /> | |
| <span className="text-emerald-400 font-medium">Copied</span> | |
| </> | |
| ) : ( | |
| <> | |
| <Copy className="w-3 h-3 text-zinc-400" /> | |
| <span>Copy</span> | |
| </> | |
| )} | |
| </button> | |
| </div> | |
| </div> | |
| {/* Code Content */} | |
| <div className="p-4 overflow-x-auto text-xs font-mono leading-relaxed bg-black/70"> | |
| <table className="w-full border-collapse"> | |
| <tbody> | |
| {lines.map((line, idx) => ( | |
| <tr key={idx} className="hover:bg-white/[0.02]"> | |
| <td className="w-8 select-none text-right pr-4 text-zinc-600 font-mono text-[11px]"> | |
| {idx + 1} | |
| </td> | |
| <td className="text-zinc-200 whitespace-pre">{line}</td> | |
| </tr> | |
| ))} | |
| </tbody> | |
| </table> | |
| </div> | |
| {/* Simulated Execution Output Drawer */} | |
| {output && ( | |
| <div className="border-t border-white/[0.08] bg-black/90 p-3 text-xs font-mono"> | |
| <div className="flex items-center gap-1.5 text-zinc-500 text-[10px] uppercase tracking-wider mb-1"> | |
| <Terminal className="w-3 h-3 text-emerald-400" /> | |
| <span>Output Console</span> | |
| </div> | |
| <pre className="text-emerald-400/90 whitespace-pre-wrap leading-tight">{output}</pre> | |
| </div> | |
| )} | |
| </div> | |
| ); | |
| }; | |