SubQrButton.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { useRef, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { Button, Input, Modal, QRCode } from 'antd';
  4. import { CopyOutlined, DownloadOutlined, QrcodeOutlined } from '@ant-design/icons';
  5. interface SubQrButtonProps {
  6. value: string;
  7. label: string;
  8. onCopy: (value: string) => void;
  9. }
  10. export default function SubQrButton({ value, label, onCopy }: SubQrButtonProps) {
  11. const { t } = useTranslation();
  12. const [open, setOpen] = useState(false);
  13. const qrRef = useRef<HTMLDivElement>(null);
  14. const saveQr = () => {
  15. const canvas = qrRef.current?.querySelector('canvas');
  16. if (!canvas) return;
  17. const link = document.createElement('a');
  18. link.href = canvas.toDataURL('image/png');
  19. link.download = `${label || 'qrcode'}.png`;
  20. link.click();
  21. };
  22. return (
  23. <>
  24. <Button icon={<QrcodeOutlined />} aria-label="QR" title="QR" onClick={() => setOpen(true)} />
  25. <Modal
  26. open={open}
  27. onCancel={() => setOpen(false)}
  28. footer={null}
  29. width={440}
  30. centered
  31. destroyOnHidden
  32. rootClassName="sub-qr-modal"
  33. title={t('subscription.qrTitle')}
  34. >
  35. <p className="sub-muted sub-qr-modal-hint">{t('subscription.qrHint')}</p>
  36. <div ref={qrRef} className="sub-qr-modal-code">
  37. <QRCode
  38. value={value}
  39. size={240}
  40. type="canvas"
  41. marginSize={2}
  42. bordered={false}
  43. color="#000000"
  44. bgColor="#ffffff"
  45. />
  46. </div>
  47. <Input.TextArea
  48. className="sub-qr-modal-link"
  49. value={value}
  50. readOnly
  51. dir="ltr"
  52. autoSize={{ minRows: 2, maxRows: 5 }}
  53. />
  54. <div className="sub-qr-modal-actions">
  55. <Button type="primary" size="large" icon={<CopyOutlined />} onClick={() => onCopy(value)}>
  56. {t('copy')}
  57. </Button>
  58. <Button size="large" icon={<DownloadOutlined />} onClick={saveQr}>
  59. {t('subscription.saveQr')}
  60. </Button>
  61. </div>
  62. </Modal>
  63. </>
  64. );
  65. }