environmentPageFilter.tsx 4.0 KB

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