eventDataSection.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import * as React from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import Button from 'app/components/button';
  5. import ButtonBar from 'app/components/buttonBar';
  6. import {DataSection} from 'app/components/events/styles';
  7. import {IconAnchor} from 'app/icons/iconAnchor';
  8. import {t} from 'app/locale';
  9. import space from 'app/styles/space';
  10. import {callIfFunction} from 'app/utils/callIfFunction';
  11. const defaultProps = {
  12. wrapTitle: true,
  13. raw: false,
  14. isCentered: false,
  15. showPermalink: true,
  16. };
  17. type DefaultProps = Readonly<typeof defaultProps>;
  18. type Props = {
  19. className?: string;
  20. title: React.ReactNode;
  21. type: string;
  22. toggleRaw?: (enable: boolean) => void;
  23. actions?: React.ReactNode;
  24. } & DefaultProps;
  25. class EventDataSection extends React.Component<Props> {
  26. static defaultProps = defaultProps;
  27. componentDidMount() {
  28. const dataSectionDOM = this.dataSectionDOMRef.current;
  29. if (location.hash && dataSectionDOM) {
  30. const [, hash] = location.hash.split('#');
  31. try {
  32. const anchorElement = hash && dataSectionDOM.querySelector('div#' + hash);
  33. if (anchorElement) {
  34. anchorElement.scrollIntoView();
  35. }
  36. } catch {
  37. // Since we're blindly taking the hash from the url and shoving
  38. // it into a querySelector, it's possible that this may
  39. // raise an exception if the input is invalid. So let's just ignore
  40. // this instead of blowing up.
  41. // e.g. `document.querySelector('div#=')`
  42. // > Uncaught DOMException: Failed to execute 'querySelector' on 'Document': 'div#=' is not a valid selector.
  43. }
  44. }
  45. }
  46. dataSectionDOMRef = React.createRef<HTMLDivElement>();
  47. render() {
  48. const {
  49. children,
  50. className,
  51. type,
  52. title,
  53. toggleRaw,
  54. raw,
  55. wrapTitle,
  56. actions,
  57. isCentered,
  58. showPermalink,
  59. } = this.props;
  60. const titleNode = wrapTitle ? <h3>{title}</h3> : title;
  61. return (
  62. <DataSection ref={this.dataSectionDOMRef} className={className || ''}>
  63. {title && (
  64. <SectionHeader id={type} isCentered={isCentered}>
  65. <Title>
  66. {showPermalink ? (
  67. <Permalink href={'#' + type} className="permalink">
  68. <StyledIconAnchor />
  69. {titleNode}
  70. </Permalink>
  71. ) : (
  72. <div>{titleNode}</div>
  73. )}
  74. </Title>
  75. {type === 'extra' && (
  76. <ButtonBar merged active={raw ? 'raw' : 'formatted'}>
  77. <Button
  78. barId="formatted"
  79. size="xsmall"
  80. onClick={() => callIfFunction(toggleRaw, false)}
  81. >
  82. {t('Formatted')}
  83. </Button>
  84. <Button
  85. barId="raw"
  86. size="xsmall"
  87. onClick={() => callIfFunction(toggleRaw, true)}
  88. >
  89. {t('Raw')}
  90. </Button>
  91. </ButtonBar>
  92. )}
  93. {actions && <ActionContainer>{actions}</ActionContainer>}
  94. </SectionHeader>
  95. )}
  96. <SectionContents>{children}</SectionContents>
  97. </DataSection>
  98. );
  99. }
  100. }
  101. const Title = styled('div')`
  102. display: flex;
  103. `;
  104. const StyledIconAnchor = styled(IconAnchor)`
  105. display: none;
  106. position: absolute;
  107. top: 4px;
  108. left: -22px;
  109. `;
  110. const Permalink = styled('a')`
  111. :hover ${StyledIconAnchor} {
  112. display: block;
  113. color: ${p => p.theme.gray300};
  114. }
  115. `;
  116. const SectionHeader = styled('div')<{isCentered?: boolean}>`
  117. display: flex;
  118. flex-wrap: wrap;
  119. align-items: center;
  120. margin-bottom: ${space(1)};
  121. > * {
  122. margin-bottom: ${space(0.5)};
  123. }
  124. & h3,
  125. & h3 a {
  126. font-size: 14px;
  127. font-weight: 600;
  128. line-height: 1.2;
  129. color: ${p => p.theme.gray300};
  130. }
  131. & h3 {
  132. font-size: 14px;
  133. font-weight: 600;
  134. line-height: 1.2;
  135. padding: ${space(0.75)} 0;
  136. margin-bottom: 0;
  137. text-transform: uppercase;
  138. }
  139. & small {
  140. color: ${p => p.theme.textColor};
  141. font-size: ${p => p.theme.fontSizeMedium};
  142. margin-right: ${space(0.5)};
  143. margin-left: ${space(0.5)};
  144. text-transform: none;
  145. }
  146. & small > span {
  147. color: ${p => p.theme.textColor};
  148. border-bottom: 1px dotted ${p => p.theme.border};
  149. font-weight: normal;
  150. }
  151. @media (min-width: ${props => props.theme.breakpoints[2]}) {
  152. & > small {
  153. margin-left: ${space(1)};
  154. display: inline-block;
  155. }
  156. }
  157. ${p =>
  158. p.isCentered &&
  159. css`
  160. align-items: center;
  161. @media (max-width: ${p.theme.breakpoints[0]}) {
  162. display: block;
  163. }
  164. `}
  165. >*:first-child {
  166. position: relative;
  167. flex-grow: 1;
  168. }
  169. `;
  170. const SectionContents = styled('div')`
  171. position: relative;
  172. `;
  173. const ActionContainer = styled('div')`
  174. flex-shrink: 0;
  175. max-width: 100%;
  176. `;
  177. export default EventDataSection;