1
0

ConfigBlock.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import type { MouseEvent } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { Button, Collapse, Popover, Tag, Tooltip, message } from 'antd';
  4. import { CopyOutlined, DownloadOutlined, QrcodeOutlined } from '@ant-design/icons';
  5. import { ClipboardManager, FileManager } from '@/utils';
  6. import { QrPanel } from '@/pages/inbounds/qr';
  7. import './ConfigBlock.css';
  8. interface ConfigBlockProps {
  9. label: string;
  10. text: string;
  11. fileName: string;
  12. qrRemark?: string;
  13. showQr?: boolean;
  14. tagColor?: string;
  15. defaultOpen?: boolean;
  16. }
  17. export default function ConfigBlock({
  18. label,
  19. text,
  20. fileName,
  21. qrRemark = '',
  22. showQr = true,
  23. tagColor = 'gold',
  24. defaultOpen = false,
  25. }: ConfigBlockProps) {
  26. const { t } = useTranslation();
  27. const [messageApi, messageContextHolder] = message.useMessage();
  28. async function copy() {
  29. const ok = await ClipboardManager.copyText(text);
  30. if (ok) messageApi.success(t('copied'));
  31. }
  32. const actions = (
  33. <div className="config-block-actions" onClick={(e: MouseEvent) => e.stopPropagation()}>
  34. <Tooltip title={t('copy')}>
  35. <Button size="small" icon={<CopyOutlined />} onClick={copy} />
  36. </Tooltip>
  37. <Tooltip title={t('download')}>
  38. <Button
  39. size="small"
  40. icon={<DownloadOutlined />}
  41. onClick={() => FileManager.downloadTextFile(text, fileName)}
  42. />
  43. </Tooltip>
  44. {showQr && (
  45. <Popover
  46. trigger="click"
  47. placement="left"
  48. destroyOnHidden
  49. content={<QrPanel value={text} remark={qrRemark || label} size={220} />}
  50. >
  51. <Tooltip title={t('pages.clients.qrCode')}>
  52. <Button size="small" icon={<QrcodeOutlined />} />
  53. </Tooltip>
  54. </Popover>
  55. )}
  56. </div>
  57. );
  58. return (
  59. <>
  60. {messageContextHolder}
  61. <Collapse
  62. className="config-block"
  63. defaultActiveKey={defaultOpen ? ['cfg'] : []}
  64. items={[{
  65. key: 'cfg',
  66. label: <Tag color={tagColor} style={{ margin: 0, fontWeight: 600, letterSpacing: '0.3px' }}>{label}</Tag>,
  67. extra: actions,
  68. children: <code className="config-block-text">{text}</code>,
  69. }]}
  70. />
  71. </>
  72. );
  73. }