QrcodeModal.tsx
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { useState } from 'react';
import type { QrcodeStatus } from '../types/home';
export interface QrcodeModalProps {
isOpen: boolean;
onClose: () => void;
positionName: string;
qrcodeUrl: string | null;
qrcodeStatus: QrcodeStatus;
}
export function QrcodeModal({
isOpen,
onClose,
positionName,
qrcodeUrl,
qrcodeStatus,
}: QrcodeModalProps) {
const [imageError, setImageError] = useState(false);
if (!isOpen) {
return null;
}
const handleImageError = () => {
setImageError(true);
};
const isQrcodeAvailable = qrcodeStatus === 'available' && qrcodeUrl && !imageError;
return (
<div
className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 px-margin-mobile"
onClick={onClose}
>
<div
className="bg-surface rounded-3xl p-6 max-w-sm w-full"
onClick={(e) => e.stopPropagation()}
>
<h3 className="text-headline-sm text-center mb-4">{positionName}</h3>
<div className="w-48 h-48 mx-auto bg-surface-container rounded-2xl flex items-center justify-center">
{isQrcodeAvailable ? (
<img
src={qrcodeUrl}
alt="二维码"
className="w-full h-full object-contain"
onError={handleImageError}
/>
) : (
<span className="text-on-surface-variant">二维码不可用</span>
)}
</div>
<p className="text-center text-on-surface-variant mt-4">扫码添加专属服务助手</p>
<div className="mt-4 flex justify-center">
<button
onClick={onClose}
className="px-6 py-2 bg-primary text-white text-label-sm rounded-lg hover:bg-primary/90 transition-colors"
>
关闭
</button>
</div>
</div>
</div>
);
}