eventCause.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {Fragment, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import flatMap from 'lodash/flatMap';
  4. import uniqBy from 'lodash/uniqBy';
  5. import {CommitRow} from 'sentry/components/commitRow';
  6. import {CauseHeader, DataSection} from 'sentry/components/events/styles';
  7. import {Panel} from 'sentry/components/panels';
  8. import {IconAdd, IconSubtract} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import space from 'sentry/styles/space';
  11. import type {AvatarProject, Group} from 'sentry/types';
  12. import type {Event} from 'sentry/types/event';
  13. import useCommitters from 'sentry/utils/useCommitters';
  14. interface Props {
  15. event: Event;
  16. project: AvatarProject;
  17. group?: Group;
  18. }
  19. function EventCause({group, event, project}: Props) {
  20. const [isExpanded, setIsExpanded] = useState(false);
  21. const {committers} = useCommitters({
  22. group,
  23. eventId: event.id,
  24. projectSlug: project.slug,
  25. });
  26. function getUniqueCommitsWithAuthors() {
  27. // Get a list of commits with author information attached
  28. const commitsWithAuthors = flatMap(committers, ({commits, author}) =>
  29. commits.map(commit => ({
  30. ...commit,
  31. author,
  32. }))
  33. );
  34. // Remove duplicate commits
  35. return uniqBy(commitsWithAuthors, commit => commit.id);
  36. }
  37. if (!committers.length) {
  38. return null;
  39. }
  40. const commits = getUniqueCommitsWithAuthors();
  41. return (
  42. <DataSection>
  43. <CauseHeader>
  44. <h3 data-test-id="event-cause">
  45. {t('Suspect Commits')} ({commits.length})
  46. </h3>
  47. {commits.length > 1 && (
  48. <ExpandButton onClick={() => setIsExpanded(!isExpanded)}>
  49. {isExpanded ? (
  50. <Fragment>
  51. {t('Show less')} <IconSubtract isCircled size="md" />
  52. </Fragment>
  53. ) : (
  54. <Fragment>
  55. {t('Show more')} <IconAdd isCircled size="md" />
  56. </Fragment>
  57. )}
  58. </ExpandButton>
  59. )}
  60. </CauseHeader>
  61. <Panel>
  62. {commits.slice(0, isExpanded ? 100 : 1).map(commit => (
  63. <CommitRow key={commit.id} commit={commit} />
  64. ))}
  65. </Panel>
  66. </DataSection>
  67. );
  68. }
  69. const ExpandButton = styled('button')`
  70. display: flex;
  71. align-items: center;
  72. & > svg {
  73. margin-left: ${space(0.5)};
  74. }
  75. `;
  76. export default EventCause;