index.tsx 9.3 KB

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