forked from sqlchat/sqlchat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeBlock.tsx
More file actions
82 lines (76 loc) · 2.68 KB
/
CodeBlock.tsx
File metadata and controls
82 lines (76 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import copy from "copy-to-clipboard";
import { toast } from "react-hot-toast";
import { useTranslation } from "react-i18next";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { oneDark } from "react-syntax-highlighter/dist/cjs/styles/prism";
import { useConnectionStore, useQueryStore } from "@/store";
import Icon from "./Icon";
import Tooltip from "./kit/Tooltip";
interface Props {
language: string;
value: string;
}
export const CodeBlock = (props: Props) => {
const { language, value } = props;
const { t } = useTranslation();
const connectionStore = useConnectionStore();
const queryStore = useQueryStore();
const currentConnectionCtx = connectionStore.currentConnectionCtx;
// Only show execute button in the following situations:
// * SQL code;
// * Connection setup;
const showExecuteButton =
currentConnectionCtx?.connection &&
currentConnectionCtx?.database &&
language.toUpperCase() === "SQL";
const copyToClipboard = () => {
copy(value);
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 (
<div className="w-full max-w-full relative font-sans text-[16px]">
<div className="flex items-center justify-between py-2 px-4">
<span className="text-xs text-white font-mono">{language}</span>
<div className="flex items-center space-x-2">
<Tooltip title={t("common.copy")} side="top">
<button
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"
onClick={copyToClipboard}
>
<Icon.BiClipboard className="w-full h-auto" />
</button>
</Tooltip>
{showExecuteButton && (
<Tooltip title={t("common.execute")} side="top">
<button
className="flex justify-center items-center rounded bg-none w-6 h-6 p-1 text-xs text-white bg-indigo-600 opacity-90 hover:opacity-100"
onClick={handleExecuteQuery}
>
<Icon.IoPlay className="w-full h-auto" />
</button>
</Tooltip>
)}
</div>
</div>
<SyntaxHighlighter
language={language.toLowerCase()}
style={oneDark}
customStyle={{ margin: 0 }}
>
{value}
</SyntaxHighlighter>
</div>
);
};