chat-indic-ui / src /components /code-display-block.tsx
sam2ai's picture
Synced repo using 'sync_with_huggingface' Github Action
0971cc4 verified
"use client";
import React from "react";
import { CheckIcon, CopyIcon } from "@radix-ui/react-icons";
import { useTheme } from "next-themes";
import { CodeBlock, dracula, github } from "react-code-blocks";
import { toast } from "sonner";
import { Button } from "./ui/button";
interface ButtonCodeblockProps {
code: string;
lang: string;
}
export default function CodeDisplayBlock({ code, lang }: ButtonCodeblockProps) {
const [isCopied, setisCopied] = React.useState(false);
const { theme } = useTheme();
const copyToClipboard = () => {
navigator.clipboard.writeText(code);
setisCopied(true);
toast.success("Code copied to clipboard!");
setTimeout(() => {
setisCopied(false);
}, 1500);
};
return (
<div className="relative my-4 overflow-scroll overflow-x-scroll flex flex-col text-start ">
<CodeBlock
customStyle={
theme === "dark"
? { background: "#303033" }
: { background: "#fcfcfc" }
}
text={code}
language="tsx"
showLineNumbers={false}
theme={theme === "dark" ? dracula : github}
/>
<Button
onClick={copyToClipboard}
variant="ghost"
size="iconSm"
className="h-5 w-5 absolute top-2 right-2"
>
{isCopied ? (
<CheckIcon className="w-4 h-4" />
) : (
<CopyIcon className="w-4 h-4" />
)}
</Button>
</div>
);
}