All files / app/containers DifferenceViewerHeader.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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184                                              3x               3x           3x         3x 4x 4x 4x 4x 4x 4x 4x 4x   4x                                               8x       8x           8x                                         8x         8x             8x     8x                                                                                                                          
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
 
import { style } from 'app/styles';
 
import {
  getStartDate,
  getEndDate,
  getDiffLeftCommit,
  getDiffRightCommit,
  getSelectedPath,
  getHasUncommittedChanges,
} from 'app/selectors/stateVars';
import {
  getCommitsForTimeplot,
  getFilteredCommits,
} from 'app/selectors/commits';
import { setDates } from 'app/actions/setDates';
 
import { RevSelector } from 'app/components/RevSelector';
import { EpochDateTime } from 'app/components/EpochDateTime';
import { fetchDiff } from 'app/actions/diff';
 
const outerStyle = {
  _extends: ['flexRow'],
  flexGrow: 0,
  flexShrink: 0,
  background: '@colors.altBackground',
  color: '@colors.altForeground',
};
 
const revChildrenStyle = {
  _extends: 'flexColumn',
  alignItems: 'center',
  minWidth: 160,
};
 
const revSelectorStyle = {
  flexGrow: 1,
  textAlign: 'center',
};
 
export const DifferenceViewerHeader: React.FC = (): React.ReactElement => {
  const selectedPath = useSelector(getSelectedPath);
  const startDate = useSelector(getStartDate);
  const endDate = useSelector(getEndDate);
  const leftCommit = useSelector(getDiffLeftCommit);
  const rightCommit = useSelector(getDiffRightCommit);
  const timeplotCommits = useSelector(getCommitsForTimeplot);
  const hasUncommittedChanges = useSelector(getHasUncommittedChanges);
  const dispatch = useDispatch();
 
  return (
    <div style={style(outerStyle)}>
      <RevSelector
        style={style(revSelectorStyle)}
        disablePrevious={shouldDisablePrevious(leftCommit)}
        disableNext={shouldDisableNext(leftCommit)}
        onNextRevClick={onLeftRevNext}
        onPreviousRevClick={onLeftRevPrevious}
      >
        {renderRevChildren('left', leftCommit)}
      </RevSelector>
      <RevSelector
        style={style(revSelectorStyle)}
        disablePrevious={shouldDisablePrevious(rightCommit)}
        disableNext={shouldDisableNext(rightCommit)}
        onNextRevClick={onRightRevNext}
        onPreviousRevClick={onRightRevPrevious}
      >
        {renderRevChildren('right', rightCommit)}
      </RevSelector>
    </div>
  );
 
  function renderRevChildren(which, _commit) {
    Iif (!timeplotCommits || timeplotCommits.length === 0) {
      return null;
    }
    const commit =
      which === 'left' && !_commit
        ? timeplotCommits[0]
        : which === 'right' && !_commit && !hasUncommittedChanges
          ? timeplotCommits[0]
          : _commit;
 
    return (
      <div style={style(revChildrenStyle)}>
        {commit ? (
          <>
            <div style={style('flexRow')}>
              <EpochDateTime value={commit.authorDate} />
            </div>
            <div style={style('flexRow')}>
              (#
              {commit.hash})
              {commit.id === timeplotCommits[0].id && ' (Local HEAD)'}
            </div>
          </>
        ) : (
          <div style={style('flexRow')}>Uncommitted Changes</div>
        )}
      </div>
    );
  }
 
  function shouldDisablePrevious(commit) {
    Iif (!timeplotCommits) {
      return true;
    }
    // a null left or right commit means it's the local changes
    // or the HEAD rev and there should be previous commits
    return (
      (commit && commit === timeplotCommits[timeplotCommits.length - 1]) ||
      (commit && commit === rightCommit && rightCommit === leftCommit)
    );
  }
  function shouldDisableNext(commit) {
    // a null commit means it's the latest rev
    Iif (!timeplotCommits || !commit) {
      return true;
    }
    return commit === null;
  }
 
  function onLeftRevPrevious() {
    setLeftRev(-1);
  }
 
  function onLeftRevNext() {
    setLeftRev(1);
  }
 
  function setLeftRev(relativeIndex) {
    if (!timeplotCommits || timeplotCommits.length === 0) {
      return;
    }
    let newLeftCommit = null;
    if (leftCommit) {
      const foundIndex = timeplotCommits.findIndex(commit => {
        return commit.id === leftCommit.id;
      });
      if (foundIndex !== -1) {
        newLeftCommit = timeplotCommits[foundIndex - relativeIndex];
      }
    } else if (relativeIndex < 0) {
      newLeftCommit = timeplotCommits[2];
    }
    if (newLeftCommit) {
      dispatch(setDates((newLeftCommit.authorDate + 1) * 1000, endDate * 1000));
    }
  }
 
  function onRightRevPrevious() {
    setRightRev(-1);
  }
 
  function onRightRevNext() {
    setRightRev(1);
  }
 
  function setRightRev(relativeIndex) {
    if (!timeplotCommits || timeplotCommits.length === 0) {
      return;
    }
    let newRightCommit = null;
    if (rightCommit) {
      const foundIndex = timeplotCommits.findIndex(commit => {
        return commit.id === rightCommit.id;
      });
      if (foundIndex !== -1) {
        newRightCommit = timeplotCommits[foundIndex - relativeIndex];
      }
    } else if (relativeIndex < 0) {
      newRightCommit = timeplotCommits[0];
    }
    if (newRightCommit) {
      dispatch(
        setDates(startDate * 1000, (newRightCommit.authorDate + 1) * 1000)
      );
    }
  }
};