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://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 ( {!nftDID && ( To browse videos published to the Ceramic Network, connect your wallet. )} {nftDID && !videos && ( Searched{' '} {nftDID.slice(0,30)}…{nftDID.slice(-10)} )} { setMetaInput(value) }} /> {videos && ( {videos.map(({ id, lister, startsAt, metadata, title, }) => ( ))}
Videos Published To Ceramic
Start Time Lister Video
{isoStringFor( new Date(startsAt) )} {lister.slice(0, 15)}…{lister.slice(-5)} {title}
)}
{metadata && !videos && ( Loading:{' '} {metadata.slice(0, 15)}…{metadata.slice(metadata.lastIndexOf('/'))} )} { evt.preventDefault() setFromObject( // @ts-ignore JSON5.parse( ifSet(evt.target.json.value) ?? '{}' ) ) }} >
) } const { title, stops, source, startsAt = new Date(), isNew = false } = info return }