<commit_before>/* eslint-disable no-underscore-dangle */
/* eslint-disable react/forbid-prop-types */
import React from 'react';
import { Button, OverlayTrigger, Popover } from 'react-bootstrap';
import PropTypes from 'prop-types';
import uuid from 'uuid';
import MoleculesFetcher from '../fetchers/MoleculesFetcher';
import NotificationActions from '../actions/NotificationActions';
import LoadingActions from '../actions/LoadingActions';

const notify = (params) => {
  NotificationActions.add({
    title: params.title, message: params.msg, level: params.lvl, position: 'tc', dismissible: 'button', uid: uuid.v4()
  });
};

const buildRxn = (reaction) => {
  let rxn = '$RXN\n\n\n\n';
  const molBreak = '$MOL\n';
  let rl = 0;
  let xl = 0;
  let pl = 0;
  let molfiles = '';
  reaction._starting_materials.map((e) => {
    rl += 1;
    molfiles += molBreak + e.molfile.slice(0, e.molfile.lastIndexOf('$$$$'));
  });
  reaction._reactants.map((e) => {
    xl += 1;
    molfiles += molBreak + e.molfile.slice(0, e.molfile.lastIndexOf('$$$$'));
  });
  reaction._products.map((e) => {
    pl += 1;
    molfiles += molBreak + e.molfile.slice(0, e.molfile.lastIndexOf('$$$$'));
  });
  rxn += (`   ${(rl + xl).toString()}`).slice(-3);
  rxn += (`   ${pl.toString()}`).slice(-3);
  rxn += '\n';
  rxn += molfiles;
  return { rxn };
};

const ScifinderSearch = (props) => {
  const { el } = props;
  const search = (_type) => {
    const str = el.type === 'reaction' ? buildRxn(el).rxn : el.molfile;
    const params = { str, search: _type, ctype: 'x-mdl-rxnfile' };
    LoadingActions.start();
    MoleculesFetcher.fetchSciFinder(params)
      .then((result) => {
        if (result.errors.length === 0) {
          const url = `${result.host}${result.path}`;
          window.open(url, '_blank');
        } else {
          notify({ title: 'SciFinder-n Error', lvl: 'error', msg: result.errors[0] });
        }
      }).catch((errorMessage) => {
        console.log(errorMessage);
        notify({ title: 'SciFinder-n Error', lvl: 'error', msg: errorMessage });
      }).finally(() => LoadingActions.stop());
  };

  const renderButtons = () => {
    const BTNS = [
      { substances: 'icon-sample' }, { reactions: 'icon-reaction' }, { references: 'fa fa-book' }, { suppliers: 'fa fa-shopping-cart' }
    ];
    if (el.type === 'reaction') { [BTNS[0], BTNS[1]] = [BTNS[1], BTNS[0]]; }
    const btn = (name, icon) => (
      <Button key={`_sfn_btn_${name}`} onClick={() => search(name)}>
        <span style={{ float: 'left' }}><i className={icon} aria-hidden="true" /></span>&nbsp;
        <span><b>{name}</b></span>
      </Button>
    );
    return BTNS.map(b => Object.entries(b).map(([key, value]) => btn(key, value)));
  };

  const popoverSettings = (
    <Popover
      className="collection-overlay"
      id="popover-layout"
      style={{ maxWidth: 'none', width: 'auto' }}
    >
      <div>
        <h3 className="popover-title">SciFinder-n Search for</h3>
        <div className="popover-content" style={{ display: 'flex', flexDirection: 'column' }}>
          {renderButtons()}
        </div>
      </div>
    </Popover>
  );

  return (
    <OverlayTrigger trigger="click" placement="left" overlay={popoverSettings} rootClose>
      <Button className="button-right" style={{ padding: '0px' }}>
        <img src="/images/sfn-horizontal-logo-header.svg" style={{ width: '5vw' }} alt="SciFinder-n" />
      </Button>
    </OverlayTrigger>
  );
};

