demoWalkthrough.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {useEffect, useState} from 'react';
  2. import {getOnboardingTasks} from 'sentry/components/onboardingWizard/taskConfig';
  3. import {
  4. findActiveTasks,
  5. findCompleteTasks,
  6. findUpcomingTasks,
  7. } from 'sentry/components/onboardingWizard/utils';
  8. import {OnboardingTask, Organization, Project} from 'sentry/types';
  9. import useApi from 'sentry/utils/useApi';
  10. import {OnboardingState} from 'sentry/views/onboarding/types';
  11. type Options = {
  12. onboardingState: OnboardingState | undefined;
  13. organization: Organization;
  14. projects: Project[];
  15. };
  16. /**
  17. * This function is used to determine which tasks to show as complete/incomplete in the sidebar progress circle.
  18. *
  19. * TODO: Move these to the demo repo once we can use React Hooks in Hooks in the repo.
  20. *
  21. */
  22. export function useSandboxSidebarTasks({
  23. organization,
  24. projects,
  25. onboardingState,
  26. }: Options) {
  27. const api = useApi();
  28. const [tasks, setTasks] = useState<OnboardingTask[]>([]);
  29. useEffect(() => {
  30. const getTasks = async () => {
  31. const serverTasks = await api.requestPromise(
  32. `/organizations/${organization.slug}/onboarding-tasks/`,
  33. {method: 'GET'}
  34. );
  35. const taskDescriptors = getOnboardingTasks({
  36. organization,
  37. projects,
  38. onboardingState,
  39. });
  40. // Map server task state (i.e. completed status) with tasks objects
  41. const allTasks = taskDescriptors.map(
  42. desc =>
  43. ({
  44. ...desc,
  45. ...serverTasks.find(
  46. serverTask =>
  47. serverTask.task === desc.task || serverTask.task === desc.serverTask
  48. ),
  49. requisiteTasks: [],
  50. } as OnboardingTask)
  51. );
  52. // Map incomplete requisiteTasks as full task objects
  53. const mappedTasks = allTasks.map(task => ({
  54. ...task,
  55. requisiteTasks: task.requisites
  56. .map(key => allTasks.find(task2 => task2.task === key)!)
  57. .filter(reqTask => reqTask.status !== 'complete'),
  58. }));
  59. setTasks(mappedTasks);
  60. return;
  61. };
  62. getTasks();
  63. }, [organization, projects, onboardingState, api]);
  64. return tasks;
  65. }
  66. /**
  67. * This function is used to determine which onboarding task is shown in the Sidebar panel in the Sandbox.
  68. *
  69. * TODO: Move this to the demo repo once we can use React Hooks in Hooks in the repo.
  70. *
  71. */
  72. export function useSandboxTasks({organization, projects, onboardingState}: Options) {
  73. const api = useApi();
  74. const [allTasks, setAllTasks] = useState<OnboardingTask[]>([]);
  75. const [customTasks, setCustomTasks] = useState<OnboardingTask[]>([]);
  76. const [activeTasks, setActiveTasks] = useState<OnboardingTask[]>([]);
  77. const [upcomingTasks, setUpcomingTasks] = useState<OnboardingTask[]>([]);
  78. const [completeTasks, setCompleteTasks] = useState<OnboardingTask[]>([]);
  79. useEffect(() => {
  80. const getTasks = async () => {
  81. const serverTasks = await api.requestPromise(
  82. `/organizations/${organization.slug}/onboarding-tasks/`,
  83. {method: 'GET'}
  84. );
  85. const taskDescriptors = getOnboardingTasks({
  86. organization,
  87. projects,
  88. onboardingState,
  89. });
  90. // Map server task state (i.e. completed status) with tasks objects
  91. const totalTasks = taskDescriptors.map(
  92. desc =>
  93. ({
  94. ...desc,
  95. ...serverTasks.find(
  96. serverTask =>
  97. serverTask.task === desc.task || serverTask.task === desc.serverTask
  98. ),
  99. requisiteTasks: [],
  100. } as OnboardingTask)
  101. );
  102. // Map incomplete requisiteTasks as full task objects
  103. const mappedTasks = totalTasks.map(task => ({
  104. ...task,
  105. requisiteTasks: task.requisites
  106. .map(key => totalTasks.find(task2 => task2.task === key)!)
  107. .filter(reqTask => reqTask.status !== 'complete'),
  108. }));
  109. const all = mappedTasks.filter(task => task.display);
  110. const tasks = all.filter(task => !task.renderCard);
  111. setAllTasks(all);
  112. setCustomTasks(all.filter(task => task.renderCard));
  113. setActiveTasks(tasks.filter(findActiveTasks));
  114. setUpcomingTasks(tasks.filter(findUpcomingTasks));
  115. setCompleteTasks(tasks.filter(findCompleteTasks));
  116. return;
  117. };
  118. getTasks();
  119. }, [organization, projects, onboardingState, api]);
  120. return {
  121. allTasks,
  122. customTasks,
  123. active: activeTasks,
  124. upcoming: upcomingTasks,
  125. complete: completeTasks,
  126. };
  127. }