Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

IssueNewStatusAndTransitionGuide.tsx 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { SpotlightTour, SpotlightTourStep } from 'design-system';
  21. import React, { useState } from 'react';
  22. import { useIntl } from 'react-intl';
  23. import { CallBackProps } from 'react-joyride';
  24. import { useCurrentUser } from '../../../app/components/current-user/CurrentUserContext';
  25. import DocumentationLink from '../../../components/common/DocumentationLink';
  26. import { SCREEN_POSITION_COMPUTE_DELAY } from '../../../components/common/ScreenPositionHelper';
  27. import { useDismissNoticeMutation } from '../../../queries/users';
  28. import { IssueTransition } from '../../../types/issues';
  29. import { Issue } from '../../../types/types';
  30. import { NoticeType } from '../../../types/users';
  31. interface Props {
  32. run?: boolean;
  33. togglePopup: (issue: string, popup: string, show?: boolean) => void;
  34. issues: Issue[];
  35. }
  36. const PLACEMENT_RIGHT = 'right';
  37. const DOC_LINK = '/user-guide/issues/#statuses';
  38. export const SESSION_STORAGE_TRANSITION_GUIDE_KEY = 'issueNewStatusAndTransitionGuideStep';
  39. const EXTRA_DELAY = 100;
  40. const GUIDE_WIDTH = 360;
  41. export default function IssueNewStatusAndTransitionGuide(props: Readonly<Props>) {
  42. const { run, issues, togglePopup } = props;
  43. const { currentUser, updateDismissedNotices } = useCurrentUser();
  44. const { mutateAsync: dismissNotice } = useDismissNoticeMutation();
  45. const intl = useIntl();
  46. const [step, setStep] = useState(
  47. +(sessionStorage.getItem(SESSION_STORAGE_TRANSITION_GUIDE_KEY) ?? 0),
  48. );
  49. const [start, setStart] = React.useState(false);
  50. const issueWithAcceptTransition = issues.find((issue) =>
  51. issue.transitions.includes(IssueTransition.Accept),
  52. );
  53. const userCompletedCCTGuide =
  54. currentUser.isLoggedIn && currentUser.dismissedNotices[NoticeType.ISSUE_GUIDE];
  55. const userCompletedStatusGuide =
  56. currentUser.isLoggedIn &&
  57. currentUser.dismissedNotices[NoticeType.ISSUE_NEW_STATUS_AND_TRANSITION_GUIDE];
  58. const canRun =
  59. userCompletedCCTGuide && !userCompletedStatusGuide && run && issueWithAcceptTransition;
  60. // Wait for the issue list to be rendered, then scroll to the issue, wait for an extra delay
  61. // to ensure proper positioning of the SpotlightTour in the context of ScreenPositionHelper,
  62. // then start the tour.
  63. React.useEffect(() => {
  64. // Should start the tour if it is not started yet
  65. if (!start && canRun) {
  66. setTimeout(() => {
  67. // Scroll to issue. This ensures proper rendering of the SpotlightTour.
  68. document
  69. .querySelector(`[data-guiding-id="issue-transition-${issueWithAcceptTransition.key}"]`)
  70. ?.scrollIntoView({ behavior: 'instant', block: 'center' });
  71. // Start the tour
  72. if (step !== 0) {
  73. togglePopup(issueWithAcceptTransition.key, 'transition', true);
  74. setTimeout(() => {
  75. setStart(run);
  76. }, 0);
  77. } else {
  78. setStart(run);
  79. }
  80. }, SCREEN_POSITION_COMPUTE_DELAY + EXTRA_DELAY);
  81. }
  82. }, [canRun, run, step, start, togglePopup, issueWithAcceptTransition]);
  83. React.useEffect(() => {
  84. if (start && canRun) {
  85. sessionStorage.setItem(SESSION_STORAGE_TRANSITION_GUIDE_KEY, step.toString());
  86. }
  87. }, [step, start, canRun]);
  88. if (!canRun || !start) {
  89. return null;
  90. }
  91. const onToggle = (props: CallBackProps) => {
  92. const { action, lifecycle, index } = props;
  93. switch (action) {
  94. case 'close':
  95. case 'skip':
  96. case 'reset':
  97. togglePopup(issueWithAcceptTransition.key, 'transition', false);
  98. sessionStorage.removeItem(SESSION_STORAGE_TRANSITION_GUIDE_KEY);
  99. dismissNotice(NoticeType.ISSUE_NEW_STATUS_AND_TRANSITION_GUIDE)
  100. .then(() => {
  101. updateDismissedNotices(NoticeType.ISSUE_NEW_STATUS_AND_TRANSITION_GUIDE, true);
  102. })
  103. .catch(() => {
  104. /* noop */
  105. });
  106. break;
  107. case 'next':
  108. if (lifecycle === 'complete') {
  109. if (index === 0) {
  110. togglePopup(issueWithAcceptTransition.key, 'transition', true);
  111. setTimeout(() => {
  112. setStep(step + 1);
  113. }, 0);
  114. } else {
  115. setStep(step + 1);
  116. }
  117. }
  118. break;
  119. case 'prev':
  120. if (lifecycle === 'complete') {
  121. if (index === 1) {
  122. togglePopup(issueWithAcceptTransition.key, 'transition', false);
  123. }
  124. setStep(step - 1);
  125. }
  126. break;
  127. default:
  128. break;
  129. }
  130. };
  131. const constructContent = (stepIndex: number) => {
  132. return (
  133. <>
  134. <div className="sw-flex sw-flex-col sw-gap-4">
  135. <span>{intl.formatMessage({ id: `guiding.issue_accept.${stepIndex}.content.1` })}</span>
  136. <span>{intl.formatMessage({ id: `guiding.issue_accept.${stepIndex}.content.2` })}</span>
  137. </div>
  138. <DocumentationLink to={DOC_LINK} className="sw-mt-1 sw-inline-block">
  139. {intl.formatMessage({ id: `guiding.issue_accept.${stepIndex}.content.link` })}
  140. </DocumentationLink>
  141. </>
  142. );
  143. };
  144. const steps: SpotlightTourStep[] = [
  145. {
  146. target: `[data-guiding-id="issue-transition-${issueWithAcceptTransition.key}"]`,
  147. title: intl.formatMessage({ id: 'guiding.issue_accept.1.title' }),
  148. content: intl.formatMessage({ id: 'guiding.issue_accept.1.content.1' }),
  149. placement: PLACEMENT_RIGHT,
  150. },
  151. {
  152. target: '[data-guiding-id="issue-accept-transition"]',
  153. title: intl.formatMessage({ id: 'guiding.issue_accept.2.title' }),
  154. content: constructContent(2),
  155. placement: PLACEMENT_RIGHT,
  156. },
  157. {
  158. target: '[data-guiding-id="issue-deprecated-transitions"]',
  159. title: intl.formatMessage({ id: 'guiding.issue_accept.3.title' }),
  160. content: constructContent(3),
  161. placement: PLACEMENT_RIGHT,
  162. },
  163. ];
  164. return (
  165. <SpotlightTour
  166. width={GUIDE_WIDTH}
  167. callback={onToggle}
  168. steps={steps}
  169. stepIndex={step}
  170. run={run}
  171. continuous
  172. skipLabel={intl.formatMessage({ id: 'skip' })}
  173. backLabel={intl.formatMessage({ id: 'go_back' })}
  174. closeLabel={intl.formatMessage({ id: 'close' })}
  175. nextLabel={intl.formatMessage({ id: 'next' })}
  176. stepXofYLabel={(x: number, y: number) =>
  177. intl.formatMessage({ id: 'guiding.step_x_of_y' }, { '0': x, '1': y })
  178. }
  179. />
  180. );
  181. }