import shallowEqual from 'fbjs/lib/shallowEqual';
import * as React from 'react';
/**
 * Contributes an item to the RepoHeader. The contribution is added and remains present for this component's entire
 * lifecycle.
 *
 * A React component that needs to contribute an item to the RepoHeader should include a
 * <RepoHeaderContributionPortal> element that specifies the item.
 *
 * It is called a "portal" because it is similar to the React concept of a "portal": it effectively renders an
 * element in a different DOM hierarchy (i.e., in RepoHeader).
 */
export class RepoHeaderContributionPortal extends React.Component {
    componentDidMount() {
        this.addOrUpdateContribution();
    }
    componentDidUpdate() {
        this.addOrUpdateContribution();
    }
    shouldComponentUpdate(nextProps) {
        // This "smart" comparison lets us skip ~75% of the updates that extending React.PureComponent (and not
        // implementing shouldComponentUpdate) or always returning true here would yield.
        //
        // We use fbjs/lib/shallowEqual because it's what React uses, so it avoids bringing in another dep and
        // avoids implementation disparities (and is faster than a deep comparison).
        return (this.props.repoHeaderContributionsLifecycleProps !== nextProps.repoHeaderContributionsLifecycleProps ||
            this.props.position !== nextProps.position ||
            this.props.priority !== nextProps.priority ||
            !shallowEqual(this.props.element.props, nextProps.element.props));
    }
    componentWillUnmount() {
        const key = this.props.element.key; // enforced in RepoHeaderContributionStore
        if (this.props.repoHeaderContributionsLifecycleProps) {
            // Don't need to worry about being unable to remove this because once
            // this.props.repoHeaderContributionsLifecycleProps is set (from RepoHeader), it is never unset.
            this.props.repoHeaderContributionsLifecycleProps.onRepoHeaderContributionRemove(key);
        }
    }
    render() {
        return null;
    }
    addOrUpdateContribution() {
        if (this.props.repoHeaderContributionsLifecycleProps) {
            this.props.repoHeaderContributionsLifecycleProps.onRepoHeaderContributionAdd(this.props);
        }
    }
}
//# sourceMappingURL=RepoHeaderContributionPortal.js.map