-
Notifications
You must be signed in to change notification settings - Fork 56
/
index.js
242 lines (199 loc) · 7.57 KB
/
index.js
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import React from 'react';
import { useEffect, useState } from 'react';
import { useParams, Link } from 'react-router-dom';
import validator from 'validator';
import { Button, Group, Container, Textarea, TextInput, Stack, Title, Text } from '@mantine/core';
import {
IconSquarePlus,
IconDownload,
IconLock,
IconEye,
IconPerspective,
IconHeading,
} from '@tabler/icons';
import Error from '../../components/info/error';
import { getSecret, secretExists } from '../../api/secret';
import { downloadFile } from '../../api/upload';
import { getToken } from '../../helpers/token';
const Secret = () => {
const { secretId, encryptionKey = null } = useParams();
const [secret, setSecret] = useState(null);
const [title, setTitle] = useState(null);
const [preventBurn, setPreventBurn] = useState(null);
const [isSecretOpen, setIsSecretOpen] = useState(false);
const [password, setPassword] = useState('');
const [isPasswordRequired, setIsPasswordRequired] = useState(false);
const [files, setFiles] = useState(null);
const [isDownloaded, setIsDownloaded] = useState(false);
const [error, setError] = useState(null);
const [hasConvertedBase64ToPlain, setHasConvertedBase64ToPlain] = useState(false);
const fetchSecret = async (event) => {
event.preventDefault();
if (isPasswordRequired && !password) {
return;
}
if (secret) {
setIsSecretOpen(true);
return;
}
const json = await getSecret(secretId, encryptionKey, password);
if (json.statusCode === 401) {
setIsPasswordRequired(true);
setError('Incorrect password!');
return;
}
if (json.error) {
setError(json.error);
} else {
setSecret(validator.unescape(json.secret));
if (json.title) {
setTitle(validator.unescape(json.title));
}
if (json.files) {
setFiles(json.files);
}
if (json.preventBurn) {
setPreventBurn(json.preventBurn);
}
setIsSecretOpen(true);
setError(null);
}
};
useEffect(() => {
(async () => {
const response = await secretExists(secretId, password);
if (response.statusCode === 401) {
setIsPasswordRequired(true);
return () => {};
}
if (response.error) {
setError(response.error);
}
})();
// eslint-disable-next-line
}, [secretId]);
const onPasswordChange = (event) => {
setPassword(event.target.value);
};
const onFilesDownload = (event) => {
event.preventDefault();
downloadFile(
{
files,
encryptionKey,
secretId,
},
getToken()
);
if (!preventBurn) {
setIsDownloaded(true);
}
};
const convertBase64ToPlain = () => {
setSecret(atob(secret));
setHasConvertedBase64ToPlain(true);
};
return (
<Container>
<Stack>
<Title order={1}>View your secret</Title>
<Text>We will only show the secret once.</Text>
{title && <TextInput icon={<IconHeading size={14} />} value={title} readOnly />}
{isSecretOpen && (
<Textarea minRows={10} maxRows={30} value={secret} autosize readOnly />
)}
{isPasswordRequired && !isSecretOpen && (
<>
<Text>A password is required to open this secret</Text>
<TextInput
id="lemon-password"
icon={<IconLock size={14} />}
placeholder="Your password"
value={password}
onChange={onPasswordChange}
required
style={{ WebkitTextSecurity: 'disc' }}
/>
</>
)}
<Group>
{!isSecretOpen && (
<Button
styles={() => ({
root: {
backgroundColor: 'var(--color-contrast)',
'&:hover': {
backgroundColor: 'var(--color-contrast)',
filter: 'brightness(115%)',
},
},
})}
leftIcon={<IconEye size={14} />}
onClick={fetchSecret}
>
View the secret
</Button>
)}
</Group>
<Group position="right">
{isSecretOpen && (
<Button
styles={() => ({
root: {
backgroundColor: 'var(--color-contrast)',
'&:hover': {
backgroundColor: 'var(--color-contrast)',
filter: 'brightness(115%)',
},
},
})}
leftIcon={<IconSquarePlus size={14} />}
component={Link}
to="/"
>
Create a new secret
</Button>
)}
{files && !isDownloaded && (
<Button
styles={() => ({
root: {
backgroundColor: '#FF9769',
'&:hover': {
backgroundColor: '#FF9769',
filter: 'brightness(115%)',
},
},
})}
onClick={onFilesDownload}
disabled={!secretId}
leftIcon={<IconDownload size={14} />}
>
Download files
</Button>
)}
{isSecretOpen && (
<Button
styles={() => ({
root: {
backgroundColor: '#FF9769',
'&:hover': {
backgroundColor: '#FF9769',
filter: 'brightness(115%)',
},
},
})}
leftIcon={<IconPerspective size={14} />}
onClick={convertBase64ToPlain}
disabled={hasConvertedBase64ToPlain}
>
Convert base64 to plain text
</Button>
)}
</Group>
</Stack>
{error && <Error>{error}</Error>}
</Container>
);
};
export default Secret;