forked from comit-network/xmr-btc-swap
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathActionableMonospaceTextBox.tsx
More file actions
169 lines (161 loc) · 4.42 KB
/
ActionableMonospaceTextBox.tsx
File metadata and controls
169 lines (161 loc) · 4.42 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { Box, Button, IconButton, Tooltip } from "@mui/material";
import { FileCopyOutlined, QrCode as QrCodeIcon } from "@mui/icons-material";
import { writeText } from "@tauri-apps/plugin-clipboard-manager";
import { useState } from "react";
import MonospaceTextBox from "./MonospaceTextBox";
import { Modal } from "@mui/material";
import QRCode from "react-qr-code";
type ModalProps = {
open: boolean;
onClose: () => void;
content: string;
};
type Props = {
content: string;
displayCopyIcon?: boolean;
enableQrCode?: boolean;
light?: boolean;
spoilerText?: string;
};
function QRCodeModal({ open, onClose, content }: ModalProps) {
return (
<Modal open={open} onClose={onClose}>
<Box
sx={{
display: "flex",
flexDirection: "column",
gap: 2,
justifyContent: "center",
alignItems: "center",
height: "100%",
}}
>
<QRCode
value={content}
size={500}
style={{
maxWidth: "90%",
maxHeight: "90%",
backgroundColor: "white",
borderRadius: 2,
padding: "1rem",
border: "1rem solid #e0e0e0",
}}
viewBox="0 0 500 500"
/>
<Button
onClick={onClose}
size="large"
variant="contained"
color="primary"
>
Done
</Button>
</Box>
</Modal>
);
}
export default function ActionableMonospaceTextBox({
content,
displayCopyIcon = true,
enableQrCode = true,
light = false,
spoilerText,
}: Props) {
const [copied, setCopied] = useState(false);
const [qrCodeOpen, setQrCodeOpen] = useState(false);
const [isQrCodeButtonHovered, setIsQrCodeButtonHovered] = useState(false);
const [isRevealed, setIsRevealed] = useState(!spoilerText);
const handleCopy = async () => {
await writeText(content);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
return (
<>
<Box sx={{ position: "relative" }}>
<Tooltip
title={
isQrCodeButtonHovered
? ""
: copied
? "Copied to clipboard"
: "Click to copy"
}
arrow
>
<Box
sx={{
display: "flex",
alignItems: "center",
cursor: "pointer",
filter: spoilerText && !isRevealed ? "blur(8px)" : "none",
transition: "filter 0.3s ease",
}}
>
<Box sx={{ flexGrow: 1 }} onClick={handleCopy}>
<MonospaceTextBox light={light}>
{content}
{displayCopyIcon && (
<IconButton
onClick={handleCopy}
size="small"
sx={{ marginLeft: 1 }}
>
<FileCopyOutlined />
</IconButton>
)}
{enableQrCode && (
<Tooltip title="Show QR Code" arrow>
<IconButton
onClick={() => setQrCodeOpen(true)}
onMouseEnter={() => setIsQrCodeButtonHovered(true)}
onMouseLeave={() => setIsQrCodeButtonHovered(false)}
size="small"
sx={{ marginLeft: 1 }}
>
<QrCodeIcon />
</IconButton>
</Tooltip>
)}
</MonospaceTextBox>
</Box>
</Box>
</Tooltip>
{spoilerText && !isRevealed && (
<Box
onClick={() => setIsRevealed(true)}
sx={{
position: "absolute",
inset: 0,
display: "flex",
alignItems: "center",
justifyContent: "center",
cursor: "pointer",
bgcolor: "rgba(0, 0, 0, 0.1)",
borderRadius: 1,
}}
>
<Box
sx={{
bgcolor: "background.paper",
p: 2,
borderRadius: 1,
boxShadow: 2,
}}
>
{spoilerText}
</Box>
</Box>
)}
</Box>
{enableQrCode && (
<QRCodeModal
open={qrCodeOpen}
onClose={() => setQrCodeOpen(false)}
content={content}
/>
)}
</>
);
}