<commit_before>﻿import LinkUtility from './LinkUtility';
import withStateNavigator from './withStateNavigator';
import { StateNavigator } from 'navigation';
import { RefreshLinkProps } from './Props';
import * as React from 'react';
type RefreshLinkState = { link: string, active: boolean };

class RefreshLink extends React.Component<RefreshLinkProps, RefreshLinkState> {
    private crumb: number;
    context: { stateNavigator: StateNavigator };
    private onNavigate = () => {
        var { link, active } = this.state;
        var componentState = this.getComponentState();
        if (link !== componentState.link || active !== componentState.active)
            this.setState(componentState);
    }

    constructor(props, context) {
        super(props, context);
        this.state = this.getComponentState(props);
        this.crumb = this.getStateNavigator().stateContext.crumbs.length;
    }

    static contextTypes = {
        stateNavigator: () => null
    }
    
    private getStateNavigator(): StateNavigator {
        return this.props.stateNavigator || this.context.stateNavigator;
    }
    
    componentDidMount() {
        if (!this.props.navigationContext)
            this.getStateNavigator().onNavigate(this.onNavigate);
    }

    componentWillReceiveProps(nextProps) {
        this.setState(this.getComponentState(nextProps))
    }

    componentWillUnmount() {
        if (!this.props.navigationContext)
            this.getStateNavigator().offNavigate(this.onNavigate);
    }
    
    private getComponentState(props = this.props): RefreshLinkState {
        var { crumbs } = this.getStateNavigator().stateContext;
        var { acrossCrumbs, navigationData } = props;
        if (!acrossCrumbs && this.crumb !== undefined && this.crumb !== crumbs.length)
            return this.state;
        var link = this.getRefreshLink(props);
        var active = LinkUtility.isActive(this.getStateNavigator(), navigationData);
        return { link, active };
    }

    private getRefreshLink(props): string {
        var { navigationData, includeCurrentData, currentDataKeys } = props;
        var navigationData = LinkUtility.getData(this.getStateNavigator(), navigationData, includeCurrentData, currentDataKeys);
        try {
            return this.getStateNavigator().getRefreshLink(navigationData);
        } catch (e) {
            return null;
        }
    }

    render() {
        var props: any = {};
        for(var key in this.props) {
            if (LinkUtility.isValidAttribute(key))
                props[key] = this.props[key];
        }
        props.href = this.state.link && this.getStateNavigator().historyManager.getHref(this.state.link);
        props.onClick = LinkUtility.getOnClick(this.getStateNavigator(), this.props, this.state.link);
        LinkUtility.setActive(this.state.active, this.props, props);
        return <a {...props}>{this.props.children}</a>;
    }
};
export default withStateNavigator(RefreshLink);<commit_msg>Derived state from props and removed context<commit_after>﻿import LinkUtility from './LinkUtility';
import withStateNavigator from './withStateNavigator';
import { RefreshLinkProps } from './Props';
import * as React from 'react';
type RefreshLinkState = { link?: string, active?: boolean, crumb?: number };

class RefreshLink extends React.Component<RefreshLinkProps, RefreshLinkState> {
    constructor(props) {
        super(props);
        this.state = { crumb: props.stateNavigator.stateContext.crumbs.length };
    }

    static getDerivedStateFromProps(props, { crumb }): RefreshLinkState {
        var { acrossCrumbs, navigationData, includeCurrentData, currentDataKeys, stateNavigator } = props;
        var { crumbs, state } = stateNavigator.stateContext;
        if (!acrossCrumbs && crumb !== crumbs.length)
            return null;
        navigationData = LinkUtility.getData(stateNavigator, navigationData, includeCurrentData, currentDataKeys);
        try {
            var link = stateNavigator.getRefreshLink(navigationData);
        } catch {}
        return { link, active: LinkUtility.isActive(stateNavigator, navigationData) };
    }
    
    render() {
        var props: any = {};
        for(var key in this.props) {
            if (LinkUtility.isValidAttribute(key))
                props[key] = this.props[key];
        }
        props.href = this.state.link && this.props.stateNavigator.historyManager.getHref(this.state.link);
        props.onClick = LinkUtility.getOnClick(this.props.stateNavigator, this.props, this.state.link);
        LinkUtility.setActive(this.state.active, this.props, props);
        return <a {...props}>{this.props.children}</a>;
    }
};
export default withStateNavigator(RefreshLink);
{'fraction_non_alphanumeric': 0.11595429538721963, 'fraction_numerical': 0.0, 'mean_word_length': 3.078515962036238, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '954887', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1306, 'n_words': 263}