chore: update code block component

This commit is contained in:
steven
2023-04-14 14:09:32 +08:00
parent 790e418eec
commit 80e4d7c353
5 changed files with 26 additions and 19 deletions

View File

@ -2,7 +2,7 @@
"name": "sqlchat", "name": "sqlchat",
"private": false, "private": false,
"scripts": { "scripts": {
"dev": "next dev", "dev": "next dev --turbo",
"build": "next build", "build": "next build",
"export": "next export", "export": "next export",
"start": "next start", "start": "next start",

View File

@ -1,9 +1,10 @@
import { toast } from "react-hot-toast"; import { toast } from "react-hot-toast";
import { useTranslation } from "react-i18next";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { oneDark } from "react-syntax-highlighter/dist/cjs/styles/prism"; import { oneDark } from "react-syntax-highlighter/dist/cjs/styles/prism";
import { useConnectionStore, useQueryStore } from "@/store"; import { useConnectionStore, useQueryStore } from "@/store";
import { checkStatementIsSelect } from "@/utils";
import Icon from "./Icon"; import Icon from "./Icon";
import Tooltip from "./kit/Tooltip";
interface Props { interface Props {
language: string; language: string;
@ -12,14 +13,14 @@ interface Props {
export const CodeBlock = (props: Props) => { export const CodeBlock = (props: Props) => {
const { language, value } = props; const { language, value } = props;
const { t } = useTranslation();
const connectionStore = useConnectionStore(); const connectionStore = useConnectionStore();
const queryStore = useQueryStore(); const queryStore = useQueryStore();
const currentConnectionCtx = connectionStore.currentConnectionCtx; const currentConnectionCtx = connectionStore.currentConnectionCtx;
// Only show execute button in the following situations: // Only show execute button in the following situations:
// * SQL code, and it is a SELECT statement; // * SQL code;
// * Connection setup; // * Connection setup;
const showExecuteButton = const showExecuteButton = currentConnectionCtx?.connection && currentConnectionCtx?.database && language.toUpperCase() === "SQL";
language.toUpperCase() === "SQL" && checkStatementIsSelect(value) && currentConnectionCtx?.connection && currentConnectionCtx?.database;
const copyToClipboard = () => { const copyToClipboard = () => {
if (!navigator.clipboard || !navigator.clipboard.writeText) { if (!navigator.clipboard || !navigator.clipboard.writeText) {
@ -50,19 +51,23 @@ export const CodeBlock = (props: Props) => {
<div className="flex items-center justify-between py-2 px-4"> <div className="flex items-center justify-between py-2 px-4">
<span className="text-xs text-white font-mono">{language}</span> <span className="text-xs text-white font-mono">{language}</span>
<div className="flex items-center space-x-2"> <div className="flex items-center space-x-2">
<button <Tooltip title={t("common.copy")} side="top">
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>
{showExecuteButton && (
<button <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" 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={handleExecuteQuery} onClick={copyToClipboard}
> >
<Icon.IoPlay className="w-full h-auto" /> <Icon.BiClipboard className="w-full h-auto" />
</button> </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-gray-500 opacity-70 hover:opacity-100"
onClick={handleExecuteQuery}
>
<Icon.IoPlay className="w-full h-auto" />
</button>
</Tooltip>
)} )}
</div> </div>
</div> </div>

View File

@ -8,7 +8,8 @@
"loading": "Loading", "loading": "Loading",
"setting": "Setting", "setting": "Setting",
"copy": "Copy", "copy": "Copy",
"delete": "Delete" "delete": "Delete",
"execute": "Execute"
}, },
"conversation": { "conversation": {
"new-chat": "New Chat", "new-chat": "New Chat",
@ -21,7 +22,7 @@
"select-database": "Select your database" "select-database": "Select your database"
}, },
"execution": { "execution": {
"title": "Execute query", "title": "Execute SQL",
"message": { "message": {
"executing": "Executing query...", "executing": "Executing query...",
"no-connection": "No connection selected", "no-connection": "No connection selected",

View File

@ -8,7 +8,8 @@
"loading": "加载中", "loading": "加载中",
"setting": "设置", "setting": "设置",
"copy": "复制", "copy": "复制",
"delete": "删除" "delete": "删除",
"execute": "执行"
}, },
"conversation": { "conversation": {
"new-chat": "新建会话", "new-chat": "新建会话",
@ -21,7 +22,7 @@
"select-database": "选择数据库" "select-database": "选择数据库"
}, },
"execution": { "execution": {
"title": "执行查询", "title": "执行 SQL",
"message": { "message": {
"executing": "正在执行查询...", "executing": "正在执行查询...",
"no-connection": "未选择连接", "no-connection": "未选择连接",

View File

@ -15,7 +15,7 @@
} }
.btn { .btn {
@apply hide-scrollbar text-base px-3 py-2 rounded-lg border border-none outline-none bg-zinc-600 text-gray-200 hover:opacity-80 disabled:!opacity-60 disabled:cursor-not-allowed; @apply flex flex-row justify-center items-center text-base px-3 py-2 rounded-lg border border-none outline-none bg-zinc-600 text-gray-200 hover:opacity-80 disabled:!opacity-60 disabled:cursor-not-allowed;
} }
.btn-outline { .btn-outline {