useHasFirstSpan.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import type {Project} from 'sentry/types/project';
  2. import usePageFilters from 'sentry/utils/usePageFilters';
  3. import useProjects from 'sentry/utils/useProjects';
  4. import {ModuleName} from 'sentry/views/insights/types';
  5. const excludedModuleNames = [
  6. ModuleName.ALL,
  7. ModuleName.OTHER,
  8. ModuleName.MOBILE_UI,
  9. ] as const;
  10. type ExcludedModuleNames = (typeof excludedModuleNames)[number];
  11. const modulePropertyMap: Record<
  12. Exclude<ModuleName, ExcludedModuleNames>,
  13. keyof Project
  14. > = {
  15. [ModuleName.HTTP]: 'hasInsightsHttp',
  16. [ModuleName.DB]: 'hasInsightsDb',
  17. [ModuleName.CACHE]: 'hasInsightsCaches',
  18. [ModuleName.VITAL]: 'hasInsightsVitals',
  19. [ModuleName.QUEUE]: 'hasInsightsQueues',
  20. [ModuleName.SCREEN_LOAD]: 'hasInsightsScreenLoad',
  21. [ModuleName.APP_START]: 'hasInsightsAppStart',
  22. // Renamed resource to assets
  23. [ModuleName.RESOURCE]: 'hasInsightsAssets',
  24. [ModuleName.AI]: 'hasInsightsLlmMonitoring',
  25. };
  26. /* Returns whether the module and current project selection has received a first insight span */
  27. export function useHasFirstSpan(module: ModuleName): boolean {
  28. const {projects: allProjects} = useProjects();
  29. const pageFilters = usePageFilters();
  30. // Unsupported modules. Remove MOBILE_UI from this list once released.
  31. if ((excludedModuleNames as readonly ModuleName[]).includes(module)) return false;
  32. let selectedProjects: Project[] = [];
  33. // There are three cases for the selected pageFilter projects:
  34. // - [] empty list represents "My Projects"
  35. // - [-1] represents "All Projects"
  36. // - [.., ..] otherwise, represents a list of project IDs
  37. if (pageFilters.selection.projects.length === 0) {
  38. selectedProjects = allProjects.filter(p => p.isMember);
  39. } else if (
  40. pageFilters.selection.projects.length === 1 &&
  41. pageFilters.selection.projects[0] === -1
  42. ) {
  43. selectedProjects = allProjects;
  44. } else {
  45. selectedProjects = allProjects.filter(p =>
  46. pageFilters.selection.projects.includes(parseInt(p.id, 10))
  47. );
  48. }
  49. return selectedProjects.some(p => p[modulePropertyMap[module]] === true);
  50. }