useHasFirstSpan.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. };
  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)) {
  37. return false;
  38. }
  39. if (projects) {
  40. return projects.some(p => p[modulePropertyMap[module]] === true);
  41. }
  42. let selectedProjects: Project[] = [];
  43. // There are three cases for the selected pageFilter projects:
  44. // - [] empty list represents "My Projects"
  45. // - [-1] represents "All Projects"
  46. // - [.., ..] otherwise, represents a list of project IDs
  47. if (pageFilters.selection.projects.length === 0) {
  48. selectedProjects = allProjects.filter(p => p.isMember);
  49. } else if (
  50. pageFilters.selection.projects.length === 1 &&
  51. pageFilters.selection.projects[0] === -1
  52. ) {
  53. selectedProjects = allProjects;
  54. } else {
  55. selectedProjects = allProjects.filter(p =>
  56. pageFilters.selection.projects.includes(parseInt(p.id, 10))
  57. );
  58. }
  59. return selectedProjects.some(p => p[modulePropertyMap[module]] === true);
  60. }