getSdkUpdateSuggestion.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import ExternalLink from 'sentry/components/links/externalLink';
  4. import List from 'sentry/components/list';
  5. import ListItem from 'sentry/components/list/listItem';
  6. import {t, tct} from 'sentry/locale';
  7. import space from 'sentry/styles/space';
  8. import {UpdateSdkSuggestion} from 'sentry/types';
  9. import {Event} from 'sentry/types/event';
  10. type Props = {
  11. sdk: Event['sdk'];
  12. suggestion: NonNullable<Event['sdkUpdates']>[0];
  13. capitalized?: boolean;
  14. shortStyle?: boolean;
  15. };
  16. function getSdkUpdateSuggestion({
  17. sdk,
  18. suggestion,
  19. shortStyle = false,
  20. capitalized = false,
  21. }: Props) {
  22. function getUpdateSdkContent({newSdkVersion, sdkName}: UpdateSdkSuggestion) {
  23. if (capitalized) {
  24. return sdk
  25. ? shortStyle
  26. ? tct('Update to [sdk-name]@v[new-sdk-version]', {
  27. 'sdk-name': sdkName,
  28. 'new-sdk-version': newSdkVersion,
  29. })
  30. : tct(
  31. 'Update your SDK from [sdk-name]@v[sdk-version] to [sdk-name]@v[new-sdk-version]',
  32. {
  33. 'sdk-name': sdkName,
  34. 'sdk-version': sdk.version,
  35. 'new-sdk-version': newSdkVersion,
  36. }
  37. )
  38. : t('Update your SDK version');
  39. }
  40. return sdk
  41. ? shortStyle
  42. ? tct('update to [sdk-name]@v[new-sdk-version]', {
  43. 'sdk-name': sdkName,
  44. 'new-sdk-version': newSdkVersion,
  45. })
  46. : tct(
  47. 'update your SDK from [sdk-name]@v[sdk-version] to [sdk-name]@v[new-sdk-version]',
  48. {
  49. 'sdk-name': sdkName,
  50. 'sdk-version': sdk.version,
  51. 'new-sdk-version': newSdkVersion,
  52. }
  53. )
  54. : t('update your SDK version');
  55. }
  56. const getTitleData = () => {
  57. switch (suggestion.type) {
  58. case 'updateSdk':
  59. return {
  60. href: suggestion?.sdkUrl,
  61. content: getUpdateSdkContent(suggestion),
  62. };
  63. case 'changeSdk':
  64. return {
  65. href: suggestion?.sdkUrl,
  66. content: capitalized
  67. ? tct('Migrate to [recommended-sdk-version]', {
  68. 'recommended-sdk-version': suggestion.newSdkName,
  69. })
  70. : tct('migrate to [recommended-sdk-version]', {
  71. 'recommended-sdk-version': suggestion.newSdkName,
  72. }),
  73. };
  74. case 'enableIntegration':
  75. return {
  76. href: suggestion?.integrationUrl,
  77. content: capitalized
  78. ? tct('Enable the [recommended-integration-name]', {
  79. 'recommended-integration-name': suggestion.integrationName,
  80. })
  81. : tct('enable the [recommended-integration-name] integration', {
  82. 'recommended-integration-name': suggestion.integrationName,
  83. }),
  84. };
  85. default:
  86. return null;
  87. }
  88. };
  89. const getTitle = () => {
  90. const titleData = getTitleData();
  91. if (!titleData) {
  92. return null;
  93. }
  94. const {href, content} = titleData;
  95. if (!href) {
  96. return content;
  97. }
  98. return <ExternalLink href={href}>{content}</ExternalLink>;
  99. };
  100. const title = <Fragment>{getTitle()}</Fragment>;
  101. if (!suggestion.enables.length) {
  102. return title;
  103. }
  104. const alertContent = suggestion.enables
  105. .map((subSuggestion, index) => {
  106. const subSuggestionContent = getSdkUpdateSuggestion({
  107. suggestion: subSuggestion,
  108. sdk,
  109. capitalized,
  110. });
  111. if (!subSuggestionContent) {
  112. return null;
  113. }
  114. return <ListItem key={index}>{subSuggestionContent}</ListItem>;
  115. })
  116. .filter(content => !!content);
  117. if (!alertContent.length) {
  118. return title;
  119. }
  120. return (
  121. <span>
  122. {tct('[title] so you can:', {title})}
  123. <StyledList symbol="bullet">{alertContent}</StyledList>
  124. </span>
  125. );
  126. }
  127. export default getSdkUpdateSuggestion;
  128. const StyledList = styled(List)`
  129. margin-top: ${space(1)};
  130. `;