import LinkIcon from 'mdi-react/LinkIcon';
import * as React from 'react';
import { fromEvent, Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';
import { replaceRevisionInURL } from '..';
import { ActionItem } from '../../components/ActionItem';
/**
 * A repository header action that replaces the revision in the URL with the canonical 40-character
 * Git commit SHA.
 */
export class GoToPermalinkAction extends React.PureComponent {
    constructor() {
        super(...arguments);
        this.subscriptions = new Subscription();
    }
    componentDidMount() {
        // Trigger the user presses 'y'.
        this.subscriptions.add(fromEvent(window, 'keydown')
            .pipe(filter(event => 
        // 'y' shortcut (if no input element is focused)
        event.key === 'y' && !['INPUT', 'TEXTAREA'].includes(document.activeElement.nodeName)))
            .subscribe(event => {
            event.preventDefault();
            // Replace the revision in the current URL with the new one and push to history.
            this.props.history.push(this.permalinkURL);
        }));
    }
    componentWillUnmount() {
        this.subscriptions.unsubscribe();
    }
    render() {
        if (this.props.rev === this.props.commitID) {
            return null; // already at the permalink destination
        }
        return (React.createElement(ActionItem, { to: this.permalinkURL, "data-tooltip": "Permalink (with full Git commit SHA)" },
            React.createElement(LinkIcon, { className: "icon-inline" })));
    }
    get permalinkURL() {
        return replaceRevisionInURL(this.props.location.pathname + this.props.location.search + this.props.location.hash, this.props.commitID);
    }
}
//# sourceMappingURL=GoToPermalinkAction.js.map