globalSdkUpdateAlert.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import * as React from 'react';
  2. import styled from '@emotion/styled';
  3. import {promptsCheck, promptsUpdate} from 'sentry/actionCreators/prompts';
  4. import SidebarPanelActions from 'sentry/actions/sidebarPanelActions';
  5. import {Client} from 'sentry/api';
  6. import Alert from 'sentry/components/alert';
  7. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  8. import {IconUpgrade} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import space from 'sentry/styles/space';
  11. import {
  12. Organization,
  13. PageFilters,
  14. ProjectSdkUpdates,
  15. SDKUpdatesSuggestion,
  16. } from 'sentry/types';
  17. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  18. import {promptIsDismissed} from 'sentry/utils/promptIsDismissed';
  19. import withApi from 'sentry/utils/withApi';
  20. import withOrganization from 'sentry/utils/withOrganization';
  21. import withPageFilters from 'sentry/utils/withPageFilters';
  22. import withSdkUpdates from 'sentry/utils/withSdkUpdates';
  23. import {SidebarPanelKey} from './sidebar/types';
  24. import Button from './button';
  25. type Props = React.ComponentProps<typeof Alert> & {
  26. api: Client;
  27. organization: Organization;
  28. Wrapper?: React.ComponentType;
  29. sdkUpdates?: ProjectSdkUpdates[] | null;
  30. selection?: PageFilters;
  31. };
  32. type State = {
  33. isDismissed: boolean | null;
  34. };
  35. type AnalyticsOpts = {
  36. organization: Organization;
  37. };
  38. const recordAnalyticsSeen = ({organization}: AnalyticsOpts) =>
  39. trackAdvancedAnalyticsEvent('sdk_updates.seen', {organization});
  40. const recordAnalyticsSnoozed = ({organization}: AnalyticsOpts) =>
  41. trackAdvancedAnalyticsEvent('sdk_updates.snoozed', {organization});
  42. const recordAnalyticsClicked = ({organization}: AnalyticsOpts) =>
  43. trackAdvancedAnalyticsEvent('sdk_updates.clicked', {organization});
  44. const flattenSuggestions = (list: ProjectSdkUpdates[]) =>
  45. list.reduce<SDKUpdatesSuggestion[]>(
  46. (suggestions, sdk) => [...suggestions, ...sdk.suggestions],
  47. []
  48. );
  49. class InnerGlobalSdkSuggestions extends React.Component<Props, State> {
  50. state: State = {
  51. isDismissed: null,
  52. };
  53. componentDidMount() {
  54. this.promptsCheck();
  55. recordAnalyticsSeen({organization: this.props.organization});
  56. }
  57. async promptsCheck() {
  58. const {api, organization} = this.props;
  59. const prompt = await promptsCheck(api, {
  60. organizationId: organization.id,
  61. feature: 'sdk_updates',
  62. });
  63. this.setState({
  64. isDismissed: promptIsDismissed(prompt),
  65. });
  66. }
  67. snoozePrompt = () => {
  68. const {api, organization} = this.props;
  69. promptsUpdate(api, {
  70. organizationId: organization.id,
  71. feature: 'sdk_updates',
  72. status: 'snoozed',
  73. });
  74. this.setState({isDismissed: true});
  75. recordAnalyticsSnoozed({organization: this.props.organization});
  76. };
  77. render() {
  78. const {
  79. api: _api,
  80. selection,
  81. sdkUpdates,
  82. organization,
  83. Wrapper,
  84. ...props
  85. } = this.props;
  86. const {isDismissed} = this.state;
  87. if (!sdkUpdates || isDismissed === null || isDismissed) {
  88. return null;
  89. }
  90. // withSdkUpdates explicitly only queries My Projects. This means that when
  91. // looking at any projects outside of My Projects (like All Projects), this
  92. // will only show the updates relevant to the to user.
  93. const projectSpecificUpdates =
  94. selection?.projects.length === 0 || selection?.projects === [ALL_ACCESS_PROJECTS]
  95. ? sdkUpdates
  96. : sdkUpdates.filter(update =>
  97. selection?.projects?.includes(parseInt(update.projectId, 10))
  98. );
  99. // Are there any updates?
  100. if (flattenSuggestions(projectSpecificUpdates).length === 0) {
  101. return null;
  102. }
  103. const showBroadcastsPanel = (
  104. <Button
  105. priority="link"
  106. size="zero"
  107. onClick={() => {
  108. SidebarPanelActions.activatePanel(SidebarPanelKey.Broadcasts);
  109. recordAnalyticsClicked({organization});
  110. }}
  111. >
  112. {t('Review updates')}
  113. </Button>
  114. );
  115. const notice = (
  116. <Alert type="info" icon={<IconUpgrade />} {...props}>
  117. <Content>
  118. {t(
  119. `You have outdated SDKs in your projects. Update them for important fixes and features.`
  120. )}
  121. <Actions>
  122. <Button
  123. priority="link"
  124. size="zero"
  125. title={t('Dismiss for the next two weeks')}
  126. onClick={this.snoozePrompt}
  127. >
  128. {t('Remind me later')}
  129. </Button>
  130. |{showBroadcastsPanel}
  131. </Actions>
  132. </Content>
  133. </Alert>
  134. );
  135. return Wrapper ? <Wrapper>{notice}</Wrapper> : notice;
  136. }
  137. }
  138. const Content = styled('div')`
  139. display: flex;
  140. flex-wrap: wrap;
  141. @media (min-width: ${p => p.theme.breakpoints[0]}) {
  142. justify-content: space-between;
  143. }
  144. `;
  145. const Actions = styled('div')`
  146. display: grid;
  147. grid-template-columns: repeat(3, max-content);
  148. gap: ${space(1)};
  149. `;
  150. const GlobalSdkSuggestions = withOrganization(
  151. withSdkUpdates(withPageFilters(withApi(InnerGlobalSdkSuggestions)))
  152. );
  153. export default GlobalSdkSuggestions;