UNPKG

@git-temporal/git-temporal-react

Version:

<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

68 lines (67 loc) 2.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ActionTypes_1 = require("app/actions/ActionTypes"); const vscode_1 = require("app/actions/vscode"); const logger_1 = require("app/utilities/logger"); const requestDiff = (path, leftCommit, rightCommit) => ({ leftCommit, rightCommit, selectedPath: path, type: ActionTypes_1.ActionTypes.REQUEST_DIFF, }); const requestDeferredDiff = (path) => ({ selectedPath: path, type: ActionTypes_1.ActionTypes.REQUEST_DEFERRED_DIFF, }); const _receiveDiff = (path, diff) => ({ diff, selectedPath: path, type: ActionTypes_1.ActionTypes.RECEIVE_DIFF, }); exports.receiveDiff = (path, diff) => (dispatch, getState) => { const { diffLeftCommit, diffRightCommit, isFetching, commits } = getState(); const areDefaults = !diffLeftCommit && !diffRightCommit; const isUnchangedFile = !diff.isDirectory && ((!diff.leftFileContents && !diff.rightFileContents) || diff.leftFileContents === diff.rightFileContents); const isUnchangedDirectory = diff.isDirectory && (!diff.modifiedFiles || diff.modifiedFiles.length === 0); const areCommits = commits && commits.length > 1; if (areDefaults && (isUnchangedFile || isUnchangedDirectory)) { if (commits && commits.length > 1) { dispatch(exports.fetchDiff(path, commits[1], commits[0])); } else if (isFetching) { dispatch(requestDeferredDiff(path)); } else { dispatch(_receiveDiff(path, diff)); } } else { dispatch(_receiveDiff(path, diff)); } }; exports.fetchDiff = (path, leftCommit, rightCommit) => (dispatch, getState) => { dispatch(requestDiff(path, leftCommit, rightCommit)); if (vscode_1.isVscode) { logger_1.debug('sending diff request to vscode ', path, leftCommit, rightCommit); vscode_1.vscode.postMessage({ path, leftCommit: (leftCommit && leftCommit.id) || null, rightCommit: (rightCommit && rightCommit.id) || null, command: 'diff', }); } else { const pathParam = path && path.trim().length > 0 ? `?path=${path}` : '?path=.'; const leftCommitParam = leftCommit ? `&leftCommit=${leftCommit.id}` : ''; const rightCommitParam = rightCommit ? `&rightCommit=${rightCommit.id}` : ''; const url = `http://localhost:11966/git-temporal/diff${pathParam}${leftCommitParam}${rightCommitParam}`; fetch(url) .then(response => response.json()) .then(diff => dispatch(exports.receiveDiff(path, diff))); } };