|
- 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 {
- 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 (
- <a href={ urlForObject(item) }>{ arvadosObjectName(item) }</a>
- );
- }
-
- const typeName = WBIdTools.typeName(identifier);
- const url = (typeName === 'group' ? '/browse/' + identifier :
- typeName === 'collection' ? '/collection-browse/' + identifier :
- urlForObject({ uuid: identifier }));
- return (
- <span>
- <a href={ url }>{ identifier }</a> <a href="#" title="Look up"
- onclick={ e => { e.preventDefault(); this.fetchData(); } }>
- <i class="fas fa-search"></i>
- </a>
- </span>
- );
- }
- }
-
- 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 (
- <div class="wb-json-viewer">{ detectIds(value, app) }</div>
- );
- }
- }
-
- WBJsonViewer.defaultProps = {
- stringify: true
- };
-
- export default WBJsonViewer;
|