eventCause.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import flatMap from 'lodash/flatMap';
  4. import uniqBy from 'lodash/uniqBy';
  5. import {Client} from 'app/api';
  6. import CommitRow from 'app/components/commitRow';
  7. import {CauseHeader, DataSection} from 'app/components/events/styles';
  8. import {Panel} from 'app/components/panels';
  9. import {IconAdd, IconSubtract} from 'app/icons';
  10. import {t} from 'app/locale';
  11. import space from 'app/styles/space';
  12. import {AvatarProject, Committer, Group, Organization} from 'app/types';
  13. import {Event} from 'app/types/event';
  14. import withApi from 'app/utils/withApi';
  15. import withCommitters from 'app/utils/withCommitters';
  16. type Props = {
  17. // injected by HoC
  18. committers: Committer[];
  19. api: Client;
  20. // needed by HoC
  21. organization: Organization;
  22. project: AvatarProject;
  23. event: Event;
  24. group?: Group;
  25. };
  26. type State = {
  27. expanded: boolean;
  28. };
  29. class EventCause extends Component<Props, State> {
  30. state: State = {
  31. expanded: false,
  32. };
  33. getUniqueCommitsWithAuthors() {
  34. const {committers} = this.props;
  35. // Get a list of commits with author information attached
  36. const commitsWithAuthors = flatMap(committers, ({commits, author}) =>
  37. commits.map(commit => ({
  38. ...commit,
  39. author,
  40. }))
  41. );
  42. // Remove duplicate commits
  43. const uniqueCommitsWithAuthors = uniqBy(commitsWithAuthors, commit => commit.id);
  44. return uniqueCommitsWithAuthors;
  45. }
  46. render() {
  47. const {committers} = this.props;
  48. const {expanded} = this.state;
  49. if (!(committers && committers.length)) {
  50. return null;
  51. }
  52. const commits = this.getUniqueCommitsWithAuthors();
  53. return (
  54. <DataSection>
  55. <CauseHeader>
  56. <h3>
  57. {t('Suspect Commits')} ({commits.length})
  58. </h3>
  59. {commits.length > 1 && (
  60. <ExpandButton onClick={() => this.setState({expanded: !expanded})}>
  61. {expanded ? (
  62. <Fragment>
  63. {t('Show less')} <IconSubtract isCircled size="md" />
  64. </Fragment>
  65. ) : (
  66. <Fragment>
  67. {t('Show more')} <IconAdd isCircled size="md" />
  68. </Fragment>
  69. )}
  70. </ExpandButton>
  71. )}
  72. </CauseHeader>
  73. <Panel>
  74. {commits.slice(0, expanded ? 100 : 1).map(commit => (
  75. <CommitRow key={commit.id} commit={commit} />
  76. ))}
  77. </Panel>
  78. </DataSection>
  79. );
  80. }
  81. }
  82. const ExpandButton = styled('button')`
  83. display: flex;
  84. align-items: center;
  85. & > svg {
  86. margin-left: ${space(0.5)};
  87. }
  88. `;
  89. export default withApi(withCommitters(EventCause));