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