index.tsx 11 KB

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