environmentPageFilter.tsx 4.2 KB

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