useHasFirstSpan.tsx 2.4 KB

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