newIssueExperienceButton.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 hasStreamlinedUI = useHasStreamlinedUI();
  24. const openForm = useFeedbackForm();
  25. const {mutate} = useMutateUserOptions();
  26. const handleToggle = useCallback(() => {
  27. mutate({['prefersIssueDetailsStreamlinedUI']: !hasStreamlinedUI});
  28. trackAnalytics('issue_details.streamline_ui_toggle', {
  29. isEnabled: !hasStreamlinedUI,
  30. organization,
  31. });
  32. }, [mutate, organization, hasStreamlinedUI]);
  33. // We hide the toggle if the org doesn't have the 'opt-in' flag, or has the 'remove opt-out' flag.
  34. if (!hasStreamlinedUIFlag || hasEnforceStreamlinedUIFlag) {
  35. return null;
  36. }
  37. if (!openForm || !hasStreamlinedUI) {
  38. const label = hasStreamlinedUI
  39. ? t('Switch to the old issue experience')
  40. : t('Switch to the new issue experience');
  41. return (
  42. <ToggleButtonWrapper>
  43. <ToggleButton
  44. enabled={hasStreamlinedUI}
  45. size={hasStreamlinedUI ? 'xs' : 'sm'}
  46. icon={
  47. defined(user?.options?.prefersIssueDetailsStreamlinedUI) ? (
  48. <IconLab isSolid={hasStreamlinedUI} />
  49. ) : (
  50. <motion.div
  51. style={{height: 14}}
  52. animate={{
  53. rotate: [null, 6, -6, 12, -12, 6, -6, 0],
  54. }}
  55. transition={{
  56. duration: 1,
  57. delay: 1,
  58. repeatDelay: 3,
  59. repeat: 3,
  60. }}
  61. >
  62. <IconLab isSolid={hasStreamlinedUI} />
  63. </motion.div>
  64. )
  65. }
  66. title={label}
  67. aria-label={label}
  68. borderless={!hasStreamlinedUI}
  69. onClick={handleToggle}
  70. >
  71. <ToggleBorder />
  72. </ToggleButton>
  73. </ToggleButtonWrapper>
  74. );
  75. }
  76. return (
  77. <DropdownMenu
  78. trigger={triggerProps => (
  79. <StyledDropdownButton
  80. {...triggerProps}
  81. enabled={hasStreamlinedUI}
  82. size={hasStreamlinedUI ? 'xs' : 'sm'}
  83. aria-label={t('Switch issue experience')}
  84. >
  85. {/* Passing icon as child to avoid extra icon margin */}
  86. <IconLab isSolid={hasStreamlinedUI} />
  87. </StyledDropdownButton>
  88. )}
  89. items={[
  90. {
  91. key: 'switch-to-old-ui',
  92. label: t('Switch to the old issue experience'),
  93. onAction: handleToggle,
  94. },
  95. {
  96. key: 'give-feedback',
  97. label: t('Give feedback on new UI'),
  98. hidden: !openForm,
  99. onAction: () => {
  100. openForm({
  101. messagePlaceholder: t(
  102. 'Excluding bribes, what would make you excited to use the new UI?'
  103. ),
  104. tags: {
  105. ['feedback.source']: 'streamlined_issue_details',
  106. ['feedback.owner']: 'issues',
  107. },
  108. });
  109. },
  110. },
  111. ]}
  112. position="bottom-end"
  113. />
  114. );
  115. }
  116. const StyledDropdownButton = styled(DropdownButton)<{enabled: boolean}>`
  117. color: ${p => (p.enabled ? p.theme.button.primary.background : 'inherit')};
  118. :hover {
  119. color: ${p => (p.enabled ? p.theme.button.primary.background : 'inherit')};
  120. }
  121. `;
  122. const ToggleButtonWrapper = styled('div')`
  123. overflow: hidden;
  124. margin: 0 -1px;
  125. border-radius: 7px;
  126. `;
  127. const ToggleButton = styled(Button)<{enabled: boolean}>`
  128. position: relative;
  129. color: ${p => (p.enabled ? p.theme.button.primary.background : 'inherit')};
  130. :hover {
  131. color: ${p => (p.enabled ? p.theme.button.primary.background : 'inherit')};
  132. }
  133. &:after {
  134. position: absolute;
  135. content: '';
  136. inset: 0;
  137. background: ${p => p.theme.background};
  138. border-radius: ${p => p.theme.borderRadius};
  139. }
  140. span {
  141. z-index: 1;
  142. margin: 0;
  143. }
  144. `;
  145. const ToggleBorder = styled('div')`
  146. @keyframes rotating {
  147. from {
  148. transform: rotate(0deg);
  149. }
  150. to {
  151. transform: rotate(360deg);
  152. }
  153. }
  154. position: absolute;
  155. content: '';
  156. z-index: -1;
  157. width: 46px;
  158. height: 46px;
  159. border-radius: 7px;
  160. background: ${p => p.theme.badge.beta.background};
  161. animation: rotating 10s linear infinite;
  162. `;