useHasFirstSpan.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import type {Project} from 'sentry/types/project';
  2. import {isActiveSuperuser} from 'sentry/utils/isActiveSuperuser';
  3. import usePageFilters from 'sentry/utils/usePageFilters';
  4. import useProjects from 'sentry/utils/useProjects';
  5. import {ModuleName} from 'sentry/views/insights/types';
  6. const excludedModuleNames = [
  7. ModuleName.OTHER,
  8. ModuleName.MOBILE_UI,
  9. ModuleName.MOBILE_SCREENS,
  10. ModuleName.CRONS,
  11. ModuleName.UPTIME,
  12. ] as const;
  13. type ExcludedModuleNames = (typeof excludedModuleNames)[number];
  14. const modulePropertyMap: Record<
  15. Exclude<ModuleName, ExcludedModuleNames>,
  16. keyof Project
  17. > = {
  18. [ModuleName.HTTP]: 'hasInsightsHttp',
  19. [ModuleName.DB]: 'hasInsightsDb',
  20. [ModuleName.CACHE]: 'hasInsightsCaches',
  21. [ModuleName.VITAL]: 'hasInsightsVitals',
  22. [ModuleName.QUEUE]: 'hasInsightsQueues',
  23. [ModuleName.SCREEN_LOAD]: 'hasInsightsScreenLoad',
  24. [ModuleName.APP_START]: 'hasInsightsAppStart',
  25. // Renamed resource to assets
  26. [ModuleName.RESOURCE]: 'hasInsightsAssets',
  27. [ModuleName.AI]: 'hasInsightsLlmMonitoring',
  28. [ModuleName.SCREEN_RENDERING]: 'hasInsightsScreenLoad', // Screen rendering and screen loads share similar spans
  29. };
  30. /**
  31. * Returns whether the module and current project selection has received a first insight span
  32. * @param module The name of the module that will be checked for a first span
  33. * @param projects The projects to check for the first span. If not provided, the selected projects will be used
  34. * @returns true if the module has a first span in the selected projects, false otherwise
  35. */
  36. export function useHasFirstSpan(module: ModuleName, projects?: Project[]): boolean {
  37. const {projects: allProjects} = useProjects();
  38. const pageFilters = usePageFilters();
  39. // Unsupported modules. Remove MOBILE_UI from this list once released.
  40. if ((excludedModuleNames as readonly ModuleName[]).includes(module)) {
  41. return false;
  42. }
  43. if (projects) {
  44. return projects.some(p => p[modulePropertyMap[module]] === true);
  45. }
  46. let selectedProjects: Project[] = [];
  47. // There are three cases for the selected pageFilter projects:
  48. // - [] empty list represents "My Projects"
  49. // - [-1] represents "All Projects"
  50. // - [.., ..] otherwise, represents a list of project IDs
  51. if (pageFilters.selection.projects.length === 0 && isActiveSuperuser()) {
  52. selectedProjects = allProjects; // when superuser is enabled, My Projects isn't applicable, and in reality all projects are selected when projects.length === 0
  53. }
  54. if (pageFilters.selection.projects.length === 0) {
  55. selectedProjects = allProjects.filter(p => p.isMember);
  56. } else if (
  57. pageFilters.selection.projects.length === 1 &&
  58. pageFilters.selection.projects[0] === -1
  59. ) {
  60. selectedProjects = allProjects;
  61. } else {
  62. selectedProjects = allProjects.filter(p =>
  63. pageFilters.selection.projects.includes(parseInt(p.id, 10))
  64. );
  65. }
  66. return selectedProjects.some(p => p[modulePropertyMap[module]] === true);
  67. }