UNPKG

@jupyterlab/git

Version:

A JupyterLab extension for version control using git

121 lines (120 loc) 6.08 kB
import { caretDownIcon, caretUpIcon } from '@jupyterlab/ui-components'; import * as React from 'react'; import { classes } from 'typestyle'; import { compareWithSelectedIcon, diffIcon, selectForCompareIcon } from '../style/icons'; import { branchClass, branchWrapperClass, commitBodyClass, referenceCommitNodeClass, challengerCommitNodeClass, commitExpandedClass, commitHeaderClass, commitHeaderItemClass, commitWrapperClass, iconButtonClass, localBranchClass, remoteBranchClass, singleFileCommitClass, workingBranchClass } from '../style/PastCommitNode'; import { Git } from '../tokens'; import { ActionButton } from './ActionButton'; /** * React component for rendering an individual commit. */ export class PastCommitNode extends React.Component { /** * Returns a React component for rendering an individual commit. * * @param props - component properties * @returns React component */ constructor(props) { super(props); /** * Callback invoked upon clicking on an individual commit. * * @param event - event object */ this._onCommitClick = (event, sha) => { var _a; if (this.props.children) { this.props.toggleCommitExpansion(sha); } else { (_a = this.props.onOpenDiff) === null || _a === void 0 ? void 0 : _a.call(this, event); } }; } /** * Renders the component. * * @returns React element */ render() { return (React.createElement("li", { id: this.props.commit.commit, ref: el => this.props.setRef(el), className: classes(commitWrapperClass, !this.props.children && !!this.props.onOpenDiff ? singleFileCommitClass : this.props.expanded ? commitExpandedClass : null, this.props.isReferenceCommit && referenceCommitNodeClass, this.props.isChallengerCommit && challengerCommitNodeClass), title: this.props.children ? this.props.trans.__('View commit details') : this.props.trans.__('View file changes'), onClick: event => this._onCommitClick(event, this.props.commit.commit), onContextMenu: this.props.contextMenu && (event => { this.props.contextMenu(this.props.commit, event); }) }, React.createElement("div", { className: commitHeaderClass }, React.createElement("span", { className: commitHeaderItemClass }, this.props.commit.author), React.createElement("span", { className: commitHeaderItemClass }, +this.props.commit.commit in Git.Diff.SpecialRef ? Git.Diff.SpecialRef[+this.props.commit.commit] : this.props.commit.commit.slice(0, 7)), React.createElement("span", { className: commitHeaderItemClass }, this.props.commit.date), !this.props.commit.is_binary && (React.createElement(React.Fragment, null, React.createElement(ActionButton, { className: iconButtonClass, disabled: this.props.onSelectForCompare === null, icon: selectForCompareIcon, title: this.props.trans.__('Select for compare'), onClick: this.props.onSelectForCompare }), React.createElement(ActionButton, { className: iconButtonClass, disabled: this.props.onCompareWithSelected === null, icon: compareWithSelectedIcon, title: this.props.trans.__('Compare with selected'), onClick: this.props.onCompareWithSelected }))), this.props.children ? (this.props.expanded ? (React.createElement(caretUpIcon.react, { className: iconButtonClass, tag: "span" })) : (React.createElement(caretDownIcon.react, { className: iconButtonClass, tag: "span" }))) : (React.createElement(diffIcon.react, { className: iconButtonClass, tag: "span" }))), React.createElement("div", { className: branchWrapperClass }, this._renderBranches()), React.createElement("div", { className: branchWrapperClass }, this._renderTags()), React.createElement("div", { className: commitBodyClass }, this.props.commit.commit_msg, this.props.expanded && this.props.children))); } /** * Renders branch information. * * @returns array of React elements */ _renderBranches() { const curr = this.props.commit.commit; const branches = []; for (let i = 0; i < this.props.branches.length; i++) { const branch = this.props.branches[i]; if (branch.top_commit && branch.top_commit === curr) { branches.push(branch); } } return branches.map(this._renderBranch, this); } /** * Renders individual branch data. * * @param branch - branch data * @returns React element */ _renderBranch(branch) { return (React.createElement(React.Fragment, { key: branch.name }, branch.is_current_branch && (React.createElement("span", { className: classes(branchClass, workingBranchClass) }, "working")), React.createElement("span", { className: classes(branchClass, branch.is_remote_branch ? remoteBranchClass : localBranchClass) }, branch.name))); } /** * Renders tags information. * * @returns array of React elements */ _renderTags() { const curr = this.props.commit.commit; const tags = []; for (let i = 0; i < this.props.tagsList.length; i++) { const tag = this.props.tagsList[i]; if (tag.baseCommitId && tag.baseCommitId === curr) { tags.push(tag); } } return tags.map(this._renderTag, this); } /** * Renders individual tag data. * * @param tag - tag data * @returns React element */ _renderTag(tag) { return (React.createElement(React.Fragment, { key: tag.name }, React.createElement("span", { className: classes(branchClass, localBranchClass) }, tag.name))); } }