ScifinderSearch.propTypes = { el: PropTypes.object.isRequired };
export default ScifinderSearch;
<commit_msg>replace logo with text (#859)<commit_after>/* eslint-disable no-underscore-dangle */
/* eslint-disable react/forbid-prop-types */
import React from 'react';
import { Button, OverlayTrigger, Popover } from 'react-bootstrap';
import PropTypes from 'prop-types';
import uuid from 'uuid';
import MoleculesFetcher from '../fetchers/MoleculesFetcher';
import NotificationActions from '../actions/NotificationActions';
import LoadingActions from '../actions/LoadingActions';

const notify = (params) => {
  NotificationActions.add({
    title: params.title, message: params.msg, level: params.lvl, position: 'tc', dismissible: 'button', uid: uuid.v4()
  });
};

const buildRxn = (reaction) => {
  let rxn = '$RXN\n\n\n\n';
  const molBreak = '$MOL\n';
  let rl = 0;
  let xl = 0;
  let pl = 0;
  let molfiles = '';
  reaction._starting_materials.map((e) => {
    rl += 1;
    molfiles += molBreak + e.molfile.slice(0, e.molfile.lastIndexOf('$$$$'));
  });
  reaction._reactants.map((e) => {
    xl += 1;
    molfiles += molBreak + e.molfile.slice(0, e.molfile.lastIndexOf('$$$$'));
  });
  reaction._products.map((e) => {
    pl += 1;
    molfiles += molBreak + e.molfile.slice(0, e.molfile.lastIndexOf('$$$$'));
  });
  rxn += (`   ${(rl + xl).toString()}`).slice(-3);
  rxn += (`   ${pl.toString()}`).slice(-3);
  rxn += '\n';
  rxn += molfiles;
  return { rxn };
};

const ScifinderSearch = (props) => {
  const { el } = props;
  const search = (_type) => {
    const str = el.type === 'reaction' ? buildRxn(el).rxn : el.molfile;
    const params = { str, search: _type, ctype: 'x-mdl-rxnfile' };
    LoadingActions.start();
    MoleculesFetcher.fetchSciFinder(params)
      .then((result) => {
        if (result.errors.length === 0) {
          const url = `${result.host}${result.path}`;
          window.open(url, '_blank');
        } else {
          notify({ title: 'SciFinder-n Error', lvl: 'error', msg: result.errors[0] });
        }
      }).catch((errorMessage) => {
        console.log(errorMessage);
        notify({ title: 'SciFinder-n Error', lvl: 'error', msg: errorMessage });
      }).finally(() => LoadingActions.stop());
  };

  const renderButtons = () => {
    const BTNS = [
      { substances: 'icon-sample' }, { reactions: 'icon-reaction' }, { references: 'fa fa-book' }, { suppliers: 'fa fa-shopping-cart' }
    ];
    if (el.type === 'reaction') { [BTNS[0], BTNS[1]] = [BTNS[1], BTNS[0]]; }
    const btn = (name, icon) => (
      <Button key={`_sfn_btn_${name}`} onClick={() => search(name)}>
        <span style={{ float: 'left' }}><i className={icon} aria-hidden="true" /></span>&nbsp;
        <span><b>{name}</b></span>
      </Button>
    );
    return BTNS.map(b => Object.entries(b).map(([key, value]) => btn(key, value)));
  };

  const popoverSettings = (
    <Popover
      className="collection-overlay"
      id="popover-layout"
      style={{ maxWidth: 'none', width: 'auto' }}
    >
      <div>
        <h3 className="popover-title">SciFinder-n Search for</h3>
        <div className="popover-content" style={{ display: 'flex', flexDirection: 'column' }}>
          {renderButtons()}
        </div>
      </div>
    </Popover>
  );

  return (
    <OverlayTrigger trigger="click" placement="left" overlay={popoverSettings} rootClose>
      <Button className="button-right" style={{ padding: '0px' }}>
        <div style={{ fontSize: '1.2vmin', padding: '2px', color: '#337ab7' }}><i className="fa fa-search" aria-hidden="true" />&nbsp;CAS content</div>
      </Button>
    </OverlayTrigger>
  );
};

ScifinderSearch.propTypes = { el: PropTypes.object.isRequired };
export default ScifinderSearch;

{'fraction_non_alphanumeric': 0.20030538589672403, 'fraction_numerical': 0.007357023875624653, 'mean_word_length': 3.2085280373831777, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 79, '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': '7145789', 'n_tokens_mistral': 2495, 'n_tokens_neox': 2474, 'n_words': 518}