QRCodeModal.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { useEffect } from 'react';
  2. import { X, Download } from 'lucide-react';
  3. import { Button } from './Button';
  4. import { api } from '../api/client';
  5. interface QRCodeModalProps {
  6. archiveId: number;
  7. archiveName: string;
  8. onClose: () => void;
  9. }
  10. export function QRCodeModal({ archiveId, archiveName, onClose }: QRCodeModalProps) {
  11. const qrCodeUrl = api.getArchiveQRCodeUrl(archiveId, 300);
  12. // Close on Escape key
  13. useEffect(() => {
  14. const handleKeyDown = (e: KeyboardEvent) => {
  15. if (e.key === 'Escape') onClose();
  16. };
  17. window.addEventListener('keydown', handleKeyDown);
  18. return () => window.removeEventListener('keydown', handleKeyDown);
  19. }, [onClose]);
  20. const handleDownload = () => {
  21. const link = document.createElement('a');
  22. link.href = qrCodeUrl;
  23. link.download = `${archiveName}_qrcode.png`;
  24. link.click();
  25. };
  26. return (
  27. <div
  28. className="fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4"
  29. onClick={onClose}
  30. >
  31. <div
  32. className="bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary w-full max-w-sm"
  33. onClick={(e) => e.stopPropagation()}
  34. >
  35. {/* Header */}
  36. <div className="flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary">
  37. <h2 className="text-lg font-semibold text-white">QR Code</h2>
  38. <button
  39. onClick={onClose}
  40. className="text-bambu-gray hover:text-white transition-colors"
  41. >
  42. <X className="w-5 h-5" />
  43. </button>
  44. </div>
  45. {/* Content */}
  46. <div className="p-6 flex flex-col items-center">
  47. <p className="text-sm text-bambu-gray mb-4 text-center truncate max-w-full">
  48. {archiveName}
  49. </p>
  50. <div className="bg-white p-4 rounded-lg mb-4">
  51. <img
  52. src={qrCodeUrl}
  53. alt="QR Code"
  54. className="w-64 h-64"
  55. />
  56. </div>
  57. <p className="text-xs text-bambu-gray mb-4 text-center">
  58. Scan to open this archive
  59. </p>
  60. <Button onClick={handleDownload} className="w-full">
  61. <Download className="w-4 h-4" />
  62. Download QR Code
  63. </Button>
  64. </div>
  65. </div>
  66. </div>
  67. );
  68. }