broadcasts.tsx 5.0 KB

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