environmentPageFilter.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Envs');
  58. return (
  59. <PageFilterDropdownButton
  60. isOpen={isOpen}
  61. highlighted={desyncedFilters.has('environments')}
  62. data-test-id="page-filter-environment-selector"
  63. disabled={disabled}
  64. >
  65. <DropdownTitle>
  66. <PageFilterPinIndicator filter="environments">
  67. <IconWindow />
  68. </PageFilterPinIndicator>
  69. <TitleContainer>
  70. {summary}
  71. {!!value.length && value.length > environmentsToShow.length && (
  72. <Badge text={`+${value.length - environmentsToShow.length}`} />
  73. )}
  74. </TitleContainer>
  75. </DropdownTitle>
  76. </PageFilterDropdownButton>
  77. );
  78. };
  79. const customLoadingIndicator = (
  80. <PageFilterDropdownButton
  81. showChevron={false}
  82. disabled
  83. data-test-id="page-filter-environment-selector"
  84. >
  85. <DropdownTitle>
  86. <IconWindow />
  87. <TitleContainer>{t('Loading\u2026')}</TitleContainer>
  88. </DropdownTitle>
  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. detached
  104. showPin
  105. />
  106. );
  107. }
  108. const TitleContainer = styled('div')`
  109. display: flex;
  110. align-items: center;
  111. justify-content: flex-start;
  112. flex: 1 1 0%;
  113. margin-left: ${space(1)};
  114. overflow: hidden;
  115. white-space: nowrap;
  116. text-overflow: ellipsis;
  117. `;
  118. const DropdownTitle = styled('div')`
  119. display: flex;
  120. align-items: center;
  121. flex: 1;
  122. `;
  123. export default withRouter<Props>(EnvironmentPageFilter);