useCrossPlatformProject.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import {useMemo} from 'react';
  2. import {decodeScalar} from 'sentry/utils/queryString';
  3. import {useLocation} from 'sentry/utils/useLocation';
  4. import usePageFilters from 'sentry/utils/usePageFilters';
  5. import useProjects from 'sentry/utils/useProjects';
  6. import {isCrossPlatform} from 'sentry/views/insights/mobile/screenload/utils';
  7. export const PLATFORM_LOCAL_STORAGE_KEY = 'mobile-performance-platform';
  8. export const PLATFORM_QUERY_PARAM = 'platform';
  9. export const DEFAULT_PLATFORM = 'Android';
  10. function useCrossPlatformProject() {
  11. const {selection} = usePageFilters();
  12. const {projects} = useProjects();
  13. const location = useLocation();
  14. const project = useMemo(() => {
  15. if (selection.projects.length !== 1) {
  16. return null;
  17. }
  18. return projects.find(p => p.id === String(selection.projects)) ?? null;
  19. }, [projects, selection.projects]);
  20. const isProjectCrossPlatform = useMemo(
  21. () => !!(project && isCrossPlatform(project)),
  22. [project]
  23. );
  24. const selectedPlatform =
  25. decodeScalar(location.query[PLATFORM_QUERY_PARAM]) ??
  26. localStorage.getItem(PLATFORM_LOCAL_STORAGE_KEY) ??
  27. DEFAULT_PLATFORM;
  28. return {
  29. project,
  30. selectedPlatform,
  31. isProjectCrossPlatform,
  32. };
  33. }
  34. export default useCrossPlatformProject;