index.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import {t} from 'sentry/locale';
  2. import ConfigStore from 'sentry/stores/configStore';
  3. import type {Organization, Region} from 'sentry/types';
  4. const RegionDisplayName: Record<string, string> = {
  5. US: t('United States of America (US)'),
  6. DE: t('European Union (EU)'),
  7. };
  8. enum RegionFlagIndicator {
  9. US = '🇺🇸',
  10. DE = '🇪🇺',
  11. }
  12. export interface RegionData {
  13. displayName: string;
  14. name: string;
  15. url: string;
  16. flag?: RegionFlagIndicator;
  17. }
  18. export function getRegionDisplayName(region: Region): string {
  19. return RegionDisplayName[region.name.toUpperCase()] ?? region.name;
  20. }
  21. export function getRegionFlagIndicator(region: Region): RegionFlagIndicator | undefined {
  22. const regionName = region.name.toUpperCase();
  23. return RegionFlagIndicator[regionName];
  24. }
  25. export function getRegionDataFromOrganization(
  26. organization: Organization
  27. ): RegionData | undefined {
  28. const {regionUrl} = organization.links;
  29. const regions = ConfigStore.get('regions') ?? [];
  30. const region = regions.find(value => {
  31. return value.url === regionUrl;
  32. });
  33. if (!region) {
  34. return undefined;
  35. }
  36. return {
  37. flag: getRegionFlagIndicator(region),
  38. displayName: getRegionDisplayName(region),
  39. name: region.name,
  40. url: region.url,
  41. };
  42. }
  43. export function getRegions(): Region[] {
  44. return ConfigStore.get('regions') ?? [];
  45. }
  46. export function getRegionChoices(): [string, string][] {
  47. const regions = getRegions();
  48. return regions.map(region => {
  49. const {url} = region;
  50. return [
  51. url,
  52. `${getRegionFlagIndicator(region) || ''} ${getRegionDisplayName(region)}`,
  53. ];
  54. });
  55. }
  56. export function shouldDisplayRegions(): boolean {
  57. const regionCount = getRegions().length;
  58. return (
  59. ConfigStore.get('features').has('organizations:multi-region-selector') &&
  60. regionCount > 1
  61. );
  62. }