All files / app/containers Authors.tsx

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85                                    2x   2x 2x 2x 2x 2x   2x 2x   2x                                           14x   14x               14x                                              
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { debug } from 'app/utilities/logger';
 
import { highlightCommits, setOpenSidePanelGroup } from 'app/actions';
import { CollapsibleSidePanelGroups } from 'app/actions/ActionTypes';
import { getAuthorsStats } from 'app/selectors/authors';
import {
  getHighlightedCommitIds,
  getOpenSidePanelGroup,
} from 'app/selectors/stateVars';
 
import { CollapsibleGroup } from 'app/components/CollapsibleGroup';
import { AuthorCard } from 'app/components/AuthorCard';
import { ExtendingList } from 'app/components/ExtendingList';
 
import AuthorsActionMenu from 'app/containers/AuthorsActionMenu';
 
const COLLAPSIBLE_GROUP = CollapsibleSidePanelGroups.AUTHORS;
 
export const Authors: React.FC = (): React.ReactElement => {
  const highlightedCommitIds = useSelector(getHighlightedCommitIds);
  const authorStats = useSelector(getAuthorsStats);
  const openGroup = useSelector(getOpenSidePanelGroup);
  const dispatch = useDispatch();
 
  const groupTitle = `${authorStats.authors.length} Authors`;
  const isOpen = openGroup === COLLAPSIBLE_GROUP;
 
  return (
    <CollapsibleGroup
      title={groupTitle}
      onOpenToggle={handleOpenGroupToggle}
      isOpen={isOpen}
    >
      <AuthorsActionMenu />
      <ExtendingList
        rowCount={authorStats.authors.length}
        rowRenderer={renderRow}
      />
    </CollapsibleGroup>
  );
 
  function handleOpenGroupToggle() {
    if (!isOpen) {
      dispatch(setOpenSidePanelGroup(COLLAPSIBLE_GROUP));
    }
  }
 
  function renderRow(index: number, key: string) {
    // debug('render row', row);
    const author = authorStats.authors[index];
    const isHighlighted =
      highlightedCommitIds &&
      highlightedCommitIds.length > 0 &&
      author.commits
        .map(c => {
          return c.id;
        })
        .includes(highlightedCommitIds[0]);
 
    return (
      <AuthorCard
        key={key}
        index={index}
        author={author}
        totalLinesAdded={authorStats.totalLinesAdded}
        totalLinesDeleted={authorStats.totalLinesDeleted}
        totalCommits={authorStats.totalCommits}
        maxImpact={authorStats.maxImpact}
        maxCommits={authorStats.maxCommits}
        isHighlighted={isHighlighted}
        onClick={onAuthorClick}
      />
    );
  }
 
  function onAuthorClick(_evt, author) {
    const commitIds = author.commits.map(commit => {
      return commit.id;
    });
    dispatch(highlightCommits(commitIds));
  }
};