newIssueExperienceButton.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 hasOnlyOneUIOption = defined(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 one interface (via the organization option).
  41. hasOnlyOneUIOption
  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. return (
  50. <ToggleButtonWrapper>
  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. borderless={!hasStreamlinedUI}
  77. onClick={handleToggle}
  78. >
  79. <ToggleBorder />
  80. </ToggleButton>
  81. </ToggleButtonWrapper>
  82. );
  83. }
  84. return (
  85. <DropdownMenu
  86. trigger={triggerProps => (
  87. <StyledDropdownButton
  88. {...triggerProps}
  89. enabled={hasStreamlinedUI}
  90. size={hasStreamlinedUI ? 'xs' : 'sm'}
  91. aria-label={t('Switch issue experience')}
  92. >
  93. {/* Passing icon as child to avoid extra icon margin */}
  94. <IconLab isSolid={hasStreamlinedUI} />
  95. </StyledDropdownButton>
  96. )}
  97. items={[
  98. {
  99. key: 'switch-to-old-ui',
  100. label: t('Switch to the old issue experience'),
  101. onAction: handleToggle,
  102. },
  103. {
  104. key: 'give-feedback',
  105. label: t('Give feedback on new UI'),
  106. hidden: !openForm,
  107. onAction: () => {
  108. openForm({
  109. messagePlaceholder: t(
  110. 'Excluding bribes, what would make you excited to use the new UI?'
  111. ),
  112. tags: {
  113. ['feedback.source']: 'streamlined_issue_details',
  114. ['feedback.owner']: 'issues',
  115. },
  116. });
  117. },
  118. },
  119. ]}
  120. position="bottom-end"
  121. />
  122. );
  123. }
  124. const StyledDropdownButton = styled(DropdownButton)<{enabled: boolean}>`
  125. color: ${p => (p.enabled ? p.theme.button.primary.background : 'inherit')};
  126. :hover {
  127. color: ${p => (p.enabled ? p.theme.button.primary.background : 'inherit')};
  128. }
  129. `;
  130. const ToggleButtonWrapper = styled('div')`
  131. overflow: hidden;
  132. margin: 0 -1px;
  133. border-radius: 7px;
  134. `;
  135. const ToggleButton = styled(Button)<{enabled: boolean}>`
  136. position: relative;
  137. color: ${p => (p.enabled ? p.theme.button.primary.background : 'inherit')};
  138. :hover {
  139. color: ${p => (p.enabled ? p.theme.button.primary.background : 'inherit')};
  140. }
  141. &:after {
  142. position: absolute;
  143. content: '';
  144. inset: 0;
  145. background: ${p => p.theme.background};
  146. border-radius: ${p => p.theme.borderRadius};
  147. }
  148. span {
  149. z-index: 1;
  150. margin: 0;
  151. }
  152. `;
  153. const ToggleBorder = styled('div')`
  154. @keyframes rotating {
  155. from {
  156. transform: rotate(0deg);
  157. }
  158. to {
  159. transform: rotate(360deg);
  160. }
  161. }
  162. position: absolute;
  163. content: '';
  164. z-index: -1;
  165. width: 46px;
  166. height: 46px;
  167. border-radius: 7px;
  168. background: ${p => p.theme.badge.beta.background};
  169. animation: rotating 10s linear infinite;
  170. `;