chore: copy button

This commit is contained in:
Joel 2024-10-16 11:05:51 +08:00
parent d1dcd39191
commit 1bd70bd8bf
3 changed files with 25 additions and 6 deletions

View File

@ -1,13 +1,14 @@
'use client' 'use client'
import type { FC } from 'react' import type { FC } from 'react'
import React, { useCallback } from 'react' import React, { useCallback, useEffect, useState } from 'react'
import copy from 'copy-to-clipboard' import copy from 'copy-to-clipboard'
import { import {
RiClipboardLine, RiClipboardLine,
} from '@remixicon/react' } from '@remixicon/react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import Toast from '../../base/toast' import { ClipboardCheck } from '../../base/icons/src/vender/line/files'
import Tooltip from '../../base/tooltip'
import ActionButton from '@/app/components/base/action-button' import ActionButton from '@/app/components/base/action-button'
type Props = { type Props = {
label: string label: string
@ -19,11 +20,25 @@ const KeyValueItem: FC<Props> = ({
value, value,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
const [isCopied, setIsCopied] = useState(false)
const handleCopy = useCallback(() => { const handleCopy = useCallback(() => {
copy(value) copy(value)
Toast.notify({ type: 'success', message: t('common.actionMsg.copySuccessfully') }) setIsCopied(true)
}, [value]) }, [value])
useEffect(() => {
if (isCopied) {
const timer = setTimeout(() => {
setIsCopied(false)
}, 2000)
return () => {
clearTimeout(timer)
}
}
}, [isCopied])
const CopyIcon = isCopied ? ClipboardCheck : RiClipboardLine
return ( return (
<div className='flex items-center gap-1 self-stretch'> <div className='flex items-center gap-1 self-stretch'>
<span className='flex w-10 flex-col justify-center items-start text-text-tertiary system-xs-medium'>{label}</span> <span className='flex w-10 flex-col justify-center items-start text-text-tertiary system-xs-medium'>{label}</span>
@ -31,9 +46,11 @@ const KeyValueItem: FC<Props> = ({
<span className='system-xs-medium text-text-secondary'> <span className='system-xs-medium text-text-secondary'>
{value} {value}
</span> </span>
<Tooltip popupContent={t(`common.operation.${isCopied ? 'copied' : 'copy'}`)} position='top'>
<ActionButton onClick={handleCopy}> <ActionButton onClick={handleCopy}>
<RiClipboardLine className='w-3.5 h-3.5 text-text-tertiary' /> <CopyIcon className='w-3.5 h-3.5 text-text-tertiary' />
</ActionButton> </ActionButton>
</Tooltip>
</div> </div>
</div> </div>
) )

View File

@ -23,6 +23,7 @@ const translation = {
remove: 'Remove', remove: 'Remove',
send: 'Send', send: 'Send',
copy: 'Copy', copy: 'Copy',
copied: 'Copied',
lineBreak: 'Line break', lineBreak: 'Line break',
sure: 'I\'m sure', sure: 'I\'m sure',
download: 'Download', download: 'Download',

View File

@ -23,6 +23,7 @@ const translation = {
remove: '移除', remove: '移除',
send: '发送', send: '发送',
copy: '复制', copy: '复制',
copied: ' 已复制',
lineBreak: '换行', lineBreak: '换行',
sure: '我确定', sure: '我确定',
download: '下载', download: '下载',