-
Notifications
You must be signed in to change notification settings - Fork 4
/
search.js
69 lines (57 loc) · 1.74 KB
/
search.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
import { db } from './dbInstance.js'
const response = await fetch('./search-template.html')
const text = await response.text()
const template = document.createElement('template')
template.innerHTML = text
const style = document.createElement('style')
style.textContent = '@import url("./search.css");'
document.head.appendChild(style)
class DistributedSearch extends HTMLElement {
constructor () {
super()
this.init()
}
get form () {
return this.querySelector('form')
}
get url () {
return this.querySelector('[name=url]').value
}
init () {
const instance = template.content.cloneNode(true)
this.appendChild(instance)
this.form.addEventListener('submit', (e) => {
e.preventDefault()
this.handleSubmit(e)
})
}
async handleSubmit () {
// TODO: Detect `@username@domain syntax
const url = this.url
try {
// TODO: Redirect to p2p version
const data = await db.resolveURL(url)
const { id, type } = data
if (type === 'Person' || type === 'Service') {
const newURL = new URL('/profile.html', window.location.href)
newURL.searchParams.set('actor', id)
window.location.href = newURL.href
} else if (type === 'Note') {
const newURL = new URL('/post.html', window.location.href)
newURL.searchParams.set('url', id)
window.location.href = newURL.href
} else {
throw new Error(`Invalid JSON-LD type: ${type}`)
}
} catch (e) {
this.showError(e)
}
}
showError (e) {
console.error(e)
const element = document.createElement('error-message')
element.setAttribute('message', e.message)
this.appendChild(element)
}
}
customElements.define('distributed-search', DistributedSearch)