IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

124 lines
3.3KB

  1. import { h, Component } from 'preact';
  2. import makeArvadosRequest from 'make-arvados-request';
  3. import urlForObject from 'url-for-object';
  4. import arvadosObjectName from 'arvados-object-name';
  5. import arvadosTypeName from 'arvados-type-name';
  6. class WBNameAndUuid extends Component {
  7. fetchData() {
  8. const { uuid, app, lookup } = this.props;
  9. if (!uuid)
  10. return;
  11. if (lookup && (uuid in lookup)) {
  12. this.setState({ 'item': lookup[uuid]});
  13. return;
  14. }
  15. const { arvHost, arvToken } = app.state;
  16. let prom = new Promise(accept => accept());
  17. if (/[0-9a-f]{32}\+[0-9]+/g.exec(uuid)) {
  18. let filters = [
  19. ['portable_data_hash', '=', uuid]
  20. ];
  21. prom = prom.then(() => makeArvadosRequest(arvHost, arvToken,
  22. '/arvados/v1/collections?filters=' +
  23. encodeURIComponent(JSON.stringify(filters))));
  24. prom = prom.then(xhr => {
  25. if (xhr.response.items.length === 0) {
  26. this.setState({
  27. 'item': {
  28. 'uuid': uuid,
  29. 'name': 'Collection with portable data hash ' + uuid
  30. }
  31. });
  32. return;
  33. }
  34. let item = xhr.response.items[0];
  35. if (xhr.response.items.length > 1)
  36. item.name += ' +' + (xhr.response.items.length - 1) + ' others';
  37. this.setState({ item });
  38. });
  39. } else if (/[a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}/.exec(uuid)) {
  40. let typeName = arvadosTypeName(uuid);
  41. const filters = [
  42. ['uuid', '=', uuid]
  43. ];
  44. let prom = makeArvadosRequest(arvHost, arvToken,
  45. '/arvados/v1/' + typeName +
  46. 's?filters=' + encodeURIComponent(JSON.stringify(filters)));
  47. prom = prom.then(xhr => {
  48. const item = xhr.response.items[0];
  49. if (!item)
  50. this.setState({ 'error': 'Item not found' });
  51. else
  52. this.setState({
  53. 'item': item
  54. });
  55. });
  56. prom = prom.catch(xhr => {
  57. this.setState({
  58. 'error': 'Unable to retrieve: ' + xhr.status + ' (' + xhr.statusText + ')'
  59. });
  60. });
  61. } else {
  62. this.setState({
  63. 'item': {
  64. 'uuid': uuid
  65. }
  66. });
  67. }
  68. }
  69. componentDidMount() {
  70. if (this.props.lazy)
  71. ;//this.setState({ item: { uuid: this.props.uuid }});
  72. else
  73. this.fetchData();
  74. }
  75. componentWillReceiveProps(nextProps) {
  76. if (this.props.uuid === nextProps.uuid)
  77. return;
  78. if (nextProps.lazy) {
  79. this.setState({ item: null });
  80. } else {
  81. this.props = nextProps;
  82. this.fetchData();
  83. }
  84. }
  85. render({ uuid, onLinkClicked, lazy }, { error, item }) {
  86. if (!uuid)
  87. return (
  88. <div><i>{ String(uuid) }</i></div>
  89. );
  90. return (
  91. <div>
  92. <div>
  93. { error ? error : (item ? (
  94. <a href={ urlForObject(item) } onclick={ onLinkClicked }>{ arvadosObjectName(item) }</a>
  95. ) : (lazy ? null : 'Loading...')) }
  96. </div>
  97. <div>
  98. { uuid } { (lazy && !item) ? (
  99. <a href="#" title="Look up" onclick={ e => { e.preventDefault(); this.fetchData(); } }>
  100. <i class="fas fa-search"></i>
  101. </a>
  102. ) : null }
  103. </div>
  104. </div>
  105. );
  106. }
  107. }
  108. export default WBNameAndUuid;