<commit_before>import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'

import { Label } from 'semantic-ui-react'
import { makeDebugger } from '../../../../src/lib'

const debug = makeDebugger('carbon-ad-native')

class CarbonAdNative extends PureComponent {
  static propTypes = {
    inverted: PropTypes.bool,
  }

  state = {}

  componentDidMount() {
    debug('componentDidMount', { mounted: this.mounted })
    this.mounted = true

    this.getAd()
  }

  componentWillUpdate() {
    const shouldGetAd = Date.now() - this.timeOfLastAd > 10000
    debug('componentWillUpdate', { mounted: this.mounted, shouldGetAd })
    if (shouldGetAd) {
      this.getAd()
    }
  }

  componentWillUnmount() {
    this.mounted = false
    this.cleanup()
  }

  cleanup = () => {
    debug('cleanup', { script: this.script, mounted: this.mounted })
    if (!this.script) return

    document.getElementsByTagName('head')[0].removeChild(this.script)
    this.script = null
  }

  getAd = () => {
    debug('getAd', { mounted: this.mounted })
    if (!this.mounted) return

    window._handleNativeJSON = this.handleNativeJSON
    this.timeOfLastAd = Date.now()

    this.cleanup()

    this.script = document.createElement('script')
    this.script.src = `https://srv.buysellads.com/ads/CK7DC2QW.json?${[
      'segment=placement:reactsemanticuicom',
      'callback=_handleNativeJSON',
      `v=${Date.now()}`,
    ].join('&')}`

    document.getElementsByTagName('head')[0].appendChild(this.script)
  }

  handleNativeJSON = (json) => {
    debug('handleNativeJSON', { mounted: this.mounted })
    try {
      const sanitizedAd = json.ads
        .filter(ad => Object.keys(ad).length > 0)
        .filter(ad => !!ad.statlink)
        .filter(Boolean)[0]
      debug('handleNativeJSON sanitizedAd', sanitizedAd)

      if (!sanitizedAd) {
        this.getAd()
      } else if (this.mounted) {
        this.setState({ ad: sanitizedAd })
      }
    } catch (err) {
      // eslint-disable-next-line no-console
      console.error(err)
    }
  }

  render() {
    const { inverted } = this.props
    const { ad } = this.state<commit_msg>fix(docs): remove infinite ad loop fixes #3447<commit_after>import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'

import { Label } from 'semantic-ui-react'
import { makeDebugger } from '../../../../src/lib'

const debug = makeDebugger('carbon-ad-native')

const MAX_FAILED_ADS = 10

class CarbonAdNative extends PureComponent {
  static propTypes = {
    inverted: PropTypes.bool,
  }

  state = {}

  componentDidMount() {
    debug('componentDidMount', { mounted: this.mounted })
    this.mounted = true
    this.failedAds = 0

    this.getAd()
  }

  componentWillUpdate() {
    const shouldGetAd = Date.now() - this.timeOfLastAd > 10000
    debug('componentWillUpdate', { mounted: this.mounted, shouldGetAd })
    if (shouldGetAd) {
      this.getAd()
    }
  }

  componentWillUnmount() {
    this.mounted = false
    this.cleanup()
  }

  cleanup = () => {
    debug('cleanup', { script: this.script, mounted: this.mounted })
    if (!this.script) return

    document.getElementsByTagName('head')[0].removeChild(this.script)
    this.script = null
  }

  getAd = () => {
    debug('getAd', { mounted: this.mounted })
    if (!this.mounted) return

    window._handleNativeJSON = this.handleNativeJSON
    this.timeOfLastAd = Date.now()

    this.cleanup()

    this.script = document.createElement('script')
    this.script.src = `https://srv.buysellads.com/ads/CK7DC2QW.json?${[
      'segment=placement:reactsemanticuicom',
      'callback=_handleNativeJSON',
      `v=${Date.now()}`,
    ].join('&')}`

    document.getElementsByTagName('head')[0].appendChild(this.script)
  }

  handleNativeJSON = (json) => {
    debug('handleNativeJSON', { mounted: this.mounted })
    try {
      const sanitizedAd = json.ads
        .filter(ad => Object.keys(ad).length > 0)
        .filter(ad => !!ad.statlink)
        .filter(Boolean)[0]
      debug('handleNativeJSON sanitizedAd', sanitizedAd)

      if (!sanitizedAd) {
        this.failedAds += 1

        if (this.failedAds < MAX_FAILED_ADS) {
          this.getAd()
        }
      } else if (this.mounted) {
        this.setState({ ad: sanitizedAd })
        this.failedAds = 0
      }
    } catch (err) {
      // eslint-disable-next-line no-console
      console.error(err)
    }
  }

  render() {
    const { inverted } = this.props
    const { ad } = this.state
{'fraction_non_alphanumeric': 0.14895509115162295, 'fraction_numerical': 0.006891951978657181, 'mean_word_length': 2.8387372013651877, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 17, 'https://': 2, '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': '3862069', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1436, 'n_words': 267}