LiveConnectionWarning.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { css } from '@emotion/css';
  2. import React, { PureComponent } from 'react';
  3. import { Unsubscribable } from 'rxjs';
  4. import { GrafanaTheme2 } from '@grafana/data';
  5. import { config, getGrafanaLiveSrv } from '@grafana/runtime';
  6. import { Alert, stylesFactory } from '@grafana/ui';
  7. import { contextSrv } from 'app/core/services/context_srv';
  8. export interface Props {}
  9. export interface State {
  10. show?: boolean;
  11. }
  12. export class LiveConnectionWarning extends PureComponent<Props, State> {
  13. subscription?: Unsubscribable;
  14. styles = getStyle(config.theme2);
  15. state: State = {};
  16. componentDidMount() {
  17. // Only show the error in development mode
  18. if (process.env.NODE_ENV === 'development') {
  19. // Wait a second to listen for server errors
  20. setTimeout(this.initListener, 1500);
  21. }
  22. }
  23. initListener = () => {
  24. const live = getGrafanaLiveSrv();
  25. if (live) {
  26. this.subscription = live.getConnectionState().subscribe({
  27. next: (v) => {
  28. this.setState({ show: !v });
  29. },
  30. });
  31. }
  32. };
  33. componentWillUnmount() {
  34. if (this.subscription) {
  35. this.subscription.unsubscribe();
  36. }
  37. }
  38. render() {
  39. const { show } = this.state;
  40. if (show) {
  41. if (!contextSrv.isSignedIn || !config.liveEnabled || contextSrv.user.orgRole === '') {
  42. return null; // do not show the warning for anonymous users or ones with no org (and /login page etc)
  43. }
  44. return (
  45. <div className={this.styles.foot}>
  46. <Alert severity={'warning'} className={this.styles.warn} title="connection to server is lost..." />
  47. </div>
  48. );
  49. }
  50. return null;
  51. }
  52. }
  53. const getStyle = stylesFactory((theme: GrafanaTheme2) => {
  54. return {
  55. foot: css`
  56. position: absolute;
  57. bottom: 0px;
  58. left: 0px;
  59. right: 0px;
  60. z-index: 10000;
  61. cursor: wait;
  62. margin: 16px;
  63. `,
  64. warn: css`
  65. border: 2px solid ${theme.colors.warning.main};
  66. max-width: 400px;
  67. margin: auto;
  68. height: 3em;
  69. `,
  70. };
  71. });