-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
213 lines (187 loc) · 5.57 KB
/
app.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
// app.js
import { html, Component, render } from './js/spux.js'
import { getPath, getQueryStringValue, loadFile, saveFile } from './util.js'
import './js/dior.js'
import GithubRibbon from './components/GithubRibbon.js'
export class App extends Component {
constructor() {
super()
const serverUrl = getQueryStringValue('storage') || di.data.storage || 'https://nosdav.net'
const mode = getQueryStringValue('mode') || di.data.m || 'm'
const uri = getQueryStringValue('uri') || di.data.uri || 'paste.txt'
document.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.key === 's') {
e.preventDefault();
this.save();
}
})
this.state = {
userPublicKey: null,
filename: uri,
contentType: null,
fileContent: '',
path: null,
downloadLink: '',
serverUrl: serverUrl,
mode: mode
}
}
updateFileContent = (event) => {
this.setState({ fileContent: event.target.value })
}
updateDownloadLink = () => {
const { filename, userPublicKey, serverUrl, mode } = this.state
const path = getPath(serverUrl, userPublicKey, filename, mode)
this.setState({ downloadLink: path })
}
updateFilename = (event) => {
this.setState({ filename: event.target.value }, () => {
this.updateDownloadLink();
});
}
userLogin = async () => {
var userPublicKey
try {
userPublicKey = await window.nostr.getPublicKey();
if (userPublicKey) {
Swal.fire({
title: "Authenticated!",
text: "File loaded to pastebin!",
icon: "success",
timer: 1500
});
} else {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Something went wrong!",
footer: '<p><a target="_blank" href="https://nostrapps.github.io/extensions/">Please install a nostr extension</a></p>'
});
}
} catch (error) {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Something went wrong!",
footer: '<p><a target="_blank" href="https://nostrapps.github.io/extensions/">Please install a nostr extension</a></p>'
});
console.error('Login error:', error);
}
console.log(`Logged in with public key: ${userPublicKey}`);
await this.setState({ userPublicKey: userPublicKey });
this.loadFile()
}
// Load the file content from the server
loadFile = async () => {
if (!this.state.userPublicKey) {
// alert('Please login first');
return;
}
const { filename, userPublicKey, serverUrl, mode } = this.state;
const fileContent = await loadFile(serverUrl, userPublicKey, filename, mode);
if (fileContent) {
this.setState({ fileContent: fileContent });
}
this.updateDownloadLink();
};
save = async () => {
if (!this.state.userPublicKey) {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Something went wrong!"
});
// alert('Please login first');
return;
}
const { fileContent, filename, userPublicKey, serverUrl, mode } = this.state;
var success
try {
success = await saveFile(serverUrl, userPublicKey, filename, mode, fileContent);
} catch (error) {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Something went wrong!"
});
// alert('Please login first');
return;
}
if (success) {
Swal.fire({
title: "Saved!",
text: "File Saved Successfully!",
icon: "success",
timer: 1500
});
}
if (!success) {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Something went wrong!"
});
}
};
render() {
const { userPublicKey, filename, fileContent, downloadLink } = this.state
return html`
<${GithubRibbon} repo="https://github.com/nosdav/pastebin/" />
<div id="container">
<h1>NosDAV</h1>
<label for="file-name"
>File${' '}
<a href="${downloadLink}" id="download" target="_blank">
<svg
xmlns="http://www.w3.org/2000/svg"
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
<polyline points="7 10 12 15 17 10"></polyline>
<line x1="12" y1="15" x2="12" y2="3"></line></svg></a
></label>
<input
type="text"
id="file-name"
name="file-name"
placeholder="Enter filename"
value="${filename}"
onInput="${event => {
this.updateFilename(event)
this.updateDownloadLink()
}}"
/>
<label for="content">Enter Text Below</label>
<textarea
id="file-content"
placeholder="Login First"
name="content"
rows="10"
value="${fileContent}"
onInput="${this.updateFileContent}"
></textarea>
<div>
${userPublicKey
? html`
<button id="save" onClick="${this.save}">Save</button>
<button id="login" onClick="${this.userLogin}">
Load
</button>
`
: html` <button id="login" onClick="${this.userLogin}">
Login
</button>`}
</div>
<div></div>
</div>
`
}
}
render(html` <${App} /> `, document.body)