getSdkUpdateSuggestion.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import ExternalLink from 'app/components/links/externalLink';
  4. import {t, tct} from 'app/locale';
  5. import space from 'app/styles/space';
  6. import {Event} from 'app/types/event';
  7. type Props = {
  8. sdk: Event['sdk'];
  9. suggestion: NonNullable<Event['sdkUpdates']>[0];
  10. shortStyle?: boolean;
  11. };
  12. function getSdkUpdateSuggestion({sdk, suggestion, shortStyle = false}: Props) {
  13. const getTitleData = () => {
  14. switch (suggestion.type) {
  15. case 'updateSdk':
  16. return {
  17. href: suggestion?.sdkUrl,
  18. content: sdk
  19. ? shortStyle
  20. ? t('update to version %s', suggestion.newSdkVersion)
  21. : t(
  22. 'update your SDK from version %s to version %s',
  23. sdk.version,
  24. suggestion.newSdkVersion
  25. )
  26. : t('update your SDK version'),
  27. };
  28. case 'changeSdk':
  29. return {
  30. href: suggestion?.sdkUrl,
  31. content: tct('migrate to the [sdkName] SDK', {
  32. sdkName: <code>{suggestion.newSdkName}</code>,
  33. }),
  34. };
  35. case 'enableIntegration':
  36. return {
  37. href: suggestion?.integrationUrl,
  38. content: t("enable the '%s' integration", suggestion.integrationName),
  39. };
  40. default:
  41. return null;
  42. }
  43. };
  44. const getTitle = () => {
  45. const titleData = getTitleData();
  46. if (!titleData) {
  47. return null;
  48. }
  49. const {href, content} = titleData;
  50. if (!href) {
  51. return content;
  52. }
  53. return <ExternalLink href={href}>{content}</ExternalLink>;
  54. };
  55. const title = <Fragment>{getTitle()}</Fragment>;
  56. if (!suggestion.enables.length) {
  57. return title;
  58. }
  59. const alertContent = suggestion.enables
  60. .map((subSuggestion, index) => {
  61. const subSuggestionContent = getSdkUpdateSuggestion({
  62. suggestion: subSuggestion,
  63. sdk,
  64. });
  65. if (!subSuggestionContent) {
  66. return null;
  67. }
  68. return <Fragment key={index}>{subSuggestionContent}</Fragment>;
  69. })
  70. .filter(content => !!content);
  71. if (!alertContent.length) {
  72. return title;
  73. }
  74. return tct('[title] so you can: [suggestion]', {
  75. title,
  76. suggestion: <AlertUl>{alertContent}</AlertUl>,
  77. });
  78. }
  79. export default getSdkUpdateSuggestion;
  80. const AlertUl = styled('ul')`
  81. margin-top: ${space(1)};
  82. margin-bottom: ${space(1)};
  83. padding-left: 0 !important;
  84. `;