sidebar.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import {useCallback, useEffect, useMemo, useRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import {AnimatePresence, motion} from 'framer-motion';
  4. import HighlightTopRight from 'sentry-images/pattern/highlight-top-right.svg';
  5. import {updateOnboardingTask} from 'sentry/actionCreators/onboardingTasks';
  6. import SidebarPanel from 'sentry/components/sidebar/sidebarPanel';
  7. import {CommonSidebarProps} from 'sentry/components/sidebar/types';
  8. import Tooltip from 'sentry/components/tooltip';
  9. import {t} from 'sentry/locale';
  10. import space from 'sentry/styles/space';
  11. import {OnboardingTask, OnboardingTaskKey, Project} from 'sentry/types';
  12. import testableTransition from 'sentry/utils/testableTransition';
  13. import useApi from 'sentry/utils/useApi';
  14. import useOrganization from 'sentry/utils/useOrganization';
  15. import withProjects from 'sentry/utils/withProjects';
  16. import {usePersistedOnboardingState} from 'sentry/views/onboarding/utils';
  17. import ProgressHeader from './progressHeader';
  18. import Task from './task';
  19. import {getMergedTasks} from './taskConfig';
  20. import {findActiveTasks, findCompleteTasks, findUpcomingTasks, taskIsDone} from './utils';
  21. type Props = Pick<CommonSidebarProps, 'orientation' | 'collapsed'> & {
  22. onClose: () => void;
  23. projects: Project[];
  24. };
  25. /**
  26. * How long (in ms) to delay before beginning to mark tasks complete
  27. */
  28. const INITIAL_MARK_COMPLETE_TIMEOUT = 600;
  29. /**
  30. * How long (in ms) to delay between marking each unseen task as complete.
  31. */
  32. const COMPLETION_SEEN_TIMEOUT = 800;
  33. const Heading = styled(motion.div)`
  34. display: flex;
  35. color: ${p => p.theme.purple300};
  36. font-size: ${p => p.theme.fontSizeExtraSmall};
  37. text-transform: uppercase;
  38. font-weight: 600;
  39. line-height: 1;
  40. margin-top: ${space(3)};
  41. `;
  42. Heading.defaultProps = {
  43. layout: true,
  44. transition: testableTransition(),
  45. };
  46. const customizedTasksHeading = <Heading key="customized">{t('The Basics')}</Heading>;
  47. const completeNowHeading = <Heading key="now">{t('Next Steps')}</Heading>;
  48. const upcomingTasksHeading = (
  49. <Heading key="upcoming">
  50. <Tooltip
  51. containerDisplayMode="block"
  52. title={t('Some tasks should be completed before completing these tasks')}
  53. >
  54. {t('Level Up')}
  55. </Tooltip>
  56. </Heading>
  57. );
  58. const completedTasksHeading = <Heading key="complete">{t('Completed')}</Heading>;
  59. function OnboardingWizardSidebar({collapsed, orientation, onClose, projects}: Props) {
  60. const api = useApi();
  61. const organization = useOrganization();
  62. const [onboardingState, setOnboardingState] = usePersistedOnboardingState();
  63. const markCompletionTimeout = useRef<number | undefined>();
  64. const markCompletionSeenTimeout = useRef<number | undefined>();
  65. function completionTimeout(time: number): Promise<void> {
  66. window.clearTimeout(markCompletionTimeout.current);
  67. return new Promise(resolve => {
  68. markCompletionTimeout.current = window.setTimeout(resolve, time);
  69. });
  70. }
  71. function seenTimeout(time: number): Promise<void> {
  72. window.clearTimeout(markCompletionSeenTimeout.current);
  73. return new Promise(resolve => {
  74. markCompletionSeenTimeout.current = window.setTimeout(resolve, time);
  75. });
  76. }
  77. const {allTasks, customTasks, active, upcoming, complete} = useMemo(() => {
  78. const all = getMergedTasks({
  79. organization,
  80. projects,
  81. onboardingState: onboardingState || undefined,
  82. }).filter(task => task.display);
  83. const tasks = all.filter(task => !task.renderCard);
  84. return {
  85. allTasks: all,
  86. customTasks: all.filter(task => task.renderCard),
  87. active: tasks.filter(findActiveTasks),
  88. upcoming: tasks.filter(findUpcomingTasks),
  89. complete: tasks.filter(findCompleteTasks),
  90. };
  91. }, [organization, projects, onboardingState]);
  92. const markTasksAsSeen = useCallback(
  93. async function () {
  94. const unseenTasks = allTasks
  95. .filter(task => taskIsDone(task) && !task.completionSeen)
  96. .map(task => task.task);
  97. // Incrementally mark tasks as seen. This gives the card completion
  98. // animations time before we move each task into the completed section.
  99. for (const task of unseenTasks) {
  100. await seenTimeout(COMPLETION_SEEN_TIMEOUT);
  101. updateOnboardingTask(api, organization, {task, completionSeen: true});
  102. }
  103. },
  104. [api, organization, allTasks]
  105. );
  106. const markSeenOnOpen = useCallback(
  107. async function () {
  108. // Add a minor delay to marking tasks complete to account for the animation
  109. // opening of the sidebar panel
  110. await completionTimeout(INITIAL_MARK_COMPLETE_TIMEOUT);
  111. markTasksAsSeen();
  112. },
  113. [markTasksAsSeen]
  114. );
  115. useEffect(() => {
  116. markSeenOnOpen();
  117. return () => {
  118. window.clearTimeout(markCompletionTimeout.current);
  119. window.clearTimeout(markCompletionSeenTimeout.current);
  120. };
  121. }, [markSeenOnOpen]);
  122. function makeTaskUpdater(status: OnboardingTask['status']) {
  123. return (task: OnboardingTaskKey) =>
  124. updateOnboardingTask(api, organization, {task, status, completionSeen: true});
  125. }
  126. function renderItem(task: OnboardingTask) {
  127. return (
  128. <AnimatedTaskItem
  129. task={task}
  130. key={`${task.task}`}
  131. onSkip={makeTaskUpdater('skipped')}
  132. onMarkComplete={makeTaskUpdater('complete')}
  133. />
  134. );
  135. }
  136. const completeList = (
  137. <CompleteList key="complete-group">
  138. <AnimatePresence initial={false}>{complete.map(renderItem)}</AnimatePresence>
  139. </CompleteList>
  140. );
  141. const customizedCards = customTasks
  142. .map(task =>
  143. task.renderCard?.({
  144. organization,
  145. task,
  146. onboardingState,
  147. setOnboardingState,
  148. projects,
  149. })
  150. )
  151. .filter(card => !!card);
  152. const items = [
  153. customizedCards.length > 0 && customizedTasksHeading,
  154. ...customizedCards,
  155. active.length > 0 && completeNowHeading,
  156. ...active.map(renderItem),
  157. upcoming.length > 0 && upcomingTasksHeading,
  158. ...upcoming.map(renderItem),
  159. complete.length > 0 && completedTasksHeading,
  160. completeList,
  161. ];
  162. return (
  163. <TaskSidebarPanel collapsed={collapsed} hidePanel={onClose} orientation={orientation}>
  164. <TopRight src={HighlightTopRight} />
  165. <ProgressHeader allTasks={allTasks} completedTasks={complete} />
  166. <TaskList>
  167. <AnimatePresence initial={false}>{items}</AnimatePresence>
  168. </TaskList>
  169. </TaskSidebarPanel>
  170. );
  171. }
  172. const TaskSidebarPanel = styled(SidebarPanel)`
  173. width: 450px;
  174. `;
  175. const AnimatedTaskItem = motion(Task);
  176. AnimatedTaskItem.defaultProps = {
  177. initial: 'initial',
  178. animate: 'animate',
  179. exit: 'exit',
  180. layout: true,
  181. variants: {
  182. initial: {
  183. opacity: 0,
  184. y: 40,
  185. },
  186. animate: {
  187. opacity: 1,
  188. y: 0,
  189. transition: testableTransition({
  190. delay: 0.8,
  191. when: 'beforeChildren',
  192. staggerChildren: 0.3,
  193. }),
  194. },
  195. exit: {
  196. y: 20,
  197. z: -10,
  198. opacity: 0,
  199. transition: {duration: 0.2},
  200. },
  201. },
  202. };
  203. const TaskList = styled('div')`
  204. display: grid;
  205. grid-auto-flow: row;
  206. gap: ${space(1)};
  207. margin: ${space(1)} ${space(4)} ${space(4)} ${space(4)};
  208. `;
  209. const CompleteList = styled('div')`
  210. display: grid;
  211. grid-auto-flow: row;
  212. > div {
  213. transition: border-radius 500ms;
  214. }
  215. > div:not(:first-of-type) {
  216. margin-top: -1px;
  217. border-top-left-radius: 0;
  218. border-top-right-radius: 0;
  219. }
  220. > div:not(:last-of-type) {
  221. border-bottom-left-radius: 0;
  222. border-bottom-right-radius: 0;
  223. }
  224. `;
  225. const TopRight = styled('img')`
  226. position: absolute;
  227. top: 0;
  228. right: 0;
  229. width: 60%;
  230. `;
  231. export default withProjects(OnboardingWizardSidebar);