broadcasts.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import React from 'react';
  2. import {getAllBroadcasts, markBroadcastsAsSeen} from 'app/actionCreators/broadcasts';
  3. import {Client} from 'app/api';
  4. import DemoModeGate from 'app/components/acl/demoModeGate';
  5. import LoadingIndicator from 'app/components/loadingIndicator';
  6. import BroadcastSdkUpdates from 'app/components/sidebar/broadcastSdkUpdates';
  7. import SidebarItem from 'app/components/sidebar/sidebarItem';
  8. import SidebarPanel from 'app/components/sidebar/sidebarPanel';
  9. import SidebarPanelEmpty from 'app/components/sidebar/sidebarPanelEmpty';
  10. import SidebarPanelItem from 'app/components/sidebar/sidebarPanelItem';
  11. import {IconBroadcast} from 'app/icons';
  12. import {t} from 'app/locale';
  13. import {Broadcast, Organization} from 'app/types';
  14. import withApi from 'app/utils/withApi';
  15. import {CommonSidebarProps, SidebarPanelKey} from './types';
  16. const MARK_SEEN_DELAY = 1000;
  17. const POLLER_DELAY = 600000; // 10 minute poll (60 * 10 * 1000)
  18. type Props = CommonSidebarProps & {
  19. api: Client;
  20. organization: Organization;
  21. };
  22. type State = {
  23. broadcasts: Broadcast[];
  24. loading: boolean;
  25. error: boolean;
  26. };
  27. class Broadcasts extends React.Component<Props, State> {
  28. state: State = {
  29. broadcasts: [],
  30. loading: true,
  31. error: false,
  32. };
  33. componentDidMount() {
  34. this.fetchData();
  35. document.addEventListener('visibilitychange', this.handleVisibilityChange);
  36. }
  37. componentWillUnmount() {
  38. if (this.timer) {
  39. window.clearTimeout(this.timer);
  40. this.timer = null;
  41. }
  42. if (this.poller) {
  43. this.stopPoll();
  44. }
  45. document.removeEventListener('visibilitychange', this.handleVisibilityChange);
  46. }
  47. poller: number | null = null;
  48. timer: number | null = null;
  49. startPoll() {
  50. this.poller = window.setTimeout(this.fetchData, POLLER_DELAY);
  51. }
  52. stopPoll() {
  53. if (this.poller) {
  54. window.clearTimeout(this.poller);
  55. this.poller = null;
  56. }
  57. }
  58. fetchData = async () => {
  59. if (this.poller) {
  60. this.stopPoll();
  61. }
  62. try {
  63. const data = await getAllBroadcasts(this.props.api, this.props.organization.slug);
  64. this.setState({loading: false, broadcasts: data || []});
  65. } catch {
  66. this.setState({loading: false, error: true});
  67. }
  68. this.startPoll();
  69. };
  70. /**
  71. * If tab/window loses visibility (note: this is different than focus), stop
  72. * polling for broadcasts data, otherwise, if it gains visibility, start
  73. * polling again.
  74. */
  75. handleVisibilityChange = () => (document.hidden ? this.stopPoll() : this.startPoll());
  76. handleShowPanel = () => {
  77. this.timer = window.setTimeout(this.markSeen, MARK_SEEN_DELAY);
  78. this.props.onShowPanel();
  79. };
  80. markSeen = async () => {
  81. const unseenBroadcastIds = this.unseenIds;
  82. if (unseenBroadcastIds.length === 0) {
  83. return;
  84. }
  85. await markBroadcastsAsSeen(this.props.api, unseenBroadcastIds);
  86. this.setState(state => ({
  87. broadcasts: state.broadcasts.map(item => ({...item, hasSeen: true})),
  88. }));
  89. };
  90. get unseenIds() {
  91. return this.state.broadcasts
  92. ? this.state.broadcasts.filter(item => !item.hasSeen).map(item => item.id)
  93. : [];
  94. }
  95. render() {
  96. const {orientation, collapsed, currentPanel, hidePanel} = this.props;
  97. const {broadcasts, loading} = this.state;
  98. const unseenPosts = this.unseenIds;
  99. return (
  100. <DemoModeGate>
  101. <React.Fragment>
  102. <SidebarItem
  103. data-test-id="sidebar-broadcasts"
  104. orientation={orientation}
  105. collapsed={collapsed}
  106. active={currentPanel === SidebarPanelKey.Broadcasts}
  107. badge={unseenPosts.length}
  108. icon={<IconBroadcast size="md" />}
  109. label={t("What's new")}
  110. onClick={this.handleShowPanel}
  111. id="broadcasts"
  112. />
  113. {currentPanel === SidebarPanelKey.Broadcasts && (
  114. <SidebarPanel
  115. data-test-id="sidebar-broadcasts-panel"
  116. orientation={orientation}
  117. collapsed={collapsed}
  118. title={t("What's new in Sentry")}
  119. hidePanel={hidePanel}
  120. >
  121. <BroadcastSdkUpdates />
  122. {loading ? (
  123. <LoadingIndicator />
  124. ) : broadcasts.length === 0 ? (
  125. <SidebarPanelEmpty>
  126. {t('No recent updates from the Sentry team.')}
  127. </SidebarPanelEmpty>
  128. ) : (
  129. broadcasts.map(item => (
  130. <SidebarPanelItem
  131. key={item.id}
  132. hasSeen={item.hasSeen}
  133. title={item.title}
  134. message={item.message}
  135. link={item.link}
  136. cta={item.cta}
  137. />
  138. ))
  139. )}
  140. </SidebarPanel>
  141. )}
  142. </React.Fragment>
  143. </DemoModeGate>
  144. );
  145. }
  146. }
  147. export default withApi(Broadcasts);