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.

94 lines
3.1KB

  1. import { h, Component } from 'preact';
  2. import WBIdTools from 'wb-id-tools';
  3. import urlForObject from 'url-for-object';
  4. import makeArvadosRequest from 'make-arvados-request';
  5. import arvadosObjectName from 'arvados-object-name';
  6. class WBLazyInlineName extends Component {
  7. componentWillReceiveProps(nextProps) {
  8. if (nextProps.identifier === this.props.identifier)
  9. return;
  10. this.setState({ item: null });
  11. }
  12. fetchData() {
  13. const { app, identifier } = this.props;
  14. const { arvHost, arvToken } = app.state;
  15. const typeName = WBIdTools.typeName(identifier);
  16. if (WBIdTools.isPDH(identifier)) {
  17. const filters = [
  18. [ 'portable_data_hash', '=', identifier ]
  19. ];
  20. let prom = makeArvadosRequest(arvHost, arvToken,
  21. '/arvados/v1/collections?filters=' + encodeURIComponent(JSON.stringify(filters)));
  22. prom = prom.then(xhr => this.setState({ item: {
  23. uuid: xhr.response.items.length > 0 ? xhr.response.items[0].uuid : '',
  24. name: xhr.response.items.length > 0 ? xhr.response.items[0].name : 'Not Found' +
  25. ( xhr.response.items_available > 1 ? ' (+' + (xhr.response.items_available - 1) + ' others)' : '' )
  26. }}));
  27. return;
  28. }
  29. let prom = makeArvadosRequest(arvHost, arvToken,
  30. '/arvados/v1/' + typeName + 's/' + identifier);
  31. prom = prom.then(xhr => this.setState({ item: xhr.response }));
  32. prom = prom.catch(() => this.setState({ item: { name: 'Not Found' }}));
  33. }
  34. render({ identifier }, { item }) {
  35. if (item) {
  36. return (
  37. <a href={ urlForObject(item) }>{ arvadosObjectName(item) }</a>
  38. );
  39. }
  40. const typeName = WBIdTools.typeName(identifier);
  41. const url = (typeName === 'group' ? '/browse/' + identifier :
  42. typeName === 'collection' ? '/collection-browse/' + identifier :
  43. urlForObject({ uuid: identifier }));
  44. return (
  45. <span>
  46. <a href={ url }>{ identifier }</a> <a href="#" title="Look up"
  47. onclick={ e => { e.preventDefault(); this.fetchData(); } }>
  48. <i class="fas fa-search"></i>
  49. </a>
  50. </span>
  51. );
  52. }
  53. }
  54. function detectIds(value, app) {
  55. const matches = WBIdTools.detectIdentifiers(value);
  56. matches.sort((a, b) => (a.index - b.index));
  57. const res = [];
  58. let ofs = 0;
  59. for (let i = 0; i < matches.length; i++) {
  60. const { index } = matches[i];
  61. const id = matches[i][0];
  62. const typeName = WBIdTools.typeName(id);
  63. const url = (typeName === 'group' ? '/browse/' + id :
  64. typeName === 'collection' ? '/collection-browse/' + id :
  65. urlForObject({ uuid: id }));
  66. res.push(value.substring(ofs, index));
  67. res.push(h(WBLazyInlineName, { identifier: id, app }, id));
  68. ofs = index + id.length;
  69. }
  70. res.push(value.substring(ofs));
  71. return res;
  72. }
  73. class WBJsonViewer extends Component {
  74. render({ value, stringify, app }) {
  75. if (stringify)
  76. value = JSON.stringify(value, null, 2);
  77. return (
  78. <div class="wb-json-viewer">{ detectIds(value, app) }</div>
  79. );
  80. }
  81. }
  82. WBJsonViewer.defaultProps = {
  83. stringify: true
  84. };
  85. export default WBJsonViewer;