<commit_before>import clsx from 'clsx';
import React, { useState, useRef, useLayoutEffect, useEffect } from 'react';
import styles from './ExpandableTextBlock.m.scss';

export function ExpandableTextBlock({
  linesWhenClosed,
  alreadyOpen,
  children,
  className,
}: {
  linesWhenClosed: number;
  alreadyOpen?: boolean;
  children: React.ReactNode;
  className?: string;
}) {
  const [isOpen, setOpen] = useState(alreadyOpen);
  const [closedHeight, setClosedHeight] = useState<number>();
  const contentRef = useRef<HTMLDivElement>(null);
  const wrapperRef = useRef<HTMLDivElement>(null);

  useLayoutEffect(() => {
    setClosedHeight(contentRef.current!.clientHeight);
  }, []);
  useEffect(() => {
    if (closedHeight && wrapperRef.current!.clientHeight >= contentRef.current!.clientHeight) {
      setOpen(true);
    }
  }, [closedHeight]);

  return (
    <div
      className={clsx(className, styles.textBlockWrapper, { [styles.open]: isOpen })}
      ref={wrapperRef}
      onClick={() => setOpen(true)}
      style={{ height: isOpen ? 'max-content' : closedHeight, overflow: 'hidden' }}
    >
      <div
        ref={contentRef}
        style={
          closedHeight
            ? undefined
            : {
                WebkitLineClamp: linesWhenClosed,
                WebkitBoxOrient: 'vertical',
                display: '-webkit-box',
              }
        }<commit_msg>state-base expandable text with line clamping<commit_after>import clsx from 'clsx';
import React, { useState, useRef, useLayoutEffect, useEffect } from 'react';
import styles from './ExpandableTextBlock.m.scss';

export function ExpandableTextBlock({
  linesWhenClosed,
  alreadyOpen,
  children,
  className,
}: {
  linesWhenClosed: number;
  alreadyOpen?: boolean;
  children: React.ReactNode;
  className?: string;
}) {
  const [isOpen, setOpen] = useState(alreadyOpen);
  const [closedHeight, setClosedHeight] = useState<number>();
  const contentRef = useRef<HTMLDivElement>(null);
  const wrapperRef = useRef<HTMLDivElement>(null);

  // measure the element height, with lines clamped, the first time this component exists
  useLayoutEffect(() => {
    setClosedHeight(contentRef.current!.clientHeight);
  }, []);
  // after the element has been measured, set isOpen if the unclamped text still fits in clamped height
  useEffect(() => {
    if (closedHeight && wrapperRef.current!.clientHeight >= contentRef.current!.clientHeight) {
      setOpen(true);
    }
  }, [closedHeight]);

  return (
    <div
      className={clsx(className, styles.textBlockWrapper, { [styles.open]: isOpen })}
      ref={wrapperRef}
      onClick={() => setOpen(true)}
      style={{ height: isOpen ? 'max-content' : closedHeight, overflow: 'hidden' }}
    >
      <div
        ref={contentRef}
        style={
          closedHeight
            ? undefined
            : {
                WebkitLineClamp: linesWhenClosed,
                WebkitBoxOrient: 'vertical',
                display: '-webkit-box',
              }
        }
{'fraction_non_alphanumeric': 0.1267418712674187, 'fraction_numerical': 0.0, 'mean_word_length': 3.1132332878581175, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, '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': '5549111', 'n_tokens_mistral': 877, 'n_tokens_neox': 857, 'n_words': 197}