index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 I can access if -1 is the only selected project.
  82. * all member projects if 'my projects' (empty list) is selected.
  83. * all projects if the user is a superuser
  84. * the requested projects
  85. */
  86. const allProjectsSelectedICanAccess =
  87. selectedProjectIds.length === 1 &&
  88. selectedProjectIds[0] === String(ALL_ACCESS_PROJECTS) &&
  89. project.hasAccess;
  90. const myProjectsSelected = selectedProjectIds.length === 0 && project.isMember;
  91. const allMemberProjectsIfSuperuser =
  92. selectedProjectIds.length === 0 && user.isSuperuser;
  93. if (
  94. allProjectsSelectedICanAccess ||
  95. myProjectsSelected ||
  96. allMemberProjectsIfSuperuser ||
  97. selectedProjectIds.includes(project.id)
  98. ) {
  99. return project.environments;
  100. }
  101. return [];
  102. })
  103. );
  104. const envDiff = new Set([...allEnvSet].filter(x => !unSortedEnvs.has(x)));
  105. // bubble the selected projects envs first, then concat the rest of the envs
  106. return Array.from(unSortedEnvs)
  107. .sort()
  108. .concat([...envDiff].sort());
  109. }, [projects, selection.projects]);
  110. const getEnvironmentsAvailableToProject = useMemo((): string[] => {
  111. const selectedProjectIds = selection.projects.map(id => String(id));
  112. const allEnvSet = new Set(projects.flatMap(project => project.environments));
  113. // NOTE: mostly taken from environmentSelector.tsx
  114. const unSortedEnvs = new Set(
  115. projects.flatMap(project => {
  116. /**
  117. * Include environments from:
  118. * all projects if -1 is the only selected project.
  119. * all member projects if 'my projects' (empty list) is selected.
  120. * the requested projects
  121. */
  122. const allProjectsSelected =
  123. selectedProjectIds.length === 1 &&
  124. selectedProjectIds[0] === String(ALL_ACCESS_PROJECTS) &&
  125. project.hasAccess;
  126. const myProjectsSelected = selectedProjectIds.length === 0 && project.isMember;
  127. if (
  128. allProjectsSelected ||
  129. myProjectsSelected ||
  130. selectedProjectIds.includes(project.id)
  131. ) {
  132. return project.environments;
  133. }
  134. return [];
  135. })
  136. );
  137. const envDiff = new Set([...allEnvSet].filter(x => !unSortedEnvs.has(x)));
  138. // bubble the selected projects envs first, then concat the rest of the envs
  139. return Array.from(unSortedEnvs)
  140. .sort()
  141. .concat([...envDiff].sort());
  142. }, [projects, selection.projects]);
  143. /**
  144. * Thresholds filtered by environment selection
  145. * NOTE: currently no way to filter for 'None' environments
  146. */
  147. const filteredThresholds = selection.environments.length
  148. ? thresholds.filter(threshold => {
  149. return threshold.environment?.name
  150. ? selection.environments.indexOf(threshold.environment.name) > -1
  151. : !selection.environments.length;
  152. })
  153. : thresholds;
  154. const thresholdsByProject: {[key: string]: Threshold[]} = useMemo(() => {
  155. const byProj = {};
  156. filteredThresholds.forEach(threshold => {
  157. const projId = threshold.project.id;
  158. if (!byProj[projId]) {
  159. byProj[projId] = [];
  160. }
  161. byProj[projId].push(threshold);
  162. });
  163. return byProj;
  164. }, [filteredThresholds]);
  165. const projectsWithoutThresholds: Project[] = useMemo(() => {
  166. // TODO: limit + paginate list
  167. return selectedProjects.filter(proj => !thresholdsByProject[proj.id]);
  168. }, [thresholdsByProject, selectedProjects]);
  169. const setTempError = msg => {
  170. setListError(msg);
  171. setTimeout(() => setListError(''), 5000);
  172. };
  173. if (isError) {
  174. return <LoadingError onRetry={refetch} message={requestError.message} />;
  175. }
  176. if (isLoading) {
  177. return <LoadingIndicator />;
  178. }
  179. return (
  180. <PageFiltersContainer>
  181. <NoProjectMessage organization={organization}>
  182. <Header router={router} hasV2ReleaseUIEnabled organization={organization} />
  183. <Layout.Body>
  184. <Layout.Main fullWidth>
  185. <FilterRow>
  186. <ReleaseThresholdsPageFilterBar condensed>
  187. <GuideAnchor target="release_projects">
  188. <ProjectPageFilter />
  189. </GuideAnchor>
  190. <EnvironmentPageFilter />
  191. </ReleaseThresholdsPageFilterBar>
  192. <ListError>{listError}</ListError>
  193. </FilterRow>
  194. {thresholdsByProject &&
  195. Object.entries(thresholdsByProject).map(([projId, thresholdsByProj]) => (
  196. <ThresholdGroupTable
  197. key={projId}
  198. project={projectsById[projId]}
  199. thresholds={thresholdsByProj}
  200. isLoading={isLoading}
  201. isError={isError}
  202. refetch={refetch}
  203. setTempError={setTempError}
  204. allEnvironmentNames={getEnvironmentsAvailableToProject}
  205. />
  206. ))}
  207. {projectsWithoutThresholds.length > 0 && (
  208. <div>
  209. <strong>Projects without Thresholds</strong>
  210. {projectsWithoutThresholds
  211. .slice(
  212. PAGE_SIZE * newProjThresholdsPage,
  213. PAGE_SIZE * newProjThresholdsPage + PAGE_SIZE
  214. )
  215. .map(proj => (
  216. <NoThresholdCard
  217. key={proj.id}
  218. project={proj}
  219. allEnvironmentNames={getAllEnvironmentNames}
  220. refetch={refetch}
  221. setTempError={setTempError}
  222. />
  223. ))}
  224. <Paginator>
  225. <Button
  226. icon={<IconChevron direction="left" size="sm" />}
  227. aria-label={t('Previous')}
  228. size="sm"
  229. disabled={newProjThresholdsPage === 0}
  230. onClick={() => {
  231. setNewProjThresholdsPage(newProjThresholdsPage - 1);
  232. }}
  233. />
  234. <CurrentPage>
  235. {newProjThresholdsPage + 1} of{' '}
  236. {Math.ceil(projectsWithoutThresholds.length / PAGE_SIZE)}
  237. </CurrentPage>
  238. <Button
  239. icon={<IconChevron direction="right" size="sm" />}
  240. aria-label={t('Next')}
  241. size="sm"
  242. disabled={
  243. PAGE_SIZE * newProjThresholdsPage + PAGE_SIZE >=
  244. projectsWithoutThresholds.length
  245. }
  246. onClick={() => {
  247. setNewProjThresholdsPage(newProjThresholdsPage + 1);
  248. }}
  249. />
  250. </Paginator>
  251. </div>
  252. )}
  253. </Layout.Main>
  254. </Layout.Body>
  255. </NoProjectMessage>
  256. </PageFiltersContainer>
  257. );
  258. }
  259. export default ReleaseThresholdList;
  260. const FilterRow = styled('div')`
  261. display: flex;
  262. align-items: center;
  263. `;
  264. const ListError = styled('div')`
  265. color: red;
  266. margin: 0 ${space(2)};
  267. width: 100%;
  268. display: flex;
  269. justify-content: center;
  270. `;
  271. const ReleaseThresholdsPageFilterBar = styled(PageFilterBar)`
  272. margin-bottom: ${space(2)};
  273. `;
  274. const Paginator = styled('div')`
  275. margin: ${space(2)} 0;
  276. display: flex;
  277. justify-content: flex-end;
  278. align-items: center;
  279. `;
  280. const CurrentPage = styled('div')`
  281. margin: 0 ${space(1)};
  282. `;