index.tsx 9.1 KB

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