-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.jsx
241 lines (234 loc) · 6.61 KB
/
View.jsx
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { useState, useEffect, useMemo } from 'react'
import Ceramic from '@ceramicnetwork/http-client'
import { TileDocument } from '@ceramicnetwork/stream-tile'
import {
Heading, Spinner, Stack, UnorderedList, ListItem,
Flex, Textarea, Button, Input, Box, Table, Thead,
Tbody, Tfoot, Tr, Th, Td, TableCaption, Text,
Link as ChakraLink,
} from '@chakra-ui/react'
import {
useLocation, useRouteMatch, useHistory,
} from 'react-router'
import { HashLink as Link } from 'react-router-hash-link'
import JSON5 from 'json5'
import { useCeramic } from 'use-ceramic'
import MarkedVideo from './MarkedVideo'
import { ifSet, isSet, load, isoStringFor } from './utils'
export default ({
nftDID, IPFSButton, ipfs, ConnectButton,
desiredChain,
}) => {
const { pathname: path } = useLocation()
const { url } = useRouteMatch()
const [metaInput, setMetaInput] = useState('')
const [metadata, setMetadata] = useState('')
const [info, setInfo] = useState(null)
const [videos, setVideos] = useState(null)
const ceramic = useCeramic()
const history = useHistory()
const setFromObject = (json) => {
const {
video: { startsAt = null, source = null } = {},
isNew, stops,
} = (
json
)
setInfo({
startsAt: new Date(startsAt), source, stops, isNew,
})
}
useEffect(() => {
if(isSet(path) && path !== '/') {
if(!path.startsWith(url)) {
setMetadata(path)
} else {
setMetadata(
path.substring(url.length)
.replace(/^\/*/g, '')
)
}
}
}, [url, path])
useEffect(() => {
const load = async () => {
try {
const list = await TileDocument.create(
ceramic.client,
null,
{
controllers: [nftDID],
family: 'public video list',
deterministic: true,
},
{ anchor: false, publish: false },
)
const { videos } = list.content
setVideos(videos)
} catch(err) {
console.error('Loading', err)
}
}
if(nftDID) {
load()
}
}, [nftDID])
useEffect(() => {
(async () => {
if(metadata.startsWith('ceramic:')) {
const CERAMIC_URL = (
process.env.CERAMIC_URL
|| 'https://ceramic-clay.3boxlabs.com'
|| 'http:https://localhost:7007'
)
const ceramic = new Ceramic(CERAMIC_URL)
const tile = await (
TileDocument.load(ceramic, metadata)
)
setInfo(tile.content)
} else if(metadata.startsWith('ipfs:')) {
if(/\.json5?/i.test(metadata)) {
setFromObject(await load(metadata))
} else {
setInfo({
startsAt: new Date(),
stops: [{ title: 'Title' }],
source: metadata,
})
}
} else if(metadata !== '') {
setInfo(await import(`./${metadata}/js`))
}
})()
}, [metadata])
if(!info) {
return (
<Stack align="center" mt={10}>
<ConnectButton/>
{!nftDID && (
<Text>To browse videos published to the Ceramic Network, connect your wallet.</Text>
)}
{nftDID && !videos && (
<Text>
Searched{' '}
<ChakraLink
href={`${desiredChain.explorer}/address/${nftDID.split(/[:_]/)[5]}`}
isExternal
title={nftDID}
>
{nftDID.slice(0,30)}…{nftDID.slice(-10)}
</ChakraLink>
</Text>
)}
<Stack>
<Flex as="form">
<Input
placeholder="Metadata File (ipfs:https://)"
value={metaInput}
onChange={({ target: { value } }) => {
setMetaInput(value)
}}
/>
<Button
onClick={() => {
setMetadata(metaInput)
}}
>Load</Button>
</Flex>
{videos && (
<Table
sx={{
a: { borderBottom: '2px dashed' },
'a:hover': {
borderBottom: '2px solid',
},
th: { textAlign: 'center' },
}}
>
<Thead>
<Tr><Th colSpan={3}>
Videos Published To Ceramic
</Th></Tr>
<Tr>
<Th>Start Time</Th>
<Th>Lister</Th>
<Th>Video</Th>
</Tr>
</Thead>
<Tbody>
{videos.map(({
id, lister, startsAt, metadata, title,
}) => (
<Tr key={id}>
<Td>{isoStringFor(
new Date(startsAt)
)}</Td>
<Td title={lister}>
{lister.slice(0, 15)}…{lister.slice(-5)}
</Td>
<Td>
<Link to={metadata}>
{title}
</Link>
</Td>
</Tr>
))}
</Tbody>
</Table>
)}
</Stack>
{metadata && !videos && (
<Stack align="center">
<Heading size="sm">
Loading:{' '}
{metadata.slice(0, 15)}…{metadata.slice(metadata.lastIndexOf('/'))}
</Heading>
<Spinner/>
</Stack>
)}
<Button onClick={() => {
history.push('/new')
}}>Upload a Video</Button>
<Button onClick={() => {
setFromObject({
video: { startsAt:
isoStringFor(new Date(), { standard: true })
},
isNew: true,
})
}}>Track a Recording</Button>
<Flex
as="form" direction="column"
onSubmit={(evt) => {
evt.preventDefault()
setFromObject(
// @ts-ignore
JSON5.parse(
ifSet(evt.target.json.value) ?? '{}'
)
)
}}
>
<Textarea
name="json" placeholder="JSON5 Events Description"
w={600} h={60}
onKeyPress={(evt) => {
if(evt.key === 'Enter' && evt.ctrlKey) {
evt.preventDefault()
// @ts-ignore
evt.target.form.submit()
}
}}
></Textarea>
<Button type="submit">Load JSON5</Button>
</Flex>
</Stack>
)
}
const {
title, stops, source, startsAt = new Date(), isNew = false
} = info
return <MarkedVideo {...{
title, stops, source, startsAt, isNew, ipfs, IPFSButton,
}}/>
}