environmentPageFilter.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import styled from '@emotion/styled';
  2. import {updateEnvironments} from 'sentry/actionCreators/pageFilters';
  3. import Badge from 'sentry/components/badge';
  4. import {EnvironmentPageFilter as NewEnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  5. import EnvironmentSelector from 'sentry/components/organizations/environmentSelector';
  6. import PageFilterDropdownButton from 'sentry/components/organizations/pageFilters/pageFilterDropdownButton';
  7. import PageFilterPinIndicator from 'sentry/components/organizations/pageFilters/pageFilterPinIndicator';
  8. import {IconWindow} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import {FormSize} from 'sentry/utils/theme';
  11. import {trimSlug} from 'sentry/utils/trimSlug';
  12. import useOrganization from 'sentry/utils/useOrganization';
  13. import usePageFilters from 'sentry/utils/usePageFilters';
  14. import useProjects from 'sentry/utils/useProjects';
  15. import useRouter from 'sentry/utils/useRouter';
  16. type EnvironmentSelectorProps = React.ComponentProps<typeof EnvironmentSelector>;
  17. type Props = {
  18. alignDropdown?: EnvironmentSelectorProps['alignDropdown'];
  19. disabled?: EnvironmentSelectorProps['disabled'];
  20. /**
  21. * Max character length for the dropdown title. Default is 20. This number
  22. * is used to determine how many projects to show, and how much to truncate.
  23. */
  24. maxTitleLength?: number;
  25. /**
  26. * Reset these URL params when we fire actions (custom routing only)
  27. */
  28. resetParamsOnChange?: string[];
  29. size?: FormSize;
  30. };
  31. function OldEnvironmentPageFilter({
  32. resetParamsOnChange = [],
  33. alignDropdown,
  34. disabled,
  35. maxTitleLength = 20,
  36. size = 'md',
  37. }: Props) {
  38. const router = useRouter();
  39. const {projects, initiallyLoaded: projectsLoaded} = useProjects();
  40. const organization = useOrganization();
  41. const {selection, isReady, desyncedFilters} = usePageFilters();
  42. const handleUpdateEnvironments = (environments: string[]) => {
  43. updateEnvironments(environments, router, {
  44. save: true,
  45. resetParams: resetParamsOnChange,
  46. });
  47. };
  48. const customDropdownButton: EnvironmentSelectorProps['customDropdownButton'] = ({
  49. isOpen,
  50. value,
  51. }) => {
  52. const environmentsToShow =
  53. value[0]?.length + value[1]?.length <= maxTitleLength - 2
  54. ? value.slice(0, 2)
  55. : value.slice(0, 1);
  56. const summary = value.length
  57. ? environmentsToShow.map(env => trimSlug(env, maxTitleLength)).join(', ')
  58. : t('All Envs');
  59. return (
  60. <PageFilterDropdownButton
  61. isOpen={isOpen}
  62. highlighted={desyncedFilters.has('environments')}
  63. data-test-id="page-filter-environment-selector"
  64. disabled={disabled}
  65. size={size}
  66. icon={
  67. <PageFilterPinIndicator filter="environments">
  68. <IconWindow />
  69. </PageFilterPinIndicator>
  70. }
  71. >
  72. <TitleContainer>
  73. {summary}
  74. {!!value.length && value.length > environmentsToShow.length && (
  75. <Badge text={`+${value.length - environmentsToShow.length}`} />
  76. )}
  77. </TitleContainer>
  78. </PageFilterDropdownButton>
  79. );
  80. };
  81. const customLoadingIndicator = (
  82. <PageFilterDropdownButton
  83. icon={<IconWindow />}
  84. showChevron={false}
  85. disabled
  86. data-test-id="page-filter-environment-selector"
  87. >
  88. <TitleContainer>{t('Loading\u2026')}</TitleContainer>
  89. </PageFilterDropdownButton>
  90. );
  91. return (
  92. <EnvironmentSelector
  93. organization={organization}
  94. projects={projects}
  95. loadingProjects={!projectsLoaded || !isReady}
  96. selectedProjects={selection.projects}
  97. value={selection.environments}
  98. onUpdate={handleUpdateEnvironments}
  99. customDropdownButton={customDropdownButton}
  100. customLoadingIndicator={customLoadingIndicator}
  101. alignDropdown={alignDropdown}
  102. disabled={disabled}
  103. />
  104. );
  105. }
  106. const TitleContainer = styled('div')`
  107. text-align: left;
  108. ${p => p.theme.overflowEllipsis}
  109. `;
  110. function EnvironmentPageFilter(props: Props) {
  111. const organization = useOrganization();
  112. if (organization.features.includes('new-page-filter')) {
  113. return <NewEnvironmentPageFilter {...props} />;
  114. }
  115. return <OldEnvironmentPageFilter {...props} />;
  116. }
  117. export default EnvironmentPageFilter;