Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: copy original content instead of formatted content for CopyLink #802

Merged
merged 5 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions ui/lib/apps/SlowQuery/pages/Detail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useLocalStorageState } from '@umijs/hooks'
import client from '@lib/client'
import { useClientRequest } from '@lib/utils/useClientRequest'
import { buildQueryFn, parseQueryFn } from '@lib/utils/query'
import formatSql from '@lib/utils/formatSql'
import {
AnimatedSkeleton,
CardTabs,
Expand Down Expand Up @@ -90,7 +91,14 @@ function DetailPage() {
expanded={detailExpand.query}
onClick={toggleQuery}
/>
<CopyLink data={data.query!} />
<CopyLink
textTransKey="copyFormatted"
data={formatSql(data.query!)}
/>
<CopyLink
textTransKey="copyOriginal"
data={data.query!}
/>
</Space>
}
>
Expand All @@ -116,7 +124,14 @@ function DetailPage() {
expanded={detailExpand.prev_query}
onClick={togglePrevQuery}
/>
<CopyLink data={data.prev_stmt!} />
<CopyLink
textTransKey="copyFormatted"
baurine marked this conversation as resolved.
Show resolved Hide resolved
data={formatSql(data.prev_stmt!)}
/>
<CopyLink
textTransKey="copyOriginal"
data={data.prev_stmt!}
/>
</Space>
}
>
Expand Down
19 changes: 17 additions & 2 deletions ui/lib/apps/Statement/pages/Detail/PlanDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '@lib/components'
import { useClientRequest } from '@lib/utils/useClientRequest'
import client from '@lib/client'
import formatSql from '@lib/utils/formatSql'

import { IPageQuery } from '.'
import TabBasic from './PlanDetailTabBasic'
Expand Down Expand Up @@ -95,7 +96,14 @@ function PlanDetail({ query }: IPlanDetailProps) {
expanded={detailExpand.query}
onClick={toggleQuery}
/>
<CopyLink data={data.query_sample_text} />
<CopyLink
textTransKey="copyFormatted"
data={formatSql(data.query_sample_text)}
/>
<CopyLink
textTransKey="copyOriginal"
data={data.query_sample_text}
/>
</Space>
}
>
Expand All @@ -119,7 +127,14 @@ function PlanDetail({ query }: IPlanDetailProps) {
expanded={detailExpand.prev_query}
onClick={togglePrevQuery}
/>
<CopyLink data={data.prev_sample_text} />
<CopyLink
textTransKey="copyFormatted"
data={formatSql(data.prev_sample_text)}
/>
<CopyLink
textTransKey="copyOriginal"
data={data.prev_sample_text}
/>
</Space>
}
>
Expand Down
10 changes: 9 additions & 1 deletion ui/lib/apps/Statement/pages/Detail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
TextWithInfo,
} from '@lib/components'
import CopyLink from '@lib/components/CopyLink'
import formatSql from '@lib/utils/formatSql'
import { buildQueryFn, parseQueryFn } from '@lib/utils/query'
import { useClientRequest } from '@lib/utils/useClientRequest'

Expand Down Expand Up @@ -98,7 +99,14 @@ function DetailPage() {
expanded={sqlExpanded}
onClick={toggleSqlExpanded}
/>
<CopyLink data={plans[0].digest_text!} />
<CopyLink
textTransKey="copyFormatted"
data={formatSql(plans[0].digest_text!)}
/>
<CopyLink
textTransKey="copyOriginal"
data={plans[0].digest_text!}
/>
</Space>
}
>
Expand Down
33 changes: 23 additions & 10 deletions ui/lib/components/CopyLink/index.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import React, { useState } from 'react'
import { CopyToClipboard } from 'react-copy-to-clipboard'
import { addTranslationResource } from '@lib/utils/i18n'
import { useTranslation } from 'react-i18next'
import { useTimeoutFn } from 'react-use'
import { Tooltip } from 'antd'
import { CheckOutlined, CopyOutlined } from '@ant-design/icons'
import { addTranslationResource } from '@lib/utils/i18n'

import styles from './index.module.less'

export interface ICopyLinkProps {
data?: string
textTransKey?: 'copy' | 'copyOriginal' | 'copyFormatted'
}

const translations = {
en: {
text: 'Copy',
copy: 'Copy',
copyOriginal: 'Copy Original',
copyFormatted: 'Copy Formatted',
success: 'Copied',
tooltip: 'Copy by this button if you need to run it in SQL client',
},
zh: {
text: '复制',
copy: '复制',
copyOriginal: '复制原始 SQL',
copyFormatted: '复制格式化 SQL',
success: '已复制',
tooltip: '如果你需要在 SQL 客户端执行此 SQL 语句,请使用这个按钮进行复制',
baurine marked this conversation as resolved.
Show resolved Hide resolved
},
}

Expand All @@ -30,7 +38,7 @@ for (const key in translations) {
})
}

function CopyLink({ data }: ICopyLinkProps) {
function CopyLink({ data, textTransKey = 'copy' }: ICopyLinkProps) {
const { t } = useTranslation()
const [showCopied, setShowCopied] = useState(false)

Expand All @@ -43,15 +51,20 @@ function CopyLink({ data }: ICopyLinkProps) {
reset()
}

const copyBtn = (
<CopyToClipboard text={data} onCopy={handleCopy}>
<a>
{t(`component.copyLink.${textTransKey}`)} <CopyOutlined />
</a>
</CopyToClipboard>
)

return (
<span>
{!showCopied && (
<CopyToClipboard text={data} onCopy={handleCopy}>
<a>
{t('component.copyLink.text')} <CopyOutlined />
</a>
</CopyToClipboard>
{!showCopied && textTransKey === 'copyOriginal' && (
<Tooltip title={t('component.copyLink.tooltip')}>{copyBtn}</Tooltip>
baurine marked this conversation as resolved.
Show resolved Hide resolved
)}
{!showCopied && textTransKey !== 'copyOriginal' && copyBtn}
{showCopied && (
<span className={styles.copiedText}>
<CheckOutlined /> {t('component.copyLink.success')}
Expand Down