useOrganizationsWithRegion.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import ConfigStore from 'sentry/stores/configStore';
  2. import type {Organization} from 'sentry/types/organization';
  3. import type {Region} from 'sentry/types/system';
  4. import {useQuery} from 'sentry/utils/queryClient';
  5. import type RequestError from 'sentry/utils/requestError/requestError';
  6. import useApi from 'sentry/utils/useApi';
  7. import type {OrganizationWithRegion} from 'sentry/views/setupWizard/types';
  8. export function useOrganizationsWithRegion() {
  9. const api = useApi();
  10. return useQuery<OrganizationWithRegion[], RequestError>({
  11. queryKey: ['/organizations/'],
  12. queryFn: async () => {
  13. const regions = ConfigStore.get('memberRegions');
  14. const results = await Promise.all(
  15. regions.map<Promise<[Region, Organization[]]>>(async region => [
  16. region,
  17. await api.requestPromise(`/organizations/`, {
  18. host: region.url,
  19. // Authentication errors can happen as we span regions.
  20. allowAuthError: true,
  21. }),
  22. ])
  23. );
  24. return results.reduce<OrganizationWithRegion[]>((acc, [region, response]) => {
  25. // Don't append error results to the org list.
  26. if (response[0]) {
  27. acc = acc.concat(response.map(org => ({...org, region})));
  28. }
  29. return acc;
  30. }, []);
  31. },
  32. refetchOnWindowFocus: false,
  33. retry: false,
  34. });
  35. }