'use client'; import React from 'react'; import { useState } from 'react'; import { FaFile, FaFileCode, FaFilePdf, FaFileExcel } from 'react-icons/fa'; import { FaReact } from 'react-icons/fa'; import { FaCss3 } from 'react-icons/fa'; import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; import { Inter } from 'next/font/google'; import { nightOwl } from 'react-syntax-highlighter/dist/esm/styles/prism'; // try out this one or use the custom theme provided in example code import { motion } from 'framer-motion'; const inter = Inter({ subsets: ["latin"], weight: "500" }); interface FileBlock { title: string; code: string; } interface CodeBlockProps { codefile: FileBlock[]; defaultfile?: string; } const fileIcons: { [key: string]: JSX.Element } = { '.js': , '.tsx': , '.html': , '.css': , '.pdf': , '.xls': , '.xlsx': , 'default': }; const CodeBlock: React.FC = ({ codefile, defaultfile }) => { const [activeTitle, setActiveTitle] = useState(defaultfile || (codefile[0]?.title || '')); const [copied, setCopied] = useState(false); const activeFile = codefile.find(codefile => codefile.title === activeTitle); const code = activeFile?.code || ''; const copyToClipboard = (text: string) => { if (navigator.clipboard) { navigator.clipboard.writeText(text) .then(() => { setCopied(true); setTimeout(() => setCopied(false), 2000); }) .catch(err => { console.error('Failed to copy: ', err); }); } else { const textArea = document.createElement('textarea'); textArea.value = text; document.body.appendChild(textArea); textArea.select(); try { document.execCommand('copy'); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch (err) { console.error('Failed to copy: ', err); } document.body.removeChild(textArea); } }; const getFileIcon = (fileName: string) => { const ext = fileName.split('.').pop(); return fileIcons[`.${ext}`] || fileIcons['default']; }; return ( {codefile.map(({ title }) => ( setActiveTitle(title)} className={`flex border-t items-center px-3 py-1 rounded-full text-sm ${title === activeTitle ? 'bg-gradient-to-b from-zinc-800 via-zinc-900 to-black text-white border-zinc-600' : 'bg-zinc-950 text-gray-400 border-zinc-800'} transition-colors duration-300`} > {getFileIcon(title)} {title} ))} copyToClipboard(code)} className="text-gray-400 hover:text-white rounded-md transition relative sm:-top-1 sm:-left-2" > {copied ? ( ) : ( )} {code} ); }; export default CodeBlock;