<commit_before>import React, {
  forwardRef,
  memo,
  useState,
} from 'react';
import { View, Dimensions, ViewPropTypes, SectionList } from 'react-native';
import PropTypes from 'prop-types';
import { generateStyles, calculateDimensions, chunkArray } from './utils';

const SectionGrid = memo(
  forwardRef((props, ref) => {
    const {
      sections,
      style,
      spacing,
      fixed,
      itemDimension,
      staticDimension,
      renderItem: originalRenderItem,
      keyExtractor,
      ...restProps
    } = props;
    const { onLayout } = props;
    const { itemContainerStyle } = props;

    const [totalDimension, setTotalDimension] = useState(
      staticDimension || Dimensions.get('window').width,
    );

    const onLocalLayout = e => {
      if (!staticDimension) {
        const { width: newTotalDimension } = e.nativeEvent.layout || {};

        if (totalDimension !== newTotalDimension) {
          setTotalDimension(newTotalDimension);
        }
      }

      // call onLayout prop if passed
      if (onLayout) {
        onLayout(e);
      }
    };

    const renderRow = ({
                         renderItem,
                         rowItems,
                         rowIndex,
                         section,
                         itemsPerRow,
                         rowStyle,
                         separators,
                         isFirstRow,
                         containerStyle,
                       }) => {
      // Add spacing below section header
      let additionalRowStyle = {};
      if (isFirstRow) {
        additionalRowStyle = {
          marginTop: spacing,
        };
      }

      return (
        <View style={[rowStyle, additionalRowStyle]}>
          {rowItems.map((item, i) => (
            <View
              key={
                keyExtractor
                  ? keyExtractor(item, i)
                  : `item_${rowIndex * itemsPerRow + i}`
              }
              style={[containerStyle, itemContainerStyle]}
            >
              {renderItem({
                item,
                index: rowIndex * itemsPerRow + i,
                section,
                separators,
                rowIndex,
              })}
            </View>
          ))}
        </View>
      );
    };

    const {
      containerDimension,
      itemsPerRow,
      fixedSpacing,
    } = calculateDimensions({
      itemDimension,
      staticDimension,
      totalDimension,
      spacing,
      fixed,
    });

    const { containerStyle, rowStyle } = generateStyles({
      itemDimension,
      containerDimension,
      spacing,
      fixedSpacing,
      fixed,
    });

    const groupedSections = sections.map(section => {
      const chunkedData = chunkArray(section.data, itemsPerRow);
      const renderItem = section.renderItem || originalRenderItem;
      return {
        ...section,
        renderItem: ({ item, index, section }) =>
          renderRow({
            renderItem,
            rowItems: item,
            rowIndex: index,
            section,
            isFirstRow: index === 0,
            itemsPerRow,
            rowStyle,
            containerStyle,
          }),
        data: chunkedData,
        originalData: section.data,
      };
    });

    return (
      <SectionList
        sections={groupedSections}
        keyExtractor={(rowItems, index) => {
          if (keyExtractor) {
            return rowItems
              .map((rowItem, rowItemIndex) => {
                return keyExtractor(rowItem, rowItemIndex);
              })
              .join('_');
          } else {
            return `row_${index}`;
          }
        }}
        style={style}
        onLayout={onLocalLayout}
        ref={ref}
        {...restProps}
      />
    );
  }),
);

