<commit_before>import throttle from 'common/throttle';

let timeout;
let booted = false;

export function start(container: HTMLElement) {

  /* Detected browsers with a correct grid min-content implementation:
   * Chrome, Chromium, Brave, Opera, Safari 12+
   */
  if (window.chrome) return;

  const runHacks = throttle(200, () => {
    window.lichess.raf(() => {
      fixChat(container);
      fixBoardHeight(container);
      schedule();
    });
  });

  function schedule() {
    timeout && clearTimeout(timeout);
    timeout = setTimeout(runHacks, 500);
  }

  runHacks();

  if (!booted) {
    booted = true;
    document.body.addEventListener('chessground.resize', runHacks);
    window.lichess.pubsub.on('analyse.grid-hack', runHacks);
  }
}

function fixChat(container: HTMLElement) {
  const chat = container.querySelector('.mchat') as HTMLElement,
    board = container.querySelector('.analyse__board') as HTMLElement,
    side = container.querySelector('.analyse__side') as HTMLElement;
  if (chat && board && side) {
    const height = board.offsetHeight - side.offsetHeight;
    if (height) chat.style.height = `calc(${height}px - 2vmin)`;
  }
}

// Firefox 60- needs this to properly compute the grid layout.
export function fixBoardHeight(container: HTMLElement) {
  const el = container.querySelector('.main-board') as HTMLElement;
  el.style.height = el.offsetWidth + 'px';
  window.lichess.dispatchEvent(document.body, 'chessground.resize');
}<commit_msg>only apply grid hack when necessary<commit_after>import throttle from 'common/throttle';

let timeout: number | undefined;
let booted = false;
let lastSet: number | undefined;

export function start(container: HTMLElement) {

  /* Detected browsers with a correct grid min-content implementation:
   * Chrome, Chromium, Brave, Opera, Safari 12+
   */
  if (window.chrome) return;

  const runHacks = throttle(100, () => {
    window.lichess.raf(() => {
      fixChat(container);
      fixBoardHeight(container);
      schedule();
    });
  });

  function schedule() {
    timeout && clearTimeout(timeout);
    timeout = setTimeout(runHacks, 500);
  }

  runHacks();

  if (!booted) {
    booted = true;
    document.body.addEventListener('chessground.resize', runHacks);
    window.lichess.pubsub.on('analyse.grid-hack', runHacks);
  }
}

function fixChat(container: HTMLElement) {
  const chat = container.querySelector('.mchat') as HTMLElement,
    board = container.querySelector('.analyse__board') as HTMLElement,
    side = container.querySelector('.analyse__side') as HTMLElement;
  if (chat && board && side) {
    const height = board.offsetHeight - side.offsetHeight;
    if (height) chat.style.height = `calc(${height}px - 2vmin)`;
  }
}

// Firefox 60- needs this to properly compute the grid layout.
function fixBoardHeight(container: HTMLElement) {
  const el = container.querySelector('.main-board') as HTMLElement,
  width = el.offsetWidth;
  if (lastSet != width) {
    lastSet = width;
    el.style.height = width + 'px';
    window.lichess.dispatchEvent(document.body, 'chessground.resize');
  }
}
{'fraction_non_alphanumeric': 0.13292367399741267, 'fraction_numerical': 0.0071151358344113845, 'mean_word_length': 3.9488, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2332971', 'n_tokens_mistral': 978, 'n_tokens_neox': 954, 'n_words': 229}