header.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import * as React from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import isPropValid from '@emotion/is-prop-valid';
  4. import styled from '@emotion/styled';
  5. import moment from 'moment';
  6. import Breadcrumbs from 'app/components/breadcrumbs';
  7. import Count from 'app/components/count';
  8. import DropdownControl from 'app/components/dropdownControl';
  9. import Duration from 'app/components/duration';
  10. import ProjectBadge from 'app/components/idBadge/projectBadge';
  11. import LoadingError from 'app/components/loadingError';
  12. import MenuItem from 'app/components/menuItem';
  13. import PageHeading from 'app/components/pageHeading';
  14. import Placeholder from 'app/components/placeholder';
  15. import SubscribeButton from 'app/components/subscribeButton';
  16. import {IconCheckmark} from 'app/icons';
  17. import {t} from 'app/locale';
  18. import {PageHeader} from 'app/styles/organization';
  19. import space from 'app/styles/space';
  20. import {use24Hours} from 'app/utils/dates';
  21. import getDynamicText from 'app/utils/getDynamicText';
  22. import Projects from 'app/utils/projects';
  23. import {Dataset} from 'app/views/alerts/incidentRules/types';
  24. import Status from '../status';
  25. import {Incident, IncidentStats} from '../types';
  26. import {isOpen} from '../utils';
  27. type Props = Pick<RouteComponentProps<{orgId: string}, {}>, 'params'> & {
  28. className?: string;
  29. hasIncidentDetailsError: boolean;
  30. incident?: Incident;
  31. stats?: IncidentStats;
  32. onSubscriptionChange: (event: React.MouseEvent) => void;
  33. onStatusChange: (eventKey: any) => void;
  34. };
  35. export default class DetailsHeader extends React.Component<Props> {
  36. renderStatus() {
  37. const {incident, onStatusChange} = this.props;
  38. const isIncidentOpen = incident && isOpen(incident);
  39. const statusLabel = incident ? <StyledStatus incident={incident} /> : null;
  40. return (
  41. <DropdownControl
  42. data-test-id="status-dropdown"
  43. label={statusLabel}
  44. alignRight
  45. blendWithActor={false}
  46. buttonProps={{
  47. size: 'small',
  48. disabled: !incident || !isIncidentOpen,
  49. hideBottomBorder: false,
  50. }}
  51. >
  52. <StatusMenuItem isActive>
  53. {incident && <Status disableIconColor incident={incident} />}
  54. </StatusMenuItem>
  55. <StatusMenuItem onSelect={onStatusChange}>
  56. <IconCheckmark color="green300" />
  57. {t('Resolved')}
  58. </StatusMenuItem>
  59. </DropdownControl>
  60. );
  61. }
  62. render() {
  63. const {hasIncidentDetailsError, incident, params, stats, onSubscriptionChange} =
  64. this.props;
  65. const isIncidentReady = !!incident && !hasIncidentDetailsError;
  66. // ex - Wed, May 27, 2020 11:09 AM
  67. const dateFormat = use24Hours() ? 'ddd, MMM D, YYYY HH:mm' : 'llll';
  68. const dateStarted =
  69. incident && moment(new Date(incident.dateStarted)).format(dateFormat);
  70. const duration =
  71. incident &&
  72. moment(incident.dateClosed ? new Date(incident.dateClosed) : new Date()).diff(
  73. moment(new Date(incident.dateStarted)),
  74. 'seconds'
  75. );
  76. const isErrorDataset = incident?.alertRule?.dataset === Dataset.ERRORS;
  77. const environmentLabel = incident?.alertRule?.environment ?? t('All Environments');
  78. const project = incident && incident.projects && incident.projects[0];
  79. return (
  80. <Header>
  81. <BreadCrumbBar>
  82. <AlertBreadcrumbs
  83. crumbs={[
  84. {label: t('Alerts'), to: `/organizations/${params.orgId}/alerts/`},
  85. {label: incident && `#${incident.id}`},
  86. ]}
  87. />
  88. <Controls>
  89. <SubscribeButton
  90. disabled={!isIncidentReady}
  91. isSubscribed={incident?.isSubscribed}
  92. onClick={onSubscriptionChange}
  93. size="small"
  94. />
  95. {this.renderStatus()}
  96. </Controls>
  97. </BreadCrumbBar>
  98. <Details columns={isErrorDataset ? 5 : 3}>
  99. <div>
  100. <IncidentTitle data-test-id="incident-title" loading={!isIncidentReady}>
  101. {incident && !hasIncidentDetailsError ? incident.title : 'Loading'}
  102. </IncidentTitle>
  103. <IncidentSubTitle loading={!isIncidentReady}>
  104. {t('Triggered: ')}
  105. {dateStarted}
  106. </IncidentSubTitle>
  107. </div>
  108. {hasIncidentDetailsError ? (
  109. <StyledLoadingError />
  110. ) : (
  111. <GroupedHeaderItems columns={isErrorDataset ? 5 : 3}>
  112. <ItemTitle>{t('Environment')}</ItemTitle>
  113. <ItemTitle>{t('Project')}</ItemTitle>
  114. {isErrorDataset && <ItemTitle>{t('Users affected')}</ItemTitle>}
  115. {isErrorDataset && <ItemTitle>{t('Total events')}</ItemTitle>}
  116. <ItemTitle>{t('Active For')}</ItemTitle>
  117. <ItemValue>{environmentLabel}</ItemValue>
  118. <ItemValue>
  119. {project ? (
  120. <Projects slugs={[project]} orgId={params.orgId}>
  121. {({projects}) =>
  122. projects?.length && (
  123. <ProjectBadge avatarSize={18} project={projects[0]} />
  124. )
  125. }
  126. </Projects>
  127. ) : (
  128. <Placeholder height="25px" />
  129. )}
  130. </ItemValue>
  131. {isErrorDataset && (
  132. <ItemValue>
  133. {stats ? (
  134. <Count value={stats.uniqueUsers} />
  135. ) : (
  136. <Placeholder height="25px" />
  137. )}
  138. </ItemValue>
  139. )}
  140. {isErrorDataset && (
  141. <ItemValue>
  142. {stats ? (
  143. <Count value={stats.totalEvents} />
  144. ) : (
  145. <Placeholder height="25px" />
  146. )}
  147. </ItemValue>
  148. )}
  149. <ItemValue>
  150. {incident ? (
  151. <Duration
  152. seconds={getDynamicText({value: duration || 0, fixed: 1200})}
  153. />
  154. ) : (
  155. <Placeholder height="25px" />
  156. )}
  157. </ItemValue>
  158. </GroupedHeaderItems>
  159. )}
  160. </Details>
  161. </Header>
  162. );
  163. }
  164. }
  165. const Header = styled('div')`
  166. background-color: ${p => p.theme.backgroundSecondary};
  167. border-bottom: 1px solid ${p => p.theme.border};
  168. `;
  169. const BreadCrumbBar = styled('div')`
  170. display: flex;
  171. margin-bottom: 0;
  172. padding: ${space(2)} ${space(4)} ${space(1)};
  173. `;
  174. const AlertBreadcrumbs = styled(Breadcrumbs)`
  175. flex-grow: 1;
  176. font-size: ${p => p.theme.fontSizeExtraLarge};
  177. padding: 0;
  178. `;
  179. const Controls = styled('div')`
  180. display: grid;
  181. grid-auto-flow: column;
  182. grid-gap: ${space(1)};
  183. `;
  184. const Details = styled(PageHeader, {
  185. shouldForwardProp: p => typeof p === 'string' && isPropValid(p) && p !== 'columns',
  186. })<{columns: 3 | 5}>`
  187. margin-bottom: 0;
  188. padding: ${space(1.5)} ${space(4)} ${space(2)};
  189. grid-template-columns: max-content auto;
  190. display: grid;
  191. grid-gap: ${space(3)};
  192. grid-auto-flow: column;
  193. @media (max-width: ${p => p.theme.breakpoints[p.columns === 3 ? 1 : 2]}) {
  194. grid-template-columns: auto;
  195. grid-auto-flow: row;
  196. }
  197. `;
  198. const StyledLoadingError = styled(LoadingError)`
  199. flex: 1;
  200. &.alert.alert-block {
  201. margin: 0 20px;
  202. }
  203. `;
  204. const GroupedHeaderItems = styled('div', {
  205. shouldForwardProp: p => typeof p === 'string' && isPropValid(p) && p !== 'columns',
  206. })<{columns: 3 | 5}>`
  207. display: grid;
  208. grid-template-columns: repeat(${p => p.columns}, max-content);
  209. grid-gap: ${space(1)} ${space(4)};
  210. text-align: right;
  211. margin-top: ${space(1)};
  212. @media (max-width: ${p => p.theme.breakpoints[p.columns === 3 ? 1 : 2]}) {
  213. text-align: left;
  214. }
  215. `;
  216. const ItemTitle = styled('h6')`
  217. font-size: ${p => p.theme.fontSizeSmall};
  218. margin-bottom: 0;
  219. text-transform: uppercase;
  220. color: ${p => p.theme.gray300};
  221. letter-spacing: 0.1px;
  222. `;
  223. const ItemValue = styled('div')`
  224. display: flex;
  225. justify-content: flex-end;
  226. align-items: center;
  227. font-size: ${p => p.theme.fontSizeExtraLarge};
  228. `;
  229. const IncidentTitle = styled(PageHeading, {
  230. shouldForwardProp: p => typeof p === 'string' && isPropValid(p) && p !== 'loading',
  231. })<{loading: boolean}>`
  232. ${p => p.loading && 'opacity: 0'};
  233. line-height: 1.5;
  234. `;
  235. const IncidentSubTitle = styled('div', {
  236. shouldForwardProp: p => typeof p === 'string' && isPropValid(p) && p !== 'loading',
  237. })<{loading: boolean}>`
  238. ${p => p.loading && 'opacity: 0'};
  239. font-size: ${p => p.theme.fontSizeLarge};
  240. color: ${p => p.theme.gray300};
  241. `;
  242. const StyledStatus = styled(Status)`
  243. margin-right: ${space(2)};
  244. `;
  245. const StatusMenuItem = styled(MenuItem)`
  246. > span {
  247. padding: ${space(1)} ${space(1.5)};
  248. font-size: ${p => p.theme.fontSizeSmall};
  249. font-weight: 600;
  250. line-height: 1;
  251. text-align: left;
  252. display: grid;
  253. grid-template-columns: max-content 1fr;
  254. grid-gap: ${space(0.75)};
  255. align-items: center;
  256. }
  257. `;