useCurrentProjectState.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import {useEffect, useMemo, useState} from 'react';
  2. import first from 'lodash/first';
  3. import {splitProjectsByReplaySupport} from 'sentry/components/replaysOnboarding/utils';
  4. import {SidebarPanelKey} from 'sentry/components/sidebar/types';
  5. import {replayOnboardingPlatforms, replayPlatforms} from 'sentry/data/platformCategories';
  6. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  7. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  8. import {Project} from 'sentry/types';
  9. import useProjects from 'sentry/utils/useProjects';
  10. function useCurrentProjectState({currentPanel}: {currentPanel: '' | SidebarPanelKey}) {
  11. const [currentProject, setCurrentProject] = useState<Project | undefined>(undefined);
  12. const {projects, initiallyLoaded: projectsLoaded} = useProjects();
  13. const {selection, isReady} = useLegacyStore(PageFiltersStore);
  14. const isActive = currentPanel === SidebarPanelKey.REPLAYS_ONBOARDING;
  15. // Projects where we have the onboarding instructions ready:
  16. const projectsWithOnboarding = useMemo(
  17. () =>
  18. projects.filter(
  19. p => p.platform && replayOnboardingPlatforms.includes(p.platform) && !p.hasReplays
  20. ),
  21. [projects]
  22. );
  23. // Projects that support replays, but we haven't created the onboarding instructions (yet):
  24. const projectWithReplaySupport = useMemo(
  25. () =>
  26. projects.filter(
  27. p => p.platform && replayPlatforms.includes(p.platform) && !p.hasReplays
  28. ),
  29. [projects]
  30. );
  31. useEffect(() => {
  32. if (!isActive) {
  33. setCurrentProject(undefined);
  34. }
  35. }, [isActive]);
  36. useEffect(() => {
  37. if (currentProject || !projectsLoaded || !projects.length || !isReady || !isActive) {
  38. return;
  39. }
  40. if (!projectWithReplaySupport) {
  41. return;
  42. }
  43. if (selection.projects.length) {
  44. const selectedProjectIds = selection.projects.map(String);
  45. // If we selected something that has onboarding instructions, pick that first
  46. const projectForOnboarding = projectsWithOnboarding.find(p =>
  47. selectedProjectIds.includes(p.id)
  48. );
  49. if (projectForOnboarding) {
  50. setCurrentProject(projectForOnboarding);
  51. }
  52. // If we selected something that supports replays pick that
  53. const projectSupportsReplay = projectWithReplaySupport.find(p =>
  54. selectedProjectIds.includes(p.id)
  55. );
  56. if (projectSupportsReplay) {
  57. setCurrentProject(projectSupportsReplay);
  58. }
  59. const firstSelectedProject = projects.find(p => selectedProjectIds.includes(p.id));
  60. setCurrentProject(firstSelectedProject);
  61. } else {
  62. // We have no selection, so pick a project which we've found
  63. setCurrentProject(first(projectsWithOnboarding) || first(projectWithReplaySupport));
  64. }
  65. }, [
  66. currentProject,
  67. projectsLoaded,
  68. projects,
  69. isReady,
  70. isActive,
  71. selection.projects,
  72. projectsWithOnboarding,
  73. projectWithReplaySupport,
  74. ]);
  75. const {supported, unsupported} = useMemo(() => {
  76. return splitProjectsByReplaySupport(projects);
  77. }, [projects]);
  78. return {
  79. projects: projectWithReplaySupport,
  80. allProjects: projects,
  81. supportedProjects: supported,
  82. unsupportedProjects: unsupported,
  83. currentProject,
  84. setCurrentProject,
  85. };
  86. }
  87. export default useCurrentProjectState;