useRecommendedSdkUpgrades.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import {ServerSideSamplingStore} from 'sentry/stores/serverSideSamplingStore';
  2. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  3. import {Organization, Project} from 'sentry/types';
  4. import {defined} from 'sentry/utils';
  5. import useProjects from 'sentry/utils/useProjects';
  6. type Props = {
  7. organization: Organization;
  8. projectId: Project['id'];
  9. };
  10. export function useRecommendedSdkUpgrades({organization, projectId}: Props) {
  11. const {sdkVersions, distribution} = useLegacyStore(ServerSideSamplingStore);
  12. const {data = []} = sdkVersions;
  13. const sdksToUpdate = data.filter(
  14. ({isSendingSource, isSendingSampleRate, isSupportedPlatform}) => {
  15. return (!isSendingSource || !isSendingSampleRate) && isSupportedPlatform;
  16. }
  17. );
  18. const incompatibleSDKs = data.filter(({isSupportedPlatform}) => !isSupportedPlatform);
  19. const compatibleUpdatedSDKs = data.filter(
  20. ({isSendingSource, isSendingSampleRate, isSupportedPlatform}) =>
  21. isSendingSource && isSendingSampleRate && isSupportedPlatform
  22. );
  23. const {projects} = useProjects({
  24. slugs: [...sdksToUpdate, ...incompatibleSDKs, ...compatibleUpdatedSDKs].map(
  25. ({project}) => project
  26. ),
  27. orgId: organization.slug,
  28. });
  29. const recommendedSdkUpgrades = projects
  30. .map(project => {
  31. const sdkInfo = sdksToUpdate.find(
  32. sdkToUpdate => sdkToUpdate.project === project.slug
  33. );
  34. if (!sdkInfo) {
  35. return undefined;
  36. }
  37. return {
  38. project,
  39. latestSDKName: sdkInfo.latestSDKName,
  40. latestSDKVersion: sdkInfo.latestSDKVersion,
  41. };
  42. })
  43. .filter(defined);
  44. const incompatibleProjects = organization.features.includes(
  45. 'server-side-sampling-allow-incompatible-platforms'
  46. )
  47. ? []
  48. : projects.filter(project =>
  49. incompatibleSDKs.find(incompatibleSDK => incompatibleSDK.project === project.slug)
  50. );
  51. const isProjectIncompatible = incompatibleProjects.some(
  52. incompatibleProject => incompatibleProject.id === projectId
  53. );
  54. const isProjectOnOldSDK = recommendedSdkUpgrades.some(
  55. recommendedSdkUpgrade => recommendedSdkUpgrade.project.id === projectId
  56. );
  57. const compatibleUpdatedProjects = projects.filter(project =>
  58. compatibleUpdatedSDKs.find(
  59. compatibleUpdatedSDK => compatibleUpdatedSDK.project === project.slug
  60. )
  61. );
  62. const affectedProjects = [
  63. ...recommendedSdkUpgrades.map(({project}) => project),
  64. ...incompatibleProjects,
  65. ...compatibleUpdatedProjects,
  66. ];
  67. return {
  68. recommendedSdkUpgrades,
  69. incompatibleProjects,
  70. affectedProjects,
  71. loading: sdkVersions.loading || distribution.loading,
  72. isProjectIncompatible,
  73. isProjectOnOldSDK,
  74. };
  75. }