import { useState } from "react"; import { createPortal } from "react-dom"; import { toast } from "react-hot-toast"; import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; import { oneDark } from "react-syntax-highlighter/dist/cjs/styles/prism"; import { useConnectionStore } from "@/store"; import Icon from "./Icon"; import ExecuteStatementModal from "./ExecuteStatementModal"; interface Props { language: string; value: string; } const checkStatementIsSelect = (statement: string) => { return statement.toUpperCase().trim().startsWith("SELECT"); }; export const CodeBlock = (props: Props) => { const { language, value } = props; const connectionStore = useConnectionStore(); const [showExecuteQueryModal, setShowExecuteQueryModal] = useState(false); const currentConnectionCtx = connectionStore.currentConnectionCtx; // Only show execute button in the following situations: // * SQL code, and it is a SELECT statement; // * Connection setup; const showExecuteButton = language.toUpperCase() === "SQL" && checkStatementIsSelect(value) && currentConnectionCtx?.connection && currentConnectionCtx?.database; const copyToClipboard = () => { if (!navigator.clipboard || !navigator.clipboard.writeText) { toast.error("Failed to copy to clipboard"); return; } navigator.clipboard.writeText(value).then(() => { toast.success("Copied to clipboard"); }); }; return ( <>
{language}
{showExecuteButton && ( )}
{value}
{showExecuteQueryModal && showExecuteButton && createPortal( setShowExecuteQueryModal(false)} />, document.body )} ); };