import { h, Component } from 'preact'; import WBIdTools from 'wb-id-tools'; import urlForObject from 'url-for-object'; import makeArvadosRequest from 'make-arvados-request'; import arvadosObjectName from 'arvados-object-name'; class WBLazyInlineName extends Component { componentWillReceiveProps(nextProps) { if (nextProps.identifier === this.props.identifier) return; this.setState({ item: null }); } fetchData() { const { app, identifier } = this.props; const { arvHost, arvToken } = app.state; const typeName = WBIdTools.typeName(identifier); if (WBIdTools.isPDH(identifier)) { const filters = [ [ 'portable_data_hash', '=', identifier ] ]; let prom = makeArvadosRequest(arvHost, arvToken, '/arvados/v1/collections?filters=' + encodeURIComponent(JSON.stringify(filters))); prom = prom.then(xhr => this.setState({ item: { uuid: xhr.response.items.length > 0 ? xhr.response.items[0].uuid : '', name: xhr.response.items.length > 0 ? xhr.response.items[0].name : 'Not Found' + ( xhr.response.items_available > 1 ? ' (+' + (xhr.response.items_available - 1) + ' others)' : '' ) }})); return; } let prom = makeArvadosRequest(arvHost, arvToken, '/arvados/v1/' + typeName + 's/' + identifier); prom = prom.then(xhr => this.setState({ item: xhr.response })); prom = prom.catch(() => this.setState({ item: { name: 'Not Found' }})); } render({ identifier }, { item }) { if (item) { return ( { arvadosObjectName(item) } ); } const typeName = WBIdTools.typeName(identifier); const url = (typeName === 'group' ? '/browse/' + identifier : typeName === 'collection' ? '/collection-browse/' + identifier : urlForObject({ uuid: identifier })); return ( { identifier } { e.preventDefault(); this.fetchData(); } }> ); } } function detectIds(value, app) { const matches = WBIdTools.detectIdentifiers(value); matches.sort((a, b) => (a.index - b.index)); const res = []; let ofs = 0; for (let i = 0; i < matches.length; i++) { const { index } = matches[i]; const id = matches[i][0]; const typeName = WBIdTools.typeName(id); const url = (typeName === 'group' ? '/browse/' + id : typeName === 'collection' ? '/collection-browse/' + id : urlForObject({ uuid: id })); res.push(value.substring(ofs, index)); res.push(h(WBLazyInlineName, { identifier: id, app }, id)); ofs = index + id.length; } res.push(value.substring(ofs)); return res; } class WBJsonViewer extends Component { render({ value, stringify, app }) { if (stringify) value = JSON.stringify(value, null, 2); return (
{ detectIds(value, app) }
); } } WBJsonViewer.defaultProps = { stringify: true }; export default WBJsonViewer;