useHasFirstSpan.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.OTHER,
  7. ModuleName.MOBILE_UI,
  8. ModuleName.MOBILE_SCREENS,
  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. [ModuleName.SCREEN_RENDERING]: 'hasInsightsScreenLoad', // Screen rendering and screen loads share similar spans
  26. };
  27. /**
  28. * Returns whether the module and current project selection has received a first insight span
  29. * @param module The name of the module that will be checked for a first span
  30. * @param projects The projects to check for the first span. If not provided, the selected projects will be used
  31. * @returns true if the module has a first span in the selected projects, false otherwise
  32. */
  33. export function useHasFirstSpan(module: ModuleName, projects?: Project[]): boolean {
  34. const {projects: allProjects} = useProjects();
  35. const pageFilters = usePageFilters();
  36. // Unsupported modules. Remove MOBILE_UI from this list once released.
  37. if ((excludedModuleNames as readonly ModuleName[]).includes(module)) {
  38. return false;
  39. }
  40. if (projects) {
  41. return projects.some(p => p[modulePropertyMap[module]] === true);
  42. }
  43. let selectedProjects: Project[] = [];
  44. // There are three cases for the selected pageFilter projects:
  45. // - [] empty list represents "My Projects"
  46. // - [-1] represents "All Projects"
  47. // - [.., ..] otherwise, represents a list of project IDs
  48. if (pageFilters.selection.projects.length === 0) {
  49. selectedProjects = allProjects.filter(p => p.isMember);
  50. } else if (
  51. pageFilters.selection.projects.length === 1 &&
  52. pageFilters.selection.projects[0] === -1
  53. ) {
  54. selectedProjects = allProjects;
  55. } else {
  56. selectedProjects = allProjects.filter(p =>
  57. pageFilters.selection.projects.includes(parseInt(p.id, 10))
  58. );
  59. }
  60. return selectedProjects.some(p => p[modulePropertyMap[module]] === true);
  61. }