<commit_before>import { document } from 'global';
import React, { Component, FunctionComponent, ReactElement, StrictMode } from 'react';
import ReactDOM from 'react-dom';

import { RenderContext } from './types';

// will be provided by the webpack define plugin
declare const FRAMEWORK_OPTIONS: {
  fastRefresh?: boolean;
  strictMode?: boolean;
};

const rootEl = document ? document.getElementById('root') : null;

const render = (node: ReactElement, el: Element) =>
  new Promise((resolve) => {
    ReactDOM.render(node, el, resolve);
  });

class ErrorBoundary extends Component<{
  showException: (err: Error) => void;
  showMain: () => void;
}> {
  state = { hasError: false };

  static getDerivedStateFromError() {
    return { hasError: true };
  }

  componentDidMount() {
    const { hasError } = this.state;
    const { showMain } = this.props;
    if (!hasError) {
      showMain();
    }
  }

  componentDidCatch(err: Error) {
    const { showException } = this.props;
    // message partially duplicates stack, strip it
    showException(err);
  }

  render() {
    const { hasError } = this.state;
    const { children } = this.props;

    return hasError ? null : children;
  }
}

const isStrict = process.env.STORYBOOK_EXAMPLE_APP || FRAMEWORK_OPTIONS?.strictMode;
const Wrapper = isStrict ? React.StrictMode : React.Fragment;

export default async function renderMain({
  storyFn,
  showMain,
  showException,
  forceRender,
}: RenderContext) {
  // storyFn has context bound in by now so can be treated as a function component with no args
  const StoryFn = storyFn as FunctionComponent;

  const element = (
    <Wrapper>
      <ErrorBoundary showMain={showMain} showException={showException}>
        <StoryFn />
      </ErrorBoundary>
    </Wrapper><commit_msg>Fix unit tests<commit_after>import { document, FRAMEWORK_OPTIONS } from 'global';
import React, { Component, FunctionComponent, ReactElement, StrictMode } from 'react';
import ReactDOM from 'react-dom';

import { RenderContext } from './types';

const rootEl = document ? document.getElementById('root') : null;

const render = (node: ReactElement, el: Element) =>
  new Promise((resolve) => {
    ReactDOM.render(node, el, resolve);
  });

class ErrorBoundary extends Component<{
  showException: (err: Error) => void;
  showMain: () => void;
}> {
  state = { hasError: false };

  static getDerivedStateFromError() {
    return { hasError: true };
  }

  componentDidMount() {
    const { hasError } = this.state;
    const { showMain } = this.props;
    if (!hasError) {
      showMain();
    }
  }

  componentDidCatch(err: Error) {
    const { showException } = this.props;
    // message partially duplicates stack, strip it
    showException(err);
  }

  render() {
    const { hasError } = this.state;
    const { children } = this.props;

    return hasError ? null : children;
  }
}

const Wrapper = FRAMEWORK_OPTIONS?.strictMode ? React.StrictMode : React.Fragment;

export default async function renderMain({
  storyFn,
  showMain,
  showException,
  forceRender,
}: RenderContext) {
  // storyFn has context bound in by now so can be treated as a function component with no args
  const StoryFn = storyFn as FunctionComponent;

  const element = (
    <Wrapper>
      <ErrorBoundary showMain={showMain} showException={showException}>
        <StoryFn />
      </ErrorBoundary>
    </Wrapper>
{'fraction_non_alphanumeric': 0.11773384479197403, 'fraction_numerical': 0.0, 'mean_word_length': 3.599728629579376, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, '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': '5511732', 'n_tokens_mistral': 1022, 'n_tokens_neox': 1001, 'n_words': 289}