SectionGrid.propTypes = {
  renderItem: PropTypes.func,
  sections: PropTypes.arrayOf(PropTypes.any).isRequired,
  itemDimension: PropTypes.number,<commit_msg>Another round of optimizations<commit_after>import React, { forwardRef, memo, useCallback, useMemo, useState } from 'react';
import { View, Dimensions, ViewPropTypes, SectionList } from 'react-native';
import PropTypes from 'prop-types';
import { generateStyles, calculateDimensions, chunkArray } from './utils';

const SectionGrid = memo(
  forwardRef((props, ref) => {
    const {
      sections,
      style,
      spacing,
      fixed,
      itemDimension,
      staticDimension,
      renderItem: originalRenderItem,
      keyExtractor,
      onLayout,
      itemContainerStyle,
      ...restProps
    } = props;

    const [totalDimension, setTotalDimension] = useState(
      staticDimension || Dimensions.get('window').width,
    );

    const onLocalLayout = useCallback(
      e => {
        if (!staticDimension) {
          const { width: newTotalDimension } = e.nativeEvent.layout || {};

          if (totalDimension !== newTotalDimension) {
            setTotalDimension(newTotalDimension);
          }
        }

        // call onLayout prop if passed
        if (onLayout) {
          onLayout(e);
        }
      },
      [onLayout, staticDimension, totalDimension],
    );

    const renderRow = useCallback(
      ({
         renderItem,
         rowItems,
         rowIndex,
         section,
         itemsPerRow,
         rowStyle,
         separators,
         isFirstRow,
         containerStyle,
       }) => {
        // Add spacing below section header
        let additionalRowStyle = {};
        if (isFirstRow) {
          additionalRowStyle = {
            marginTop: spacing,
          };
        }

        return (
          <View style={[rowStyle, additionalRowStyle]}>
            {rowItems.map((item, i) => (
              <View
                key={
                  keyExtractor
                    ? keyExtractor(item, i)
                    : `item_${rowIndex * itemsPerRow + i}`
                }
                style={[containerStyle, itemContainerStyle]}
              >
                {renderItem({
                  item,
                  index: rowIndex * itemsPerRow + i,
                  section,
                  separators,
                  rowIndex,
                })}
              </View>
            ))}
          </View>
        );
      },
      [spacing, keyExtractor, itemContainerStyle],
    );

    const { containerDimension, itemsPerRow, fixedSpacing } = useMemo(
      () =>
        calculateDimensions({
          itemDimension,
          staticDimension,
          totalDimension,
          spacing,
          fixed,
        }),
      [itemDimension, staticDimension, totalDimension, spacing, fixed],
    );

    const { containerStyle, rowStyle } = useMemo(
      () =>
        generateStyles({
          itemDimension,
          containerDimension,
          spacing,
          fixedSpacing,
          fixed,
        }),
      [itemDimension, containerDimension, spacing, fixedSpacing, fixed],
    );

    const groupSectionsFunc = useCallback(
      section => {
        const chunkedData = chunkArray(section.data, itemsPerRow);
        const renderItem = section.renderItem || originalRenderItem;
        return {
          ...section,
          renderItem: ({ item, index, section }) =>
            renderRow({
              renderItem,
              rowItems: item,
              rowIndex: index,
              section,
              isFirstRow: index === 0,
              itemsPerRow,
              rowStyle,
              containerStyle,
            }),
          data: chunkedData,
          originalData: section.data,
        };
      },
      [
        itemsPerRow,
        originalRenderItem,
        renderRow,
        rowStyle,
        containerStyle,
      ],
    );

    const groupedSections = sections.map(groupSectionsFunc);

    const localKeyExtractor = useCallback(
      (rowItems, index) => {
        if (keyExtractor) {
          return rowItems
            .map((rowItem, rowItemIndex) => {
              return keyExtractor(rowItem, rowItemIndex);
            })
            .join('_');
        } else {
          return `row_${index}`;
        }
      },
      [keyExtractor],
    );

    return (
      <View onLayout={onLocalLayout}>
        <SectionList
          sections={groupedSections}
          keyExtractor={localKeyExtractor}
          style={style}
          ref={ref}
          {...restProps}
        />
      </View>
    );
  }),
);

SectionGrid.displayName = 'SectionGrid';
SectionGrid.propTypes = {
  renderItem: PropTypes.func,
  sections: PropTypes.arrayOf(PropTypes.any).isRequired,
  itemDimension: PropTypes.number,
{'fraction_non_alphanumeric': 0.10159804035926746, 'fraction_numerical': 0.00023329056339671062, 'mean_word_length': 1.5254786450662738, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 33, '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': '566386', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2223, 'n_words': 450}