373fc0496c
统一简化多个页面里的CopyButton调用,删除不再需要的空回调参数 * feat: 添加clearCookies函数的单元测试并修复cookie域名处理逻辑 * feat: 增强 data URI 处理,支持带参数的前缀并更新相关测试 * fix: 修正 AGENTS.md 中 TypeScript 类型检查命令的描述 * feat: 添加 settings.local.json 文件以配置 Bash 权限 * perf: 预设背景色避免 Popup 弹窗白屏闪烁 * perf: 避免图标过早实例化,传递组件引用而非 JSX 节点 * feat: 添加 useLazyTranslation 和 preloadNamespaces 函数以支持动态加载 i18n 命名空间 * feat: 使用 useLazyTranslation 替换 useTranslation 以支持懒加载翻译 * feat: 添加 PageSkeleton 组件及其测试用例以支持页面加载骨架屏 * feat: 使用骨架屏替换加载状态指示器,优化用户体验 * feat: 优化 CopyButton 组件的复制功能,添加定时器管理复制状态 * feat: 调整 chunk 大小警告阈值以优化构建性能
101 lines
3.0 KiB
TypeScript
101 lines
3.0 KiB
TypeScript
import React, { useEffect, useRef, useState } from 'react';
|
|
import { IconButton, Tooltip } from '@mui/material';
|
|
import ContentCopyIcon from '@mui/icons-material/ContentCopy';
|
|
import CheckIcon from '@mui/icons-material/Check';
|
|
import { copyTextToClipboard } from '@/utils/clipboard';
|
|
import type { SnackbarOptions } from '@/components/GlobalSnackbar';
|
|
|
|
/**
|
|
* 复制按钮组件属性
|
|
* @param text 要复制的文本
|
|
* @param tooltip 提示信息
|
|
* @param size 按钮大小
|
|
* @param color 按钮颜色
|
|
* @param style 自定义样式
|
|
* @param showMessage 消息提示函数,用于显示复制成功或失败的消息
|
|
*/
|
|
interface CopyButtonProps {
|
|
text: string;
|
|
tooltip?: string;
|
|
size?: 'small' | 'medium' | 'large';
|
|
color?: 'primary' | 'secondary' | 'success' | 'error' | 'info' | 'warning' | string;
|
|
style?: React.CSSProperties;
|
|
showMessage?: (message: string, options?: SnackbarOptions) => void;
|
|
}
|
|
|
|
/**
|
|
* 复制按钮组件
|
|
* @param text 要复制的文本
|
|
* @param tooltip 提示信息
|
|
* @param size 按钮大小
|
|
* @param color 按钮颜色
|
|
* @param style 自定义样式
|
|
* @param showMessage 消息提示函数,用于显示复制成功或失败的消息
|
|
* @returns 复制按钮组件
|
|
*/
|
|
export const CopyButton: React.FC<CopyButtonProps> = ({
|
|
text,
|
|
tooltip = '复制',
|
|
size = 'small',
|
|
color = 'primary',
|
|
style,
|
|
showMessage,
|
|
}) => {
|
|
const [copied, setCopied] = useState(false);
|
|
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
|
|
useEffect(() => {
|
|
return () => {
|
|
if (timerRef.current) clearTimeout(timerRef.current);
|
|
};
|
|
}, []);
|
|
|
|
const handleCopy = async () => {
|
|
if (text) {
|
|
const success = await copyTextToClipboard(text);
|
|
if (success) {
|
|
showMessage?.('复制成功', { severity: 'success' });
|
|
setCopied(true);
|
|
if (timerRef.current) clearTimeout(timerRef.current);
|
|
timerRef.current = setTimeout(() => setCopied(false), 1500);
|
|
} else {
|
|
showMessage?.('复制失败', { severity: 'error' });
|
|
}
|
|
} else {
|
|
showMessage?.('无内容可复制', { severity: 'error' });
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Tooltip title={tooltip}>
|
|
<IconButton
|
|
size={size}
|
|
onClick={handleCopy}
|
|
style={style}
|
|
sx={{
|
|
color: copied ? 'success.main' : color,
|
|
bgcolor: 'background.paper',
|
|
boxShadow: (theme) =>
|
|
`0 2px 8px ${theme.palette.mode === 'dark' ? 'rgba(0,0,0,0.3)' : 'rgba(0,0,0,0.05)'}`,
|
|
'&:hover': {
|
|
bgcolor: copied
|
|
? 'success.main'
|
|
: !['primary', 'secondary', 'success', 'error', 'info', 'warning'].includes(color)
|
|
? color
|
|
: `${color}.main`,
|
|
color: 'background.paper',
|
|
},
|
|
}}
|
|
>
|
|
{copied ? (
|
|
<CheckIcon fontSize={size === 'small' ? 'small' : 'medium'} />
|
|
) : (
|
|
<ContentCopyIcon fontSize={size === 'small' ? 'small' : 'medium'} />
|
|
)}
|
|
</IconButton>
|
|
</Tooltip>
|
|
);
|
|
};
|
|
|
|
export default CopyButton;
|