newIssueExperienceButton.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import {useCallback} from 'react';
  2. import styled from '@emotion/styled';
  3. import {motion} from 'framer-motion';
  4. import {Button} from 'sentry/components/button';
  5. import DropdownButton from 'sentry/components/dropdownButton';
  6. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  7. import {IconLab} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import {defined} from 'sentry/utils';
  10. import {trackAnalytics} from 'sentry/utils/analytics';
  11. import {useFeedbackForm} from 'sentry/utils/useFeedbackForm';
  12. import useMutateUserOptions from 'sentry/utils/useMutateUserOptions';
  13. import useOrganization from 'sentry/utils/useOrganization';
  14. import {useUser} from 'sentry/utils/useUser';
  15. import {useHasStreamlinedUI} from 'sentry/views/issueDetails/utils';
  16. export function NewIssueExperienceButton() {
  17. const user = useUser();
  18. const organization = useOrganization();
  19. const hasStreamlinedUIFlag = organization.features.includes('issue-details-streamline');
  20. const hasEnforceStreamlinedUIFlag = organization.features.includes(
  21. 'issue-details-streamline-enforce'
  22. );
  23. const hasNewUIOnly = organization.streamlineOnly;
  24. const hasStreamlinedUI = useHasStreamlinedUI();
  25. const openForm = useFeedbackForm();
  26. const {mutate} = useMutateUserOptions();
  27. const handleToggle = useCallback(() => {
  28. mutate({['prefersIssueDetailsStreamlinedUI']: !hasStreamlinedUI});
  29. trackAnalytics('issue_details.streamline_ui_toggle', {
  30. isEnabled: !hasStreamlinedUI,
  31. organization,
  32. });
  33. }, [mutate, organization, hasStreamlinedUI]);
  34. // We hide the toggle if the org...
  35. if (
  36. // doesn't have the 'opt-in' flag,
  37. !hasStreamlinedUIFlag ||
  38. // has the 'remove opt-out' flag,
  39. hasEnforceStreamlinedUIFlag ||
  40. // has access to only the updated experience through the experiment
  41. hasNewUIOnly
  42. ) {
  43. return null;
  44. }
  45. if (!openForm || !hasStreamlinedUI) {
  46. const label = hasStreamlinedUI
  47. ? t('Switch to the old issue experience')
  48. : t('Switch to the new issue experience');
  49. const text = hasStreamlinedUI ? null : t('Try New UI');
  50. return (
  51. <ToggleButton
  52. enabled={hasStreamlinedUI}
  53. size={hasStreamlinedUI ? 'xs' : 'sm'}
  54. icon={
  55. defined(user?.options?.prefersIssueDetailsStreamlinedUI) ? (
  56. <IconLab isSolid={hasStreamlinedUI} />
  57. ) : (
  58. <motion.div
  59. style={{height: 14}}
  60. animate={{
  61. rotate: [null, 6, -6, 12, -12, 6, -6, 0],
  62. }}
  63. transition={{
  64. duration: 1,
  65. delay: 1,
  66. repeatDelay: 3,
  67. repeat: 3,
  68. }}
  69. >
  70. <IconLab isSolid={hasStreamlinedUI} />
  71. </motion.div>
  72. )
  73. }
  74. title={label}
  75. aria-label={label}
  76. onClick={handleToggle}
  77. >
  78. {text}
  79. </ToggleButton>
  80. );
  81. }
  82. return (
  83. <DropdownMenu
  84. trigger={triggerProps => (
  85. <StyledDropdownButton
  86. {...triggerProps}
  87. enabled={hasStreamlinedUI}
  88. size={hasStreamlinedUI ? 'xs' : 'sm'}
  89. aria-label={t('Switch issue experience')}
  90. >
  91. {/* Passing icon as child to avoid extra icon margin */}
  92. <IconLab isSolid={hasStreamlinedUI} />
  93. </StyledDropdownButton>
  94. )}
  95. items={[
  96. {
  97. key: 'switch-to-old-ui',
  98. label: t('Switch to the old issue experience'),
  99. onAction: handleToggle,
  100. },
  101. {
  102. key: 'give-feedback',
  103. label: t('Give feedback on new UI'),
  104. hidden: !openForm,
  105. onAction: () => {
  106. openForm({
  107. messagePlaceholder: t(
  108. 'Excluding bribes, what would make you excited to use the new UI?'
  109. ),
  110. tags: {
  111. ['feedback.source']: 'streamlined_issue_details',
  112. ['feedback.owner']: 'issues',
  113. },
  114. });
  115. },
  116. },
  117. ]}
  118. position="bottom-end"
  119. />
  120. );
  121. }
  122. const StyledDropdownButton = styled(DropdownButton)<{enabled: boolean}>`
  123. color: ${p => (p.enabled ? p.theme.button.primary.background : 'inherit')};
  124. :hover {
  125. color: ${p => (p.enabled ? p.theme.button.primary.background : 'inherit')};
  126. }
  127. `;
  128. const ToggleButton = styled(Button)<{enabled: boolean}>`
  129. color: ${p => (p.enabled ? p.theme.button.primary.background : p.theme.white)};
  130. background: ${p =>
  131. p.enabled ? 'inherit' : `linear-gradient(90deg, #3468D8, #248574)`};
  132. :hover {
  133. color: ${p => (p.enabled ? p.theme.button.primary.background : p.theme.white)};
  134. }
  135. `;