<commit_before>/*
Copyright 2020 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import React from 'react';
import PropTypes from 'prop-types';
import { _t, _td } from '../../../languageHandler';
import { MatrixClientPeg } from '../../../MatrixClientPeg';
import * as sdk from '../../../index';
import {
    SetupEncryptionStore,
    PHASE_INTRO,
    PHASE_BUSY,
    PHASE_DONE,
    PHASE_CONFIRM_SKIP,
    PHASE_FINISHED,
} from '../../../stores/SetupEncryptionStore';

function keyHasPassphrase(keyInfo) {
    return (
        keyInfo.passphrase &&
        keyInfo.passphrase.salt &&
        keyInfo.passphrase.iterations
    );
}

export default class SetupEncryptionBody extends React.Component {
    static propTypes = {
        onFinished: PropTypes.func.isRequired,
    };

    constructor() {
        super();
        const store = SetupEncryptionStore.sharedInstance();
        store.on("update", this._onStoreUpdate);
        store.start();
        this.state = {
            phase: store.phase,
            // this serves dual purpose as the object for the request logic and
            // the presence of it indicating that we're in 'verify mode'.
            // Because of the latter, it lives in the state.
            verificationRequest: store.verificationRequest,
            backupInfo: store.backupInfo,
        };
    }

    _onStoreUpdate = () => {
        const store = SetupEncryptionStore.sharedInstance();
        if (store.phase === PHASE_FINISHED) {
            this.props.onFinished();
            return;
        }
        this.setState({
            phase: store.phase,
            verificationRequest: store.verificationRequest,
            backupInfo: store.backupInfo,
        });
    };

    componentWillUnmount() {
        const store = SetupEncryptionStore.sharedInstance();
        store.off("update", this._onStoreUpdate);
        store.stop();
    }

    _onUsePassphraseClick = async () => {
        const store = SetupEncryptionStore.sharedInstance();
        store.usePassPhrase();
    }

    onSkipClick = () => {
        const store = SetupEncryptionStore.sharedInstance();
        store.skip();
    }

    onSkipConfirmClick = () => {
        const store = SetupEncryptionStore.sharedInstance();
        store.skipConfirm();
    }

    onSkipBackClick = () => {
        const store = SetupEncryptionStore.sharedInstance();
        store.returnAfterSkip();
    }

    onDoneClick = () => {
        const store = SetupEncryptionStore.sharedInstance();
        store.done();
    }

    render() {
        const AccessibleButton = sdk.getComponent("elements.AccessibleButton");

        const {
            phase,
        } = this.state;

        if (this.state.verificationRequest) {
            const EncryptionPanel = sdk.getComponent("views.right_panel.EncryptionPanel");
            return <EncryptionPanel
                layout="dialog"
                verificationRequest={this.state.verificationRequest}
                onClose={this.props.onFinished}
                member={MatrixClientPeg.get().getUser(this.state.verificationRequest.otherUserId)}
            />;
        } else if (phase === PHASE_INTRO) {
            const store = SetupEncryptionStore.sharedInstance();
            let recoveryKeyPrompt;
            if (store.keyInfo && keyHasPassphrase(store.keyInfo)) {
                recoveryKeyPrompt = _t("Use Recovery Key or Passphrase");
            } else if (store.keyInfo) {
                recoveryKeyPrompt = _t("Use Recovery Key");
            }

            let useRecoveryKeyButton;
            let resetKeysCaption;
            if (recoveryKeyPrompt) {
                useRecoveryKeyButton = <AccessibleButton kind="link" onClick={this._onUsePassphraseClick}>
                    {recoveryKeyPrompt}
                </AccessibleButton>;
                resetKeysCaption = _td(
                    "If you've forgotten your recovery key you can <button>set up new recovery options</button>",
                );
            } else {
                resetKeysCaption = _td(
                    "If you have no other devices you can <button>set up new recovery options</button>",
                );
            }

            return (
                <div>
                    <p>{_t(
                        "Confirm your identity by verifying this login from one of your other sessions, " +
                        "granting it access to encrypted messages.",
                    )}</p>
                    <p>{_t(
                        "This requires the latest Riot on your other devices:",
                    )}</p>

                    <div className="mx_CompleteSecurity_clients">
                        <div className="mx_CompleteSecurity_clients_desktop">
                            <div>Riot Web</div>
                            <div>Riot Desktop</div>
                        </div>
                        <div className="mx_CompleteSecurity_clients_mobile">
                            <div>Riot iOS</div>
                            <div>Riot X for Android</div>
                        </div>
                        <p>{_t("or another cross-signing capable Matrix client")}</p>
                    </div>

                    <div className="mx_CompleteSecurity_actionRow">
                        {useRecoveryKeyButton}
                        <AccessibleButton kind="danger" onClick={this.onSkipClick}>
                            {_t("Skip")}
                        </AccessibleButton><commit_msg>Unused code & import<commit_after>/*
Copyright 2020 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import React from 'react';
import PropTypes from 'prop-types';
import { _t } from '../../../languageHandler';
import { MatrixClientPeg } from '../../../MatrixClientPeg';
import * as sdk from '../../../index';
import {
    SetupEncryptionStore,
    PHASE_INTRO,
    PHASE_BUSY,
    PHASE_DONE,
    PHASE_CONFIRM_SKIP,
    PHASE_FINISHED,
} from '../../../stores/SetupEncryptionStore';

function keyHasPassphrase(keyInfo) {
    return (
        keyInfo.passphrase &&
        keyInfo.passphrase.salt &&
        keyInfo.passphrase.iterations
    );
}

export default class SetupEncryptionBody extends React.Component {
    static propTypes = {
        onFinished: PropTypes.func.isRequired,
    };

    constructor() {
        super();
        const store = SetupEncryptionStore.sharedInstance();
        store.on("update", this._onStoreUpdate);
        store.start();
        this.state = {
            phase: store.phase,
            // this serves dual purpose as the object for the request logic and
            // the presence of it indicating that we're in 'verify mode'.
            // Because of the latter, it lives in the state.
            verificationRequest: store.verificationRequest,
            backupInfo: store.backupInfo,
        };
    }

    _onStoreUpdate = () => {
        const store = SetupEncryptionStore.sharedInstance();
        if (store.phase === PHASE_FINISHED) {
            this.props.onFinished();
            return;
        }
        this.setState({
            phase: store.phase,
            verificationRequest: store.verificationRequest,
            backupInfo: store.backupInfo,
        });
    };

    componentWillUnmount() {
        const store = SetupEncryptionStore.sharedInstance();
        store.off("update", this._onStoreUpdate);
        store.stop();
    }

    _onUsePassphraseClick = async () => {
        const store = SetupEncryptionStore.sharedInstance();
        store.usePassPhrase();
    }

    onSkipClick = () => {
        const store = SetupEncryptionStore.sharedInstance();
        store.skip();
    }

    onSkipConfirmClick = () => {
        const store = SetupEncryptionStore.sharedInstance();
        store.skipConfirm();
    }

    onSkipBackClick = () => {
        const store = SetupEncryptionStore.sharedInstance();
        store.returnAfterSkip();
    }

    onDoneClick = () => {
        const store = SetupEncryptionStore.sharedInstance();
        store.done();
    }

    render() {
        const AccessibleButton = sdk.getComponent("elements.AccessibleButton");

        const {
            phase,
        } = this.state;

        if (this.state.verificationRequest) {
            const EncryptionPanel = sdk.getComponent("views.right_panel.EncryptionPanel");
            return <EncryptionPanel
                layout="dialog"
                verificationRequest={this.state.verificationRequest}
                onClose={this.props.onFinished}
                member={MatrixClientPeg.get().getUser(this.state.verificationRequest.otherUserId)}
            />;
        } else if (phase === PHASE_INTRO) {
            const store = SetupEncryptionStore.sharedInstance();
            let recoveryKeyPrompt;
            if (store.keyInfo && keyHasPassphrase(store.keyInfo)) {
                recoveryKeyPrompt = _t("Use Recovery Key or Passphrase");
            } else if (store.keyInfo) {
                recoveryKeyPrompt = _t("Use Recovery Key");
            }

            let useRecoveryKeyButton;
            if (recoveryKeyPrompt) {
                useRecoveryKeyButton = <AccessibleButton kind="link" onClick={this._onUsePassphraseClick}>
                    {recoveryKeyPrompt}
                </AccessibleButton>;
            }

            return (
                <div>
                    <p>{_t(
                        "Confirm your identity by verifying this login from one of your other sessions, " +
                        "granting it access to encrypted messages.",
                    )}</p>
                    <p>{_t(
                        "This requires the latest Riot on your other devices:",
                    )}</p>

                    <div className="mx_CompleteSecurity_clients">
                        <div className="mx_CompleteSecurity_clients_desktop">
                            <div>Riot Web</div>
                            <div>Riot Desktop</div>
                        </div>
                        <div className="mx_CompleteSecurity_clients_mobile">
                            <div>Riot iOS</div>
                            <div>Riot X for Android</div>
                        </div>
                        <p>{_t("or another cross-signing capable Matrix client")}</p>
                    </div>

                    <div className="mx_CompleteSecurity_actionRow">
                        {useRecoveryKeyButton}
                        <AccessibleButton kind="danger" onClick={this.onSkipClick}>
                            {_t("Skip")}
                        </AccessibleButton>
{'fraction_non_alphanumeric': 0.09857216583519697, 'fraction_numerical': 0.0013762257010149664, 'mean_word_length': 1.8220873786407767, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2398660', 'n_tokens_mistral': 2976, 'n_tokens_neox': 2862, 'n_words': 727}