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, useQueryStore } from "@/store"; import { checkStatementIsSelect } from "@/utils"; import Icon from "./Icon"; interface Props { language: string; value: string; } export const CodeBlock = (props: Props) => { const { language, value } = props; const connectionStore = useConnectionStore(); const queryStore = useQueryStore(); 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"); }); }; const handleExecuteQuery = () => { if (!currentConnectionCtx) { toast.error("Please select a connection first"); return; } queryStore.setContext({ connection: currentConnectionCtx.connection, database: currentConnectionCtx.database, statement: value, }); queryStore.toggleDrawer(true); }; return (
{language}
{showExecuteButton && ( )}
{value}
); };