|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import { h, Component } from 'preact';
- import makeArvadosRequest from 'make-arvados-request';
- import urlForObject from 'url-for-object';
- import arvadosObjectName from 'arvados-object-name';
- import arvadosTypeName from 'arvados-type-name';
-
- class WBNameAndUuid extends Component {
- fetchData() {
- const { uuid, app, lookup } = this.props;
-
- if (!uuid)
- return;
-
- if (lookup && (uuid in lookup)) {
- this.setState({ 'item': lookup[uuid]});
- return;
- }
-
- const { arvHost, arvToken } = app.state;
-
- let prom = new Promise(accept => accept());
-
- if (/[0-9a-f]{32}\+[0-9]+/g.exec(uuid)) {
- let filters = [
- ['portable_data_hash', '=', uuid]
- ];
- prom = prom.then(() => makeArvadosRequest(arvHost, arvToken,
- '/arvados/v1/collections?filters=' +
- encodeURIComponent(JSON.stringify(filters))));
- prom = prom.then(xhr => {
- if (xhr.response.items.length === 0) {
- this.setState({
- 'item': {
- 'uuid': uuid,
- 'name': 'Collection with portable data hash ' + uuid
- }
- });
- return;
- }
- let item = xhr.response.items[0];
- if (xhr.response.items.length > 1)
- item.name += ' +' + (xhr.response.items.length - 1) + ' others';
- this.setState({ item });
- });
-
- } else if (/[a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}/.exec(uuid)) {
- let typeName = arvadosTypeName(uuid);
- const filters = [
- ['uuid', '=', uuid]
- ];
- let prom = makeArvadosRequest(arvHost, arvToken,
- '/arvados/v1/' + typeName +
- 's?filters=' + encodeURIComponent(JSON.stringify(filters)));
- prom = prom.then(xhr => {
- const item = xhr.response.items[0];
- if (!item)
- this.setState({ 'error': 'Item not found' });
- else
- this.setState({
- 'item': item
- });
- });
- prom = prom.catch(xhr => {
- this.setState({
- 'error': 'Unable to retrieve: ' + xhr.status + ' (' + xhr.statusText + ')'
- });
- });
-
- } else {
- this.setState({
- 'item': {
- 'uuid': uuid
- }
- });
- }
- }
-
- componentDidMount() {
- this.fetchData();
- }
-
- componentWillReceiveProps(nextProps) {
- if (this.props.uuid !== nextProps.uuid) {
- this.props = nextProps;
- this.fetchData();
- }
- }
-
- render({ uuid, onLinkClicked }, { error, item }) {
- if (!uuid)
- return (
- <div><i>{ String(uuid) }</i></div>
- );
-
- return (
- <div>
- <div>
- { error ? error : (item ? (
- <a href={ urlForObject(item) } onclick={ onLinkClicked }>{ arvadosObjectName(item) }</a>
- ) : 'Loading...') }
- </div>
- <div>
- { uuid }
- </div>
- </div>
- );
- }
- }
-
- export default WBNameAndUuid;
|