All files / app/containers Header.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 185 186 187 188 189 190 191                                            2x                                                                                         2x 2x 2x 2x 2x 2x 2x   2x   2x         2x   2x                                                                                                   6x 6x 6x 4x 4x   6x   6x                           2x 2x     2x 2x 2x   2x     2x     2x 2x 2x   6x 4x   6x                          
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
 
import { debug } from 'app/utilities/logger';
import {
  getAreCommitsFiltered,
  getFilteredCommits,
} from 'app/selectors/commits';
import { getSelectedPath, getGitRoot } from 'app/selectors/stateVars';
import {
  getDefaultedStartDate,
  getDefaultedEndDate,
} from 'app/selectors/dates';
 
import { style } from 'app/styles';
import { selectPath, setSearch } from 'app/actions';
import { setDates } from 'app/actions/setDates';
import { ExplodingDateRange } from 'app/components/ExplodingDateRange';
import { ResetLink } from 'app/components/ResetLink';
import { ExplodeOnChange } from 'app/components/ExplodeOnChange';
import { EpochDateTime } from 'app/components/EpochDateTime';
 
const styles = {
  outer: {
    _extends: ['flexColumn'],
    flexShrink: 0,
  },
  appName: {
    _extends: ['inlineBlock', 'h1Text'],
    marginBottom: 10,
  },
  date: {
    _extends: 'largerText',
  },
  dateSelected: {
    _extends: 'bigText',
    color: '@colors.selected',
  },
  dateRange: {
    _extends: ['flexGrow', 'flexColumn'],
    alignItems: 'flex-end',
    paddingTop: '@margins.small+px',
    paddingRight: '@margins.large+px',
  },
  topRow: {
    _extends: 'flexRow',
  },
  path: {
    _extends: ['inlineBlock', 'flexColumn'],
    marginBottom: 10,
    flexGrow: 1,
    minHeight: 30,
  },
  resetLink: {
    position: 'relative',
    top: '-5px',
  },
  pathPart: {
    extends: 'smallerText',
    margin: '0px 2px',
  },
  pathSeparator: {
    color: '@colors.linkText',
    wordBreak: 'break-all ',
  },
};
 
export const Header: React.FC = (): React.ReactElement => {
  const selectedPath = useSelector(getSelectedPath);
  const startDate = useSelector(getDefaultedStartDate);
  const endDate = useSelector(getDefaultedEndDate);
  const areCommitsFiltered = useSelector(getAreCommitsFiltered);
  const filteredCommits = useSelector(getFilteredCommits);
  const gitRoot = useSelector(getGitRoot);
 
  const dispatch = useDispatch();
 
  const dateStyle = style([
    styles.date,
    areCommitsFiltered && styles.dateSelected,
  ]);
  const singleCommit =
    areCommitsFiltered && filteredCommits.length === 1 && filteredCommits[0];
 
  return (
    <div style={style(styles.outer)}>
      <div style={style(styles.topRow)}>
        <div style={style(styles.appName)}>Git Temporal </div>
        <div style={style(styles.dateRange)}>
          {filteredCommits.length === 0 ? (
            <div />
          ) : singleCommit ? (
            <ExplodeOnChange
              value={singleCommit.authorDate}
              style={dateStyle}
              initialExplosion
            >
              Single commit (#
              {singleCommit.hash}) on{' '}
              <EpochDateTime
                value={singleCommit.authorDate}
                style={dateStyle}
              />
            </ExplodeOnChange>
          ) : (
            <ExplodingDateRange
              startDate={startDate}
              endDate={endDate}
              style={dateStyle}
            />
          )}
        </div>
      </div>
      <div style={style('flexRow')}>
        <div style={style(styles.path)}>
          <div>
            <div style={style('h4Text', { marginBottom: 10 })}>
              {renderPathLinks()}
            </div>
          </div>
        </div>
        {areCommitsFiltered && (
          <ResetLink
            style={style(styles.resetLink)}
            onClick={onResetDatesClick}
          >
            Reset Date Range
          </ResetLink>
        )}
      </div>
    </div>
  );
 
  function renderLinkPart(part, index, fullPath, lastIndex) {
    const partStyles: any = [styles.pathPart];
    let onClick = undefined;
    if (index !== lastIndex) {
      partStyles.push('link');
      onClick = () => onLinkPartClick(fullPath);
    }
    const sep = index > 1 ? '/' : index > 0 ? ' : ' : '';
 
    return (
      <span key={index}>
        <span style={style(styles.pathSeparator)}>{sep}</span>
        <span
          style={style(partStyles)}
          onClick={onClick}
          data-testid="pathPath"
        >
          {part}
        </span>
      </span>
    );
  }
  function renderPathLinks() {
    debug('renderPathLinks', { selectedPath, gitRoot });
    Iif (!gitRoot || gitRoot.length === 0) {
      return null;
    }
    let parts = ['(repository root)'];
    debug('renderPathLinks', { selectedPath, gitRoot });
    Eif (selectedPath && selectedPath.trim().length > 0) {
      // in vscode it sends us the full path so trim off repo root
      const relativePath = selectedPath.startsWith(gitRoot)
        ? selectedPath.slice(gitRoot.length + 1)
        : selectedPath;
      parts = parts.concat(relativePath.trim().split(/[/\\]/));
    }
 
    const lastIndex = parts.length - 1;
    let fullPathSoFar = gitRoot || '';
    return parts.map((part, index) => {
      // > 0 means don't add 'repository:'
      if (index > 0) {
        fullPathSoFar += fullPathSoFar.length > 0 ? `/${part}` : part;
      }
      return renderLinkPart(part, index, fullPathSoFar, lastIndex);
    });
  }
 
  function onLinkPartClick(fullPath) {
    dispatch(selectPath(fullPath));
  }
 
  function onResetDatesClick() {
    dispatch(setDates(null, null));
    dispatch(setSearch(null));
  }
};