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!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.5KB

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