newIssueExperienceButton.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {css} from '@emotion/react';
  2. import styled from '@emotion/styled';
  3. import merge from 'lodash/merge';
  4. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  5. import {Button} from 'sentry/components/button';
  6. import {IconLab} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import ConfigStore from 'sentry/stores/configStore';
  9. import type {User} from 'sentry/types';
  10. import {useMutation} from 'sentry/utils/queryClient';
  11. import useApi from 'sentry/utils/useApi';
  12. import {useUser} from 'sentry/utils/useUser';
  13. type UpdateUserOptionsVariables = Partial<User['options']>;
  14. export function NewIssueExperienceButton() {
  15. const user = useUser();
  16. const api = useApi();
  17. const newIssueExperienceEnabled =
  18. user?.options?.issueDetailsNewExperienceQ42023 ?? false;
  19. const {mutate} = useMutation({
  20. mutationFn: (options: UpdateUserOptionsVariables) => {
  21. return api.requestPromise('/users/me/', {
  22. method: 'PUT',
  23. data: {
  24. options,
  25. },
  26. });
  27. },
  28. onMutate: (options: UpdateUserOptionsVariables) => {
  29. ConfigStore.set('user', merge({}, user, {options}));
  30. },
  31. onError: () => {
  32. addErrorMessage('Failed to save new issue experience preference');
  33. },
  34. });
  35. const label = newIssueExperienceEnabled
  36. ? t('Switch to the old issue experience')
  37. : t('Switch to the new issue experience');
  38. return (
  39. <StyledButton
  40. enabled={newIssueExperienceEnabled}
  41. size="sm"
  42. icon={<IconLab isSolid={newIssueExperienceEnabled} />}
  43. title={label}
  44. aria-label={label}
  45. onClick={() =>
  46. mutate({['issueDetailsNewExperienceQ42023']: !newIssueExperienceEnabled})
  47. }
  48. />
  49. );
  50. }
  51. const StyledButton = styled(Button)<{enabled: boolean}>`
  52. ${p =>
  53. p.enabled &&
  54. css`
  55. color: ${p.theme.button.primary.background};
  56. :hover {
  57. color: ${p.theme.button.primary.background};
  58. }
  59. `}
  60. `;