rulesPanel.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import React from 'react';
  2. import TextareaAutosize from 'react-autosize-textarea';
  3. import styled from '@emotion/styled';
  4. import {withTheme} from 'emotion-theming';
  5. import moment from 'moment';
  6. import {Panel, PanelBody, PanelHeader} from 'app/components/panels';
  7. import Tag from 'app/components/tag';
  8. import {IconGithub, IconGitlab, IconSentry} from 'app/icons';
  9. import {inputStyles} from 'app/styles/input';
  10. import space from 'app/styles/space';
  11. type Props = {
  12. raw: string;
  13. dateUpdated: string | null;
  14. provider?: string;
  15. repoName?: string;
  16. readOnly?: boolean;
  17. type: 'codeowners' | 'issueowners';
  18. placeholder?: string;
  19. detail?: React.ReactNode;
  20. controls?: React.ReactNode[];
  21. 'data-test-id': string;
  22. };
  23. type State = {};
  24. class RulesPanel extends React.Component<Props, State> {
  25. renderIcon(provider: string) {
  26. switch (provider) {
  27. case 'github':
  28. return <IconGithub size="md" />;
  29. case 'gitlab':
  30. return <IconGitlab size="md" />;
  31. default:
  32. return <IconSentry size="md" />;
  33. }
  34. }
  35. renderTitle() {
  36. switch (this.props.type) {
  37. case 'codeowners':
  38. return 'CODEOWNERS';
  39. case 'issueowners':
  40. return 'Ownership Rules';
  41. default:
  42. return null;
  43. }
  44. }
  45. render() {
  46. const {
  47. raw,
  48. dateUpdated,
  49. provider,
  50. repoName,
  51. readOnly,
  52. placeholder,
  53. detail,
  54. controls,
  55. ['data-test-id']: dataTestId,
  56. } = this.props;
  57. return (
  58. <Container data-test-id={dataTestId}>
  59. <RulesHeader>
  60. <TitleContainer>
  61. {this.renderIcon(provider ?? '')}
  62. <Title>{this.renderTitle()}</Title>
  63. </TitleContainer>
  64. {readOnly && <ReadOnlyTag type="default">{'Read Only'}</ReadOnlyTag>}
  65. {repoName && <Repository>{repoName}</Repository>}
  66. {detail && <Detail>{detail}</Detail>}
  67. </RulesHeader>
  68. <RulesView>
  69. <InnerPanel>
  70. <InnerPanelHeader>
  71. <SyncDate>
  72. {dateUpdated && `Last synced ${moment(dateUpdated).fromNow()}`}
  73. </SyncDate>
  74. <Controls>
  75. {(controls || []).map((c, n) => (
  76. <span key={n}> {c}</span>
  77. ))}
  78. </Controls>
  79. </InnerPanelHeader>
  80. <InnerPanelBody>
  81. <StyledTextArea
  82. value={raw}
  83. spellCheck="false"
  84. autoComplete="off"
  85. autoCorrect="off"
  86. autoCapitalize="off"
  87. placeholder={placeholder}
  88. />
  89. </InnerPanelBody>
  90. </InnerPanel>
  91. </RulesView>
  92. </Container>
  93. );
  94. }
  95. }
  96. export default withTheme(RulesPanel);
  97. const Container = styled('div')`
  98. display: grid;
  99. grid-template-columns: 1fr 2fr;
  100. grid-template-areas: 'rules-header rules-view';
  101. margin-bottom: ${space(3)};
  102. `;
  103. const RulesHeader = styled('div')`
  104. grid-area: rules-header;
  105. display: grid;
  106. grid-template-columns: 2fr 1fr;
  107. grid-template-rows: 45px 1fr 1fr 1fr 1fr;
  108. grid-template-areas: 'title tag' 'repository repository' '. .' '. .' 'detail detail';
  109. border: 1px solid #c6becf;
  110. border-radius: 4px 0 0 4px;
  111. border-right: none;
  112. box-shadow: 0 2px 0 rgb(37 11 54 / 4%);
  113. background-color: #ffffff;
  114. `;
  115. const TitleContainer = styled('div')`
  116. grid-area: title;
  117. align-self: center;
  118. padding-left: ${space(2)};
  119. display: flex;
  120. * {
  121. padding-right: ${space(0.5)};
  122. }
  123. `;
  124. const Title = styled('div')`
  125. align-self: center;
  126. `;
  127. const ReadOnlyTag = styled(Tag)`
  128. grid-area: tag;
  129. align-self: center;
  130. justify-self: end;
  131. padding-right: ${space(1)};
  132. `;
  133. const Repository = styled('div')`
  134. grid-area: repository;
  135. padding-left: calc(${space(2)} + ${space(3)});
  136. color: #9386a0;
  137. font-size: 14px;
  138. `;
  139. const Detail = styled('div')`
  140. grid-area: detail;
  141. align-self: end;
  142. padding: 0 ${space(2)} ${space(2)} ${space(2)};
  143. color: #9386a0;
  144. font-size: 14px;
  145. line-height: 1.4;
  146. `;
  147. const RulesView = styled('div')`
  148. grid-area: rules-view;
  149. `;
  150. const InnerPanel = styled(Panel)`
  151. border-top-left-radius: 0;
  152. border-bottom-left-radius: 0px;
  153. margin: 0px;
  154. height: 100%;
  155. `;
  156. const InnerPanelHeader = styled(PanelHeader)`
  157. display: grid;
  158. grid-template-columns: 2fr 1fr;
  159. grid-template-areas: 'sync-date controis';
  160. text-transform: none;
  161. font-size: 16px;
  162. font-weight: 400;
  163. `;
  164. const InnerPanelBody = styled(PanelBody)`
  165. height: auto;
  166. `;
  167. const StyledTextArea = styled(TextareaAutosize)`
  168. ${p => inputStyles(p)};
  169. height: 350px !important;
  170. overflow: auto;
  171. outline: 0;
  172. width: 100%;
  173. resize: none;
  174. margin: 0;
  175. font-family: ${p => p.theme.text.familyMono};
  176. word-break: break-all;
  177. white-space: pre-wrap;
  178. line-height: ${space(3)};
  179. border: none;
  180. box-shadow: none;
  181. padding: ${space(2)};
  182. color: transparent;
  183. text-shadow: 0 0 0 #9386a0;
  184. &:hover,
  185. &:focus,
  186. &:active {
  187. border: none;
  188. box-shadow: none;
  189. }
  190. `;
  191. const SyncDate = styled('div')`
  192. grid-area: sync-date;
  193. `;
  194. const Controls = styled('div')`
  195. display: grid;
  196. align-items: center;
  197. grid-gap: ${space(1)};
  198. grid-auto-flow: column;
  199. justify-content: flex-end;
  200. `;