<commit_before>import SpvSyncFormBody from "./Form";
import { getDcrwalletLastLogLine } from "wallet";
import ReactTimeout from "react-timeout";

function parseLogLine(line) {
  const res = /^[\d :\-.]+ \[...\] (.+)$/.exec(line);
  return res ? res[1] : "";
}

@autobind
class SpvSync extends React.Component {
  constructor(props)  {
    super(props);
    this.state = this.getInitialState();
  }

  componentDidMount() {
    this.props.setInterval(() => {
      Promise
        .all([ getDcrwalletLastLogLine() ])
        .then(([ dcrwalletLine ]) => {
          const lastDcrwalletLogLine = parseLogLine(dcrwalletLine);
          if (lastDcrwalletLogLine !== this.lastDcrwalletLogLine)
          {
            this.lastDcrwalletLogLine = lastDcrwalletLogLine;
          }
        });
    }, 2000);
    if (this.props.walletPrivatePassphrase) {
      this.props.onSpvSynces(this.props.walletPrivatePassphrase);
    }
  }

  componentWillUnmount() {
    this.resetState();
  }

  getInitialState() {
    return {
      lastDcrdLogLine: "",
      lastDcrwalletLogLine: "",
      passPhrase: "",
      hasAttemptedDiscover: false
    };
  }
  render() {
    const { passPhrase, hasAttemptedDiscover } = this.state;
    const { onSetPassPhrase, onSpvSync, onKeyDown, lastDcrwalletLogLine } = this;
    const { Form,
      firstBlockTime,
      syncFetchTimeStart,
      syncFetchHeadersLastHeaderTime,
      syncFetchHeadersComplete } = this.props;
    return (
      <SpvSyncFormBody {...{
        ...this.props,
        firstBlockTime,
        syncFetchHeadersComplete,
        syncFetchTimeStart,
        syncFetchHeadersLastHeaderTime,
        Form,
        lastDcrwalletLogLine,
        passPhrase,
        hasAttemptedDiscover,
        onSetPassPhrase,
        onSpvSync,
        onKeyDown }}/>);
  }

  resetState() {
    this.setState(this.getInitialState());
  }

  onSetPassPhrase(passPhrase) {
    if (passPhrase != "") {
      this.setState({ hasAttemptedDiscover: true });
    }

    this.setState({ passPhrase });
  }

  onSpvSync() {
    const { passPhrase } = this.state;

    if (!passPhrase) {
      return this.setState({ hasAttemptedDiscover: true });
    }

    const { startSPVSync, onSetWalletPrivatePassphrase } = this.props;

    onSetWalletPrivatePassphrase && onSetWalletPrivatePassphrase(passPhrase);
    startSPVSync(passPhrase);
    this.resetState();
  }

  onKeyDown(e) {
    if (e.keyCode == 13) {   // Enter key
      e.preventDefault();
      this.onSpvSync();
    }
  }

}

export default ReactTimeout(SpvSync);
<commit_msg>[launcher] Fix restore wallet with SPV (#1741)<commit_after>import SpvSyncFormBody from "./Form";
import { getDcrwalletLastLogLine } from "wallet";
import ReactTimeout from "react-timeout";

function parseLogLine(line) {
  const res = /^[\d :\-.]+ \[...\] (.+)$/.exec(line);
  return res ? res[1] : "";
}

@autobind
class SpvSync extends React.Component {
  constructor(props)  {
    super(props);
    this.state = this.getInitialState();
  }

  componentDidMount() {
    this.props.setInterval(() => {
      Promise
        .all([ getDcrwalletLastLogLine() ])
        .then(([ dcrwalletLine ]) => {
          const lastDcrwalletLogLine = parseLogLine(dcrwalletLine);
          if (lastDcrwalletLogLine !== this.lastDcrwalletLogLine)
          {
            this.lastDcrwalletLogLine = lastDcrwalletLogLine;
          }
        });
    }, 2000);
    if (this.props.walletPrivatePassphrase) {
      this.props.startSPVSync(this.props.walletPrivatePassphrase);
    }
  }

  componentWillUnmount() {
    this.resetState();
  }

  getInitialState() {
    return {
      lastDcrdLogLine: "",
      lastDcrwalletLogLine: "",
      passPhrase: "",
      hasAttemptedDiscover: false
    };
  }
  render() {
    const { passPhrase, hasAttemptedDiscover } = this.state;
    const { onSetPassPhrase, onSpvSync, onKeyDown, lastDcrwalletLogLine } = this;
    const { Form,
      firstBlockTime,
      syncFetchTimeStart,
      syncFetchHeadersLastHeaderTime,
      syncFetchHeadersComplete } = this.props;
    return (
      <SpvSyncFormBody {...{
        ...this.props,
        firstBlockTime,
        syncFetchHeadersComplete,
        syncFetchTimeStart,
        syncFetchHeadersLastHeaderTime,
        Form,
        lastDcrwalletLogLine,
        passPhrase,
        hasAttemptedDiscover,
        onSetPassPhrase,
        onSpvSync,
        onKeyDown }}/>);
  }

  resetState() {
    this.setState(this.getInitialState());
  }

  onSetPassPhrase(passPhrase) {
    if (passPhrase != "") {
      this.setState({ hasAttemptedDiscover: true });
    }

    this.setState({ passPhrase });
  }

  onSpvSync() {
    const { passPhrase } = this.state;

    if (!passPhrase) {
      return this.setState({ hasAttemptedDiscover: true });
    }

    const { startSPVSync, onSetWalletPrivatePassphrase } = this.props;

    onSetWalletPrivatePassphrase && onSetWalletPrivatePassphrase(passPhrase);
    startSPVSync(passPhrase);
    this.resetState();
  }

  onKeyDown(e) {
    if (e.keyCode == 13) {   // Enter key
      e.preventDefault();
      this.onSpvSync();
    }
  }

}

export default ReactTimeout(SpvSync);

{'fraction_non_alphanumeric': 0.12407766990291262, 'fraction_numerical': 0.003495145631067961, 'mean_word_length': 2.7084233261339095, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '821531', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1676, 'n_words': 264}