-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.js
More file actions
209 lines (158 loc) · 4.73 KB
/
Copy pathui.js
File metadata and controls
209 lines (158 loc) · 4.73 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
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
import * as namedrop from './lib/namedrop-js/index.js';
import * as atproto from './atproto.js';
class Main extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
const tmpl = cloneTemplate('main-template');
const contentEl = tmpl.querySelector('.content');
let page;
(async () => {
const atClient = await atproto.checkLogin();
if (atClient) {
const url = new URL(window.location);
const params = new URLSearchParams(url.search);
const state = params.get('state');
const code = params.get('code');
// Detect TakingNames oauth callback
if (state && code) {
page = document.createElement('status-page');
page.atClient = atClient;
}
else {
page = document.createElement('logged-in-page');
page.atClient = atClient;
}
}
else {
page = document.createElement('login-page');
}
contentEl.appendChild(page);
})();
this.addEventListener('logout', (evt) => {
//window.location = '/';
contentEl.removeChild(page);
page = document.createElement('login-page');
contentEl.appendChild(page);
});
this.addEventListener('connected', (evt) => {
});
this.appendChild(tmpl);
}
}
class LoginPage extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
const tmpl = cloneTemplate('login-page');
const loginBtn = tmpl.querySelector('#login-btn');
loginBtn.addEventListener('click', (evt) => {
evt.preventDefault();
atproto.login('bsky.social');
});
this.appendChild(tmpl);
}
}
class LoggedInPage extends HTMLElement {
constructor() {
super();
}
get atClient() {
return this._atClient;
}
set atClient(_) {
this._atClient = _;
}
connectedCallback() {
const tmpl = cloneTemplate('logged-in-page');
const userIdEl = tmpl.querySelector('.user-id');
userIdEl.innerText = this.atClient.handle;
const logoutBtn = tmpl.querySelector('#logout-btn');
logoutBtn.addEventListener('click', (evt) => {
evt.preventDefault();
atproto.logout();
emitEvent(this, 'logout');
});
const tnBtn = tmpl.querySelector('#tn-btn');
tnBtn.addEventListener('click', async (evt) => {
await namedrop.startAuthFlow({ scopes: [ namedrop.SCOPE_ATPROTO_HANDLE ] });
});
this.appendChild(tmpl);
}
}
class StatusPage extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
const tmpl = cloneTemplate('status-page');
const statusText = tmpl.querySelector('#status-text');
(async () => {
const ndClient = await namedrop.checkAuthFlow();
if (ndClient) {
setRecords(this.atClient, ndClient, statusText);
}
})();
this.appendChild(tmpl);
}
}
async function setRecords(atClient, ndClient, statusText) {
if (ndClient.permissions.length !== 1) {
throw new Error("Wrong number of perms");
}
const domain = ndClient.permissions[0].domain;
const host = ndClient.permissions[0].host;
ndClient.setRecords({
records: [
{
domain,
host: host ? `_atproto.${host}` : '_atproto.',
type: 'TXT',
value: `did=${atClient.did}`,
}
],
});
const newHandle = host ? `${host}.${domain}` : domain;
const failHtml = `Failed to update handle. <a href='${window.location.href}'>Start Over</a>.`;
let attemptNum = 1;
const intId = setInterval(async () => {
if (attemptNum > 10) {
clearInterval(intId);
statusText.innerHTML = failHtml;
return;
}
statusText.innerText = `Handle submitted. This should only take a few seconds to verify. Checking once per second (attempt ${attemptNum})`;
attemptNum += 1;
const did = await atClient.resolveHandle(newHandle);
if (did === atClient.did) {
clearInterval(intId);
try {
await atClient.updateHandle(newHandle);
}
catch (e) {
console.log(e);
statusText.innerHTML = failHtml;
return;
}
atproto.setLoginDataHandle(newHandle);
statusText.innerHTML = `Successfully updated handle to <span class='user-id'>${newHandle}</span>. <a href='${window.location.href}'>Start Over</a>`;
}
}, 1000);
}
function cloneTemplate(templateId) {
const template = document.getElementById(templateId);
const docFrag = template.content.cloneNode(true);
return docFrag;
}
function emitEvent(el, name, detail) {
el.dispatchEvent(new CustomEvent(name, {
bubbles: true,
detail,
}));
}
customElements.define('custom-handle-main', Main);
customElements.define('logged-in-page', LoggedInPage);
customElements.define('login-page', LoginPage);
customElements.define('status-page', StatusPage);