index.tsx 9.2 KB

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