MCPcopy Create free account
hub / github.com/sqlchat/sqlchat / CodeBlock

Function CodeBlock

src/components/CodeBlock.tsx:18–82  ·  view source on GitHub ↗
(props: Props)

Source from the content-addressed store, hash-verified

16}
17
18export const CodeBlock = (props: Props) => {
19 const { language, value, messageId, wrapLongLines } = props;
20 const { t } = useTranslation();
21 const connectionStore = useConnectionStore();
22 const queryStore = useQueryStore();
23 const currentConnectionCtx = connectionStore.currentConnectionCtx;
24 // Only show execute button in the following situations:
25 // * SQL code;
26 // * Connection setup;
27 const showExecuteButton = currentConnectionCtx?.connection && currentConnectionCtx?.database && language.toUpperCase() === "SQL";
28
29 const copyToClipboard = () => {
30 copy(value);
31 toast.success("Copied to clipboard");
32 };
33
34 const handleExecuteQuery = () => {
35 if (!currentConnectionCtx) {
36 toast.error("Please select a connection first");
37 return;
38 }
39
40 queryStore.setContext({
41 connection: currentConnectionCtx.connection,
42 database: currentConnectionCtx.database,
43 messageId: messageId,
44 statement: value,
45 });
46 queryStore.toggleDrawer(true);
47 };
48
49 return (
50 <div className="w-full max-w-full relative font-sans text-[16px]">
51 <div className="flex items-center justify-between py-2 px-4">
52 <span className="text-xs text-black dark:text-gray-300 font-mono">{language}</span>
53 <div className="flex items-center space-x-2">
54 <Tooltip title={t("common.copy")} side="top">
55 <button
56 className="flex justify-center items-center rounded bg-none w-6 h-6 p-1 text-xs text-white bg-gray-500 opacity-70 hover:opacity-100"
57 onClick={copyToClipboard}
58 >
59 <Icon.BiClipboard className="w-full h-auto" />
60 </button>
61 </Tooltip>
62 {showExecuteButton && (
63 <button
64 className="flex justify-center items-center rounded bg-none h-6 py-1 px-2 text-xs text-white bg-indigo-600 opacity-90 hover:opacity-100"
65 onClick={handleExecuteQuery}
66 >
67 {t("common.run-sql")}
68 </button>
69 )}
70 </div>
71 </div>
72 <SyntaxHighlighter
73 language={language.toLowerCase()}
74 wrapLongLines={wrapLongLines || false}
75 style={oneDark}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected