-
Notifications
You must be signed in to change notification settings - Fork 42
/
homepage.ts
72 lines (58 loc) · 2.14 KB
/
homepage.ts
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
import { Fetcher, IndexedFormula, NamedNode, sym } from 'rdflib'
import { ns } from 'solid-ui'
export async function generateHomepage (
subject: NamedNode,
store: IndexedFormula,
fetcher: Fetcher
): Promise<HTMLElement> {
const ownersProfile = await loadProfile(subject, fetcher)
const name = getName(store, ownersProfile)
const wrapper = document.createElement('div')
wrapper.classList.add('container')
wrapper.appendChild(createTitle(ownersProfile.uri, name))
wrapper.appendChild(createDataSection(name))
return wrapper
}
function createDataSection (name: string): HTMLElement {
const dataSection = document.createElement('section')
const title = document.createElement('h2')
title.innerText = 'Data'
dataSection.appendChild(title)
const listGroup = document.createElement('div')
listGroup.classList.add('list-group')
dataSection.appendChild(listGroup)
const publicDataLink = document.createElement('a')
publicDataLink.classList.add('list-group-item')
publicDataLink.href = '/public/'
publicDataLink.innerText = `View ${name}'s files`
listGroup.appendChild(publicDataLink)
return dataSection
}
function createTitle (uri: string, name: string): HTMLElement {
const profileLink = document.createElement('a')
profileLink.href = uri
profileLink.innerText = name
const profileLinkPost = document.createElement('span')
profileLinkPost.innerText = '\'s Pod'
const title = document.createElement('h1')
title.appendChild(profileLink)
title.appendChild(profileLinkPost)
return title
}
async function loadProfile (
subject: NamedNode,
fetcher: Fetcher
): Promise<NamedNode> {
const pod = subject.site().uri
// TODO: This is a hack - we cannot assume that the profile is at this document, but we will live with it for now
const webId = sym(`${pod}profile/card#me`)
await fetcher.load(webId)
return webId
}
function getName (store: IndexedFormula, ownersProfile: NamedNode): string {
return (
store.anyValue(ownersProfile, ns.vcard('fn'), null, ownersProfile.doc()) ||
store.anyValue(ownersProfile, ns.foaf('name'), null, ownersProfile.doc()) ||
new URL(ownersProfile.uri).host.split('.')[0]
)
}