index.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import {useEffect, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  4. import {Button} from 'sentry/components/button';
  5. import * as Layout from 'sentry/components/layouts/thirds';
  6. import LoadingError from 'sentry/components/loadingError';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import NoProjectMessage from 'sentry/components/noProjectMessage';
  9. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  10. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  11. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  12. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  13. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  14. import {IconChevron} from 'sentry/icons';
  15. import {t} from 'sentry/locale';
  16. import ConfigStore from 'sentry/stores/configStore';
  17. import {space} from 'sentry/styles/space';
  18. import {Project} from 'sentry/types';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. import usePageFilters from 'sentry/utils/usePageFilters';
  21. import useProjects from 'sentry/utils/useProjects';
  22. import useRouter from 'sentry/utils/useRouter';
  23. import Header from '../components/header';
  24. import {Threshold} from '../utils/types';
  25. import useFetchThresholdsListData from '../utils/useFetchThresholdsListData';
  26. import NoThresholdCard from './noThresholdCard';
  27. import ThresholdGroupTable from './thresholdGroupTable';
  28. type Props = {};
  29. function ReleaseThresholdList({}: Props) {
  30. const [listError, setListError] = useState<string>('');
  31. const [newProjThresholdsPage, setNewProjThresholdsPage] = useState(0);
  32. const PAGE_SIZE = 10;
  33. const router = useRouter();
  34. const organization = useOrganization();
  35. useEffect(() => {
  36. const hasV2ReleaseUIEnabled = organization.features.includes('release-ui-v2');
  37. if (!hasV2ReleaseUIEnabled) {
  38. router.replace('/releases/');
  39. }
  40. }, [router, organization]);
  41. const {projects} = useProjects();
  42. const {selection} = usePageFilters();
  43. const {
  44. data: thresholds = [],
  45. error: requestError,
  46. isLoading,
  47. isError,
  48. refetch,
  49. } = useFetchThresholdsListData({
  50. selectedProjectIds: selection.projects,
  51. selectedEnvs: selection.environments,
  52. });
  53. const selectedProjects: Project[] = useMemo(() => {
  54. return projects.filter(
  55. project =>
  56. selection.projects.some(id => String(id) === project.id || id === -1) ||
  57. !selection.projects.length
  58. );
  59. }, [projects, selection.projects]);
  60. const projectsById: {[key: string]: Project} = useMemo(() => {
  61. const byId = {};
  62. selectedProjects.forEach(proj => {
  63. byId[proj.id] = proj;
  64. });
  65. return byId;
  66. }, [selectedProjects]);
  67. const getAllEnvironmentNames = useMemo((): string[] => {
  68. const selectedProjectIds = selection.projects.map(id => String(id));
  69. const {user} = ConfigStore.getState();
  70. const allEnvSet = new Set(projects.flatMap(project => project.environments));
  71. // NOTE: mostly taken from environmentSelector.tsx
  72. const unSortedEnvs = new Set(
  73. projects.flatMap(project => {
  74. /**
  75. * Include environments from:
  76. * all projects if the user is a superuser
  77. * the requested projects
  78. * all member projects if 'my projects' (empty list) is selected.
  79. * all projects if -1 is the only selected project.
  80. */
  81. if (
  82. (selectedProjectIds.length === 1 &&
  83. selectedProjectIds[0] === String(ALL_ACCESS_PROJECTS) &&
  84. project.hasAccess) ||
  85. (selectedProjectIds.length === 0 && (project.isMember || user.isSuperuser)) ||
  86. selectedProjectIds.includes(project.id)
  87. ) {
  88. return project.environments;
  89. }
  90. return [];
  91. })
  92. );
  93. const envDiff = new Set([...allEnvSet].filter(x => !unSortedEnvs.has(x)));
  94. // bubble the selected projects envs first, then concat the rest of the envs
  95. return Array.from(unSortedEnvs)
  96. .sort()
  97. .concat([...envDiff].sort());
  98. }, [projects, selection.projects]);
  99. /**
  100. * Thresholds filtered by environment selection
  101. * NOTE: currently no way to filter for 'None' environments
  102. */
  103. const filteredThresholds = selection.environments.length
  104. ? thresholds.filter(threshold => {
  105. return threshold.environment?.name
  106. ? selection.environments.indexOf(threshold.environment.name) > -1
  107. : !selection.environments.length;
  108. })
  109. : thresholds;
  110. const thresholdsByProject: {[key: string]: Threshold[]} = useMemo(() => {
  111. const byProj = {};
  112. filteredThresholds.forEach(threshold => {
  113. const projId = threshold.project.id;
  114. if (!byProj[projId]) {
  115. byProj[projId] = [];
  116. }
  117. byProj[projId].push(threshold);
  118. });
  119. return byProj;
  120. }, [filteredThresholds]);
  121. const projectsWithoutThresholds: Project[] = useMemo(() => {
  122. // TODO: limit + paginate list
  123. return selectedProjects.filter(proj => !thresholdsByProject[proj.id]);
  124. }, [thresholdsByProject, selectedProjects]);
  125. const setTempError = msg => {
  126. setListError(msg);
  127. setTimeout(() => setListError(''), 5000);
  128. };
  129. if (isError) {
  130. return <LoadingError onRetry={refetch} message={requestError.message} />;
  131. }
  132. if (isLoading) {
  133. return <LoadingIndicator />;
  134. }
  135. return (
  136. <PageFiltersContainer>
  137. <NoProjectMessage organization={organization}>
  138. <Header router={router} hasV2ReleaseUIEnabled />
  139. <Layout.Body>
  140. <Layout.Main fullWidth>
  141. <FilterRow>
  142. <ReleaseThresholdsPageFilterBar condensed>
  143. <GuideAnchor target="release_projects">
  144. <ProjectPageFilter />
  145. </GuideAnchor>
  146. <EnvironmentPageFilter />
  147. </ReleaseThresholdsPageFilterBar>
  148. <ListError>{listError}</ListError>
  149. </FilterRow>
  150. {thresholdsByProject &&
  151. Object.entries(thresholdsByProject).map(([projId, thresholdsByProj]) => (
  152. <ThresholdGroupTable
  153. key={projId}
  154. project={projectsById[projId]}
  155. thresholds={thresholdsByProj}
  156. isLoading={isLoading}
  157. isError={isError}
  158. refetch={refetch}
  159. setTempError={setTempError}
  160. allEnvironmentNames={getAllEnvironmentNames} // TODO: determine whether to move down to threshold group table
  161. />
  162. ))}
  163. {projectsWithoutThresholds.length > 0 && (
  164. <div>
  165. <strong>Projects without Thresholds</strong>
  166. {projectsWithoutThresholds
  167. .slice(
  168. PAGE_SIZE * newProjThresholdsPage,
  169. PAGE_SIZE * newProjThresholdsPage + PAGE_SIZE
  170. )
  171. .map(proj => (
  172. <NoThresholdCard
  173. key={proj.id}
  174. project={proj}
  175. allEnvironmentNames={getAllEnvironmentNames} // TODO: determine whether to move down to threshold group table
  176. refetch={refetch}
  177. setTempError={setTempError}
  178. />
  179. ))}
  180. <Paginator>
  181. <Button
  182. icon={<IconChevron direction="left" size="sm" />}
  183. aria-label={t('Previous')}
  184. size="sm"
  185. disabled={newProjThresholdsPage === 0}
  186. onClick={() => {
  187. setNewProjThresholdsPage(newProjThresholdsPage - 1);
  188. }}
  189. />
  190. <CurrentPage>
  191. {newProjThresholdsPage + 1} of{' '}
  192. {Math.ceil(projectsWithoutThresholds.length / PAGE_SIZE)}
  193. </CurrentPage>
  194. <Button
  195. icon={<IconChevron direction="right" size="sm" />}
  196. aria-label={t('Next')}
  197. size="sm"
  198. disabled={
  199. PAGE_SIZE * newProjThresholdsPage + PAGE_SIZE >=
  200. projectsWithoutThresholds.length
  201. }
  202. onClick={() => {
  203. setNewProjThresholdsPage(newProjThresholdsPage + 1);
  204. }}
  205. />
  206. </Paginator>
  207. </div>
  208. )}
  209. </Layout.Main>
  210. </Layout.Body>
  211. </NoProjectMessage>
  212. </PageFiltersContainer>
  213. );
  214. }
  215. export default ReleaseThresholdList;
  216. const FilterRow = styled('div')`
  217. display: flex;
  218. align-items: center;
  219. `;
  220. const ListError = styled('div')`
  221. color: red;
  222. margin: 0 ${space(2)};
  223. width: 100%;
  224. display: flex;
  225. justify-content: center;
  226. `;
  227. const ReleaseThresholdsPageFilterBar = styled(PageFilterBar)`
  228. margin-bottom: ${space(2)};
  229. `;
  230. const Paginator = styled('div')`
  231. margin: ${space(2)} 0;
  232. display: flex;
  233. justify-content: flex-end;
  234. align-items: center;
  235. `;
  236. const CurrentPage = styled('div')`
  237. margin: 0 ${space(1)};
  238. `;