<commit_before>// @flow

import { Container } from 'unstated';

export type Item = {
    uuid: string | number,
    expanded: boolean,
    disabled: boolean,
};
export type StoreState = {
    items: Array<Item>,
    accordion: boolean,
    onChange: Function,
};

class AccordionContainer extends Container<StoreState> {
    constructor(args: $Shape<StoreState> = {}) {
        super();
        this.state = {
            items: [],
            accordion: true,
            onChange: () => {},
            ...args,
        };
    }

    setAccordion(accordion: boolean) {
        if (accordion !== this.state.accordion) {
            return this.setState({ accordion });
        }
        return null;
    }

    setOnChange(onChange: Function) {
        if (onChange !== this.state.onChange) {
            return this.setState({ onChange });
        }
        return null;
    }

    addItem(newItem: Item) {
        let items;
        if (this.state.accordion && newItem.expanded) {
            // If this is a true accordion and the new item is expanded, then the others must be closed.
            items = [
                ...this.state.items.map(item => ({ ...item, expanded: false })),
                newItem,
            ];
        } else {
            items = [...this.state.items, newItem];
        }
        return this.setState({
            items,
        });
    }

    removeItem(key: string | number) {
        return this.setState({
            items: this.state.items.filter(item => item.uuid !== key),<commit_msg>Fix race-conditions issue in AccordionContainer<commit_after>// @flow

import { Container } from 'unstated';

export type Item = {
    uuid: string | number,
    expanded: boolean,
    disabled: boolean,
};
export type StoreState = {
    items: Array<Item>,
    accordion: boolean,
    onChange: Function,
};

class AccordionContainer extends Container<StoreState> {
    constructor(args: $Shape<StoreState> = {}) {
        super();
        this.state = {
            items: [],
            accordion: true,
            onChange: () => {},
            ...args,
        };
    }

    setAccordion(accordion: boolean) {
        if (accordion !== this.state.accordion) {
            return this.setState({ accordion });
        }
        return null;
    }

    setOnChange(onChange: Function) {
        if (onChange !== this.state.onChange) {
            return this.setState({ onChange });
        }
        return null;
    }

    addItem(newItem: Item) {
        // Need to use callback style otherwise race-conditions are created by concurrent registrations.
        return this.setState(state => {
            let items;
            if (state.accordion && newItem.expanded) {
                // If this is a true accordion and the new item is expanded, then the others must be closed.
                items = [
                    ...state.items.map(item => ({
                        ...item,
                        expanded: false,
                    })),
                    newItem,
                ];
            } else {
                items = [...state.items, newItem];
            }
            return {
                items,
            };
        });
    }

    removeItem(key: string | number) {
        return this.setState({
            items: this.state.items.filter(item => item.uuid !== key),
{'fraction_non_alphanumeric': 0.12186379928315412, 'fraction_numerical': 0.0, 'mean_word_length': 1.574173712528824, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 16, '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': '2130547', 'n_tokens_mistral': 905, 'n_tokens_neox': 896, 'n_words': 226}