toast.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * Adapted from LiteMol (c) David Sehnal
  5. *
  6. * @author David Sehnal <david.sehnal@gmail.com>
  7. */
  8. import * as React from 'react';
  9. import { PluginUIComponent } from './base';
  10. import { PluginToastManager } from '../mol-plugin/util/toast';
  11. import { IconButton } from './controls/common';
  12. import Cancel from '@material-ui/icons/Cancel';
  13. class ToastEntry extends PluginUIComponent<{ entry: PluginToastManager.Entry }> {
  14. private hide = () => {
  15. let entry = this.props.entry;
  16. (entry.hide || function () { }).call(null);
  17. };
  18. render() {
  19. let entry = this.props.entry;
  20. let message = typeof entry.message === 'string'
  21. ? <div dangerouslySetInnerHTML={{ __html: entry.message }} />
  22. : <div><entry.message /></div>;
  23. return <div className='msp-toast-entry'>
  24. <div className='msp-toast-title' onClick={() => this.hide()}>
  25. {entry.title}
  26. </div>
  27. <div className='msp-toast-message'>
  28. {message}
  29. </div>
  30. <div className='msp-toast-clear'></div>
  31. <div className='msp-toast-hide'>
  32. <IconButton svg={Cancel} onClick={this.hide} title='Hide' className='msp-no-hover-outline' />
  33. </div>
  34. </div>;
  35. }
  36. }
  37. export class Toasts extends PluginUIComponent {
  38. componentDidMount() {
  39. this.subscribe(this.plugin.managers.toast.events.changed, () => this.forceUpdate());
  40. }
  41. render() {
  42. const state = this.plugin.managers.toast.state;
  43. if (!state.entries.count()) return null;
  44. const entries: PluginToastManager.Entry[] = [];
  45. state.entries.forEach((t, k) => entries.push(t!));
  46. entries.sort(function (x, y) { return x.serialNumber - y.serialNumber; });
  47. return <div className='msp-toast-container'>
  48. {entries.map(e => <ToastEntry key={e.serialNumber} entry={e} />)}
  49. </div>;
  50. }
  51. }