<commit_before>/* eslint-disable jsx-a11y/click-events-have-key-events,class-methods-use-this */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

import RadioButton from '../../react-chayns-radiobutton/component/RadioButton';
import Icon from '../../react-chayns-icon/component/Icon';
import TappPortal from '../../react-chayns-tapp_portal/component/TappPortal';

let globalState = {
    modes: [],
    activeModeId: 0
};
const onChangeListener = [];

export default class ModeSwitch extends Component {
    static propTypes = {
        modes: PropTypes.arrayOf(
            PropTypes.shape({
                id: PropTypes.number,
                name: PropTypes.string,
                uacIds: PropTypes.arrayOf(PropTypes.number),
            })
        ),
        save: PropTypes.bool,
        onChange: PropTypes.func,
        defaultMode: PropTypes.number,
        show: PropTypes.bool,
    };

    static defaultProps = {
        modes: null,
        save: false,
        onChange: null,
        defaultMode: null,
        show: false,
    };

    static getCurrentMode() {
        const { modes, activeModeId } = globalState;
        return modes.find(mode => mode.id === activeModeId || null);
    }

    static addChangeListener(callback) {
        onChangeListener.push(callback);
        if (globalState.modes.length > 0) {
            const mode = globalState.modes.find(m => m.id === globalState.activeModeId);
            callback(mode);
        }
    }

    static removeChangeListener(callback) {
        onChangeListener.splice(onChangeListener.indexOf(callback), 1);
    }

    constructor(props) {
        super(props);

        this.toggleModeSwitch = this.toggleModeSwitch.bind(this);
        this.switchMode = this.switchMode.bind(this);
        this.setMode = this.setMode.bind(this);
        this.onChange = this.onChange.bind(this);
        this.init = this.init.bind(this);
        this.state = {
            modes: [],
            activeModeId: null,
            open: false
        };

        window.chayns.ready.then(() => {
            window.chayns.addAccessTokenChangeListener(this.init);
            this.init();
        });
    }

    componentWillReceiveProps(nextProps) {
        const { modes } = this.props;
        if (chayns.env.user.isAuthenticated && nextProps.modes !== modes) {
            globalState.modes = this.setModes(nextProps.modes);
            this.setState({ modes: globalState.modes });
        }
    }

    onChange(id) {
        const { modes } = this.state;
        const { onChange } = this.props;
        const mode = modes.find(m => m.id === id);
        if (onChange) {
            onChange(mode);
        }
        onChangeListener.forEach((listener) => {
            listener(mode);
        });
    }

    setMode(id) {
        const { save } = this.props;
        this.setState({ activeModeId: id });
        globalState.activeModeId = id;
        if (save) {
            window.chayns.storage.set('react__modeSwitch--currentMode', id);
        }
    }

    setModes(modes) {
        let newModes = modes || [];
        const user = newModes.filter(mode => mode.id === 0);
        const admin = newModes.filter(mode => mode.id === 1);
        if (admin.length === 0) {
            newModes.unshift({
                id: 1,
                name: 'chayns® Manager',
                uacIds: [1]
            });
        }
        if (user.length === 0) {
            newModes.unshift({
                id: 0,
                name: window.chayns.env.user.name
            });
        }
        newModes = newModes.filter(mode => !mode.uacIds || this.isUserInGroup(mode.uacIds));
        return newModes;
    }

    async init() {
        const { defaultMode, save, modes } = this.props;
        if (chayns.env.user.isAuthenticated) {
            globalState = {
                modes: this.setModes(modes),
                activeModeId: defaultMode || 0,
                open: false
            };

            chayns.removeAdminSwitchListener(this.switchMode);
            chayns.addAdminSwitchListener(this.switchMode);
            if (defaultMode) {
                globalState.activeModeId = defaultMode;
            }
            if (save) {
                const storage = await window.chayns.storage.get('react__modeSwitch--currentMode');
                if (chayns.utils.isNumber(storage.object)) {
                    globalState.activeModeId = storage.object;
                }
            }
            if (globalState.activeModeId === 0 || globalState.activeModeId === 1) {
                globalState.activeModeId = chayns.env.user.adminMode ? 1 : 0;
            }
        } else {
            globalState = {
                modes: [],
                activeModeId: null,
                open: false
            };
        }

        this.setState(globalState);
        this.onChange(globalState.activeModeId);
    }<commit_msg>Use static variables for current state<commit_after>/* eslint-disable jsx-a11y/click-events-have-key-events,class-methods-use-this */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

import RadioButton from '../../react-chayns-radiobutton/component/RadioButton';
import Icon from '../../react-chayns-icon/component/Icon';
import TappPortal from '../../react-chayns-tapp_portal/component/TappPortal';

export default class ModeSwitch extends Component {
    static propTypes = {
        modes: PropTypes.arrayOf(
            PropTypes.shape({
                id: PropTypes.number,
                name: PropTypes.string,
                uacIds: PropTypes.arrayOf(PropTypes.number),
            })
        ),
        save: PropTypes.bool,
        onChange: PropTypes.func,
        defaultMode: PropTypes.number,
        show: PropTypes.bool,
    };

