environmentPageFilter.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import styled from '@emotion/styled';
  2. import {updateEnvironments} from 'sentry/actionCreators/pageFilters';
  3. import Badge from 'sentry/components/badge';
  4. import type {ButtonProps} from 'sentry/components/button';
  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. 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?: ButtonProps['size'];
  30. };
  31. function EnvironmentPageFilter({
  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. >
  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. />
  106. );
  107. }
  108. const TitleContainer = styled('div')`
  109. display: flex;
  110. align-items: center;
  111. flex: 1 1 0%;
  112. margin-left: ${space(1)};
  113. text-align: left;
  114. ${p => p.theme.overflowEllipsis}
  115. `;
  116. const DropdownTitle = styled('div')`
  117. display: flex;
  118. align-items: center;
  119. flex: 1;
  120. width: max-content;
  121. min-width: 0;
  122. `;
  123. export default EnvironmentPageFilter;