-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathBookmarks.tsx
More file actions
71 lines (62 loc) · 2.92 KB
/
Bookmarks.tsx
File metadata and controls
71 lines (62 loc) · 2.92 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
import React from 'react'
import $rdf from 'rdflib'
import namespaces from 'solid-namespace'
import { DataBrowserContext } from '../context'
import { useWebId } from '../hooks/useWebId'
const ns = namespaces($rdf)
export const BookmarksWidget: React.FC = () => {
const { store, fetcher, podOrigin } = React.useContext(DataBrowserContext)
const bookmarks = useBookmarks(store, fetcher)
if (!Array.isArray(bookmarks) || bookmarks.length === 0) {
return null
}
return (
<div className="card">
<section className="section">
<h2 className="title">Latest bookmarks</h2>
<div className="content">
<ul>
{bookmarks.slice(0, 5).map((bookmark) => <li key={bookmark.url}><a href={bookmark.url}>{bookmark.title}</a></li>)}
</ul>
</div>
<a className="button is-text" href={`https://vincenttunru.gitlab.io/poddit?idp=${podOrigin || ''}`}>All bookmarks</a>
</section>
</div>
)
}
function useBookmarks (store: $rdf.IndexedFormula, fetcher: $rdf.Fetcher) {
const webId = useWebId()
const [bookmarks, setBookmarks] = React.useState<Array<{ title: string, url: string }>>()
React.useEffect(() => {
if (!webId) {
return
}
getBookmarks(store, fetcher, webId)
.then(setBookmarks)
.catch((e) => console.log('Error fetching bookmarks:', e))
}, [store, fetcher, webId])
return bookmarks
}
async function getBookmarks (store: $rdf.IndexedFormula, fetcher: $rdf.Fetcher, webId: string) {
const profile = $rdf.sym(webId)
const [ publicTypeIndexStatement ] = store.statementsMatching(profile, ns.solid('publicTypeIndex'), null, profile.doc(), true)
const publicTypeIndex = publicTypeIndexStatement.object
await fetcher.load(publicTypeIndex as any as $rdf.NamedNode)
const bookmarkClass = new $rdf.NamedNode('http://www.w3.org/2002/01/bookmark#Bookmark')
const [ bookmarkRegistryStatement ] = store.statementsMatching(null, ns.solid('forClass'), bookmarkClass, publicTypeIndex, true)
const bookmarkRegistry = bookmarkRegistryStatement.subject
const [ bookmarkRegistryInstanceStatement ] = store.statementsMatching(bookmarkRegistry, ns.solid('instance'), null, publicTypeIndex, true)
const bookmarkRegistryInstance = bookmarkRegistryInstanceStatement.object
await fetcher.load(bookmarkRegistryInstance as any as $rdf.NamedNode)
const bookmarkStatements = store.statementsMatching(null, ns.rdf('type'), bookmarkClass, bookmarkRegistryInstance)
return bookmarkStatements.map((statement) => {
const recalls = new $rdf.NamedNode('http://www.w3.org/2002/01/bookmark#recalls')
const bookmarkNode = statement.subject
const [ titleStatement ] = store.statementsMatching(bookmarkNode, ns.dct('title'), null, bookmarkRegistryInstance)
const [ urlStatement ] = store.statementsMatching(bookmarkNode, recalls, null, bookmarkRegistryInstance)
return {
title: titleStatement.object.value,
url: urlStatement.object.value
}
})
}