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.

88 lines
3.0KB

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