header.tsx 8.8 KB

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