UNPKG

@jupyterlab/git

Version:

A JupyterLab extension for version control using git

107 lines (106 loc) 4.47 kB
import * as React from 'react'; import { discardIcon, rewindIcon } from '../style/icons'; import { actionButtonClass, commitBodyClass } from '../style/SinglePastCommitInfo'; import { ActionButton } from './ActionButton'; import { CommitDiff } from './CommitDiff'; import { ResetRevertDialog } from './ResetRevertDialog'; /** * React component for rendering information about an individual commit. */ export class SinglePastCommitInfo extends React.Component { /** * Returns a React component for information about an individual commit. * * @param props - component properties * @returns React component */ constructor(props) { super(props); /** * Callback invoked upon a clicking a button to revert changes. * * @param event - event object */ this._onRevertClick = (event) => { event.stopPropagation(); this.setState({ resetRevertDialog: true, resetRevertAction: 'revert' }); }; /** * Callback invoked upon a clicking a button to reset changes. * * @param event - event object */ this._onResetClick = (event) => { event.stopPropagation(); this.setState({ resetRevertDialog: true, resetRevertAction: 'reset' }); }; /** * Callback invoked upon closing a dialog to reset or revert changes. */ this._onResetRevertDialogClose = () => { this.setState({ resetRevertDialog: false }); }; this.state = { info: '', commitBody: '', numFiles: '', insertions: '', deletions: '', modifiedFiles: [], loadingState: 'loading', resetRevertDialog: false, resetRevertAction: 'reset' }; } /** * Callback invoked immediately after mounting a component (i.e., inserting into a tree). */ async componentDidMount() { try { const log = await this.props.model.detailedLog(this.props.commit.commit); if (log) { this.setState({ info: log.modified_file_note, commitBody: log.commit_body, numFiles: log.modified_files_count, insertions: log.number_of_insertions, deletions: log.number_of_deletions, modifiedFiles: log.modified_files, loadingState: 'success' }); } } catch (err) { console.error(`Error while getting detailed log for commit ${this.props.commit.commit} and path ${this.props.model.pathRepository}`, err); this.setState({ loadingState: 'error' }); return; } } /** * Renders the component. * * @returns React element */ render() { if (this.state.loadingState === 'loading') { return React.createElement("div", null, "\u2026"); } if (this.state.loadingState === 'error') { return React.createElement("div", null, this.props.trans.__('Error loading commit data')); } return (React.createElement("div", null, React.createElement("p", { className: commitBodyClass }, this.state.commitBody), React.createElement(CommitDiff, { actions: React.createElement(React.Fragment, null, React.createElement(ActionButton, { className: actionButtonClass, icon: discardIcon, title: this.props.trans.__('Revert changes introduced by this commit'), onClick: this._onRevertClick }), React.createElement(ActionButton, { className: actionButtonClass, icon: rewindIcon, title: this.props.trans.__('Discard changes introduced *after* this commit (hard reset)'), onClick: this._onResetClick }), React.createElement(ResetRevertDialog, { open: this.state.resetRevertDialog, action: this.state.resetRevertAction, model: this.props.model, commit: this.props.commit, onClose: this._onResetRevertDialogClose, trans: this.props.trans })), numFiles: this.state.numFiles, insertions: this.state.insertions, deletions: this.state.deletions, files: this.state.modifiedFiles, onOpenDiff: this.props.onOpenDiff, trans: this.props.trans }))); } }