    static defaultProps = {
        modes: null,
        save: false,
        onChange: null,
        defaultMode: null,
        show: false,
    };

    static modes = [];

    static activeModeId = 0;

    static open = false;

    static onChangeListener = [];

    static getCurrentMode() {
        const { modes, activeModeId } = ModeSwitch;
        return modes.find(mode => mode.id === activeModeId || null);
    }

    static addChangeListener(callback) {
        ModeSwitch.onChangeListener.push(callback);
        if (ModeSwitch.modes.length > 0) {
            const mode = ModeSwitch.modes.find(m => m.id === ModeSwitch.activeModeId);
            callback(mode);
        }
    }

    static removeChangeListener(callback) {
        ModeSwitch.onChangeListener.splice(ModeSwitch.onChangeListener.indexOf(callback), 1);
    }

    constructor(props) {
        super(props);

        this.toggleModeSwitch = this.toggleModeSwitch.bind(this);
        this.switchMode = this.switchMode.bind(this);
        this.setMode = this.setMode.bind(this);
        this.onChange = this.onChange.bind(this);
        this.init = this.init.bind(this);
        this.state = {
            modes: [],
            activeModeId: null,
            open: false
        };

        window.chayns.ready.then(() => {
            window.chayns.addAccessTokenChangeListener(this.init);
            this.init();
        });
    }

    componentWillReceiveProps(nextProps) {
        const { modes } = this.props;
        if (chayns.env.user.isAuthenticated && nextProps.modes !== modes) {
            ModeSwitch.modes = this.setModes(nextProps.modes);
            this.setState({ modes: ModeSwitch.modes });
        }
    }

    onChange(id) {
        const { modes } = this.state;
        const { onChange } = this.props;
        const mode = modes.find(m => m.id === id);
        if (onChange) {
            onChange(mode);
        }
        ModeSwitch.onChangeListener.forEach((listener) => {
            listener(mode);
        });
    }

    setMode(id) {
        const { save } = this.props;
        this.setState({ activeModeId: id });
        ModeSwitch.activeModeId = id;
        if (save) {
            window.chayns.storage.set('react__modeSwitch--currentMode', id);
        }
    }

    setModes(modes) {
        let newModes = modes || [];
        const user = newModes.filter(mode => mode.id === 0);
        const admin = newModes.filter(mode => mode.id === 1);
        if (admin.length === 0) {
            newModes.unshift({
                id: 1,
                name: 'chayns® Manager',
                uacIds: [1]
            });
        }
        if (user.length === 0) {
            newModes.unshift({
                id: 0,
                name: window.chayns.env.user.name
            });
        }
        newModes = newModes.filter(mode => !mode.uacIds || this.isUserInGroup(mode.uacIds));
        return newModes;
    }

    async init() {
        const { defaultMode, save, modes } = this.props;
        if (chayns.env.user.isAuthenticated) {
            ModeSwitch.modes = this.setModes(modes);
            ModeSwitch.activeModeId = defaultMode || 0;
            ModeSwitch.open = false;

            chayns.removeAdminSwitchListener(this.switchMode);
            chayns.addAdminSwitchListener(this.switchMode);
            if (defaultMode) {
                ModeSwitch.activeModeId = defaultMode;
            }
            if (save) {
                const storage = await window.chayns.storage.get('react__modeSwitch--currentMode');
                if (chayns.utils.isNumber(storage.object)) {
                    ModeSwitch.activeModeId = storage.object;
                }
            }
            if (ModeSwitch.activeModeId === 0 || ModeSwitch.activeModeId === 1) {
                ModeSwitch.activeModeId = chayns.env.user.adminMode ? 1 : 0;
            }
        } else {
            ModeSwitch.modes = [];
            ModeSwitch.activeModeId = null;
            ModeSwitch.open = false;
        }

        this.setState({
            modes: ModeSwitch.modes,
            activeModeId: ModeSwitch.activeModeId,
            open: ModeSwitch.open,
        });
        this.onChange(ModeSwitch.activeModeId);
    }
{'fraction_non_alphanumeric': 0.12126607319485658, 'fraction_numerical': 0.0033630069238377845, 'mean_word_length': 1.7977310459324847, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '8638', 'n_tokens_mistral': 2923, 'n_tokens_neox': 2853, 'n_words': 518}