-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.tsx
165 lines (152 loc) · 3.59 KB
/
index.tsx
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
import React, { useEffect, useCallback, useState } from "react"
import ReactDOM from "react-dom"
import jsonld from "jsonld"
import { useDebounce } from "use-debounce"
import { QuadT, Store, Parse } from "n3.ts"
import "rdf-cytoscape/rdf-cytoscape.css"
import { Dataset } from "rdf-cytoscape"
const main = document.querySelector("main")
const jsonLdFormat = "json-ld",
nQuadsFormat = "n-quads"
const initialFormat = jsonLdFormat
const initialText = JSON.stringify(
{
"@context": {
"@vocab": "http://schema.org/",
},
"@type": "Person",
name: "John Doe",
},
null,
" "
)
async function parseText(text: string, format: string): Promise<Store> {
if (format === jsonLdFormat) {
const doc = JSON.parse(text)
const quads = (await jsonld.toRDF(doc)) as QuadT[]
for (const quad of quads) {
if (quad.subject.termType === "BlankNode") {
const value = quad.subject.value
if (value.startsWith("_:")) {
quad.subject.value = value.slice(2)
}
}
if (quad.object.termType === "BlankNode") {
const value = quad.object.value
if (value.startsWith("_:")) {
quad.object.value = value.slice(2)
}
}
if (quad.graph.termType === "BlankNode") {
const value = quad.graph.value
if (value.startsWith("_:")) {
quad.graph.value = value.slice(2)
}
}
}
return new Store(quads)
} else if (format === nQuadsFormat) {
return new Store(Parse(text))
} else {
throw new Error(`Invalid format ${format}`)
}
}
type Format = typeof jsonLdFormat | typeof nQuadsFormat
function Index(
props:
| {
frame?: false
}
| {
frame: true
format: Format
src: string
}
) {
const [error, setError] = useState(null as string | null)
const [format, setFormat] = useState(
props.frame ? props.format : initialFormat
)
const handleFormatChange = useCallback(
({ target: { value } }: React.ChangeEvent<HTMLInputElement>) => {
if (value === jsonLdFormat || value === nQuadsFormat) {
setFormat(value)
}
},
[]
)
const [value, setValue] = useState(initialText)
const handleValueChange = useCallback(
(event: React.ChangeEvent<HTMLTextAreaElement>) =>
setValue(event.target.value),
[]
)
const [store, setStore] = useState(null as Store | null)
const [text] = useDebounce(value, 1000)
useEffect(() => {
parseText(text, format)
.then((store) => {
setStore(store)
setError(null)
})
.catch((error) => {
setStore(null)
setError(error.toString())
})
}, [text, format])
if (props.frame) {
return <Frame store={store} error={error} />
}
return (
<React.Fragment>
<div className="input">
<a href="https://github.com/underlay/explore">underlay/explore</a>
<hr />
<header>
<span>input type:</span>
<label>
<input
type="radio"
value={nQuadsFormat}
name="format"
// disabled={true}
checked={format === nQuadsFormat}
onChange={handleFormatChange}
></input>
N-Quads
</label>
<label>
<input
type="radio"
value={jsonLdFormat}
name="format"
checked={format === jsonLdFormat}
onChange={handleFormatChange}
></input>
JSON-LD
</label>
</header>
<textarea value={value} onChange={handleValueChange}></textarea>
</div>
<Frame store={store} error={error} />
</React.Fragment>
)
}
function Frame({
store,
error,
}: {
store: Store | null
error: string | null
}) {
return (
<div className="rdf-cytoscape">
{store === null ? (
<p className="error">{error || "Loading..."}</p>
) : (
<Dataset dataset={store} />
)}
</div>
)
}
ReactDOM.render(<Index />, main)