<commit_before>/**
 * @license Copyright 2020 Google Inc. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 */

import * as _ from '@lhci/utils/src/lodash.js';
import {findRootSvg} from '../../../components/d3-graph.jsx';
import {HOVER_CARD_MARGIN, HOVER_CARD_WIDTH} from './hover-card.jsx';

/** @typedef {import('../project-category-summaries.jsx').StatisticWithBuild} StatisticWithBuild */

/** @param {Pick<StatisticWithBuild, 'value'>} statistic */
export function getClassNameFromStatistic(statistic) {
  if (statistic.value >= 0.9) return 'score--pass';
  if (statistic.value < 0.5) return 'score--fail';
  return 'score--average';
}

/** @param {Array<StatisticWithBuild>} statistics */
export function computeStatisticRerenderKey(statistics) {
  return statistics
    .map(s => s.id)
    .sort()
    .join(',');
}

/**
 * @param {HTMLElement} rootEl
 * @param {{top: number, left: number, right: number, bottom: number}} margin
 * @param {Array<StatisticWithBuild>} statistics
 * @param {(n: number) => number} xScale
 * @param {import('preact/hooks/src').StateUpdater<string|undefined>} setSelectedBuildId
 * @param {import('preact/hooks/src').StateUpdater<boolean>} setPinned
 */
export function appendHoverCardHitboxElements(
  rootEl,
  margin,
  statistics,
  xScale,
  setSelectedBuildId,
  setPinned
) {
  const {svg, graphWidth, graphHeight} = findRootSvg(rootEl, margin);
  const graphRootEl = rootEl.closest('.graph-root-el');
  if (!(graphRootEl instanceof HTMLElement)) throw new Error('Missing graph-root-el');

  const n = statistics.length - 1;
  const debouncedClearBuildId = _.debounce(() => setSelectedBuildId(undefined), 250);

  // The click and mouseover hitboxes
  svg
    .selectAll('.graph-hitbox')
    .data(statistics)
    .enter()
    .append('rect')
    .attr('class', 'graph-hitbox')
    .attr('x', (d, i) => xScale(i) - graphWidth / n / 2)
    .attr('y', 0)
    .attr('width', graphWidth / n)
    .attr('height', graphHeight)
    .on('click', () => {
      setPinned(current => {
        const next = !current;
        if (!next) debouncedClearBuildId();
        return next;
      });
    })
    .on('mouseover', (d, i) => {
      debouncedClearBuildId.cancel();
      if (graphRootEl.querySelector('.hover-card--pinned')) return;
      // Set the selected build
      setSelectedBuildId(d.buildId);
      updateGraphHoverElements(rootEl, graphWidth, margin.left, margin.right, xScale, i);
    })
    .on('mouseout', () => {
      if (graphRootEl.querySelector('.hover-card--pinned')) return;
      debouncedClearBuildId();
    });
}

/**
 * @param {HTMLElement} rootEl
 * @param {number} graphWidth
 * @param {number} graphLeftMargin
 * @param {number} graphRightMargin
 * @param {(n: number) => number} xScale
 * @param {number} selectedIndex
 */
export function updateGraphHoverElements(
  rootEl,
  graphWidth,
  graphLeftMargin,<commit_msg>fix(server): do not pin an undefined buildId<commit_after>/**
 * @license Copyright 2020 Google Inc. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 */

import * as _ from '@lhci/utils/src/lodash.js';
import {findRootSvg} from '../../../components/d3-graph.jsx';
import {HOVER_CARD_MARGIN, HOVER_CARD_WIDTH} from './hover-card.jsx';

/** @typedef {import('../project-category-summaries.jsx').StatisticWithBuild} StatisticWithBuild */

/** @param {Pick<StatisticWithBuild, 'value'>} statistic */
export function getClassNameFromStatistic(statistic) {
  if (statistic.value >= 0.9) return 'score--pass';
  if (statistic.value < 0.5) return 'score--fail';
  return 'score--average';
}

/** @param {Array<StatisticWithBuild>} statistics */
export function computeStatisticRerenderKey(statistics) {
  return statistics
    .map(s => s.id)
    .sort()
    .join(',');
}

/**
 * @param {HTMLElement} rootEl
 * @param {{top: number, left: number, right: number, bottom: number}} margin
 * @param {Array<StatisticWithBuild>} statistics
 * @param {(n: number) => number} xScale
 * @param {import('preact/hooks/src').StateUpdater<string|undefined>} setSelectedBuildId
 * @param {import('preact/hooks/src').StateUpdater<boolean>} setPinned
 */
export function appendHoverCardHitboxElements(
  rootEl,
  margin,
  statistics,
  xScale,
  setSelectedBuildId,
  setPinned
) {
  const {svg, graphWidth, graphHeight} = findRootSvg(rootEl, margin);
  const categoryCardEl = rootEl.closest('.category-card');
  const graphRootEl = rootEl.closest('.graph-root-el');
  if (!(categoryCardEl instanceof HTMLElement)) throw new Error('Missing category-card');
  if (!(graphRootEl instanceof HTMLElement)) throw new Error('Missing graph-root-el');

  const n = statistics.length - 1;
  const debouncedClearBuildId = _.debounce(() => setSelectedBuildId(undefined), 250);

  // The click and mouseover hitboxes
  svg
    .selectAll('.graph-hitbox')
    .data(statistics)
    .enter()
    .append('rect')
    .attr('class', 'graph-hitbox')
    .attr('x', (d, i) => xScale(i) - graphWidth / n / 2)
    .attr('y', 0)
    .attr('width', graphWidth / n)
    .attr('height', graphHeight)
    .on('click', () => {
      if (!categoryCardEl.querySelector('.hover-card--visible')) return;

      setPinned(current => {
        const next = !current;
        if (!next) debouncedClearBuildId();
        return next;
      });
    })
    .on('mouseover', (d, i) => {
      debouncedClearBuildId.cancel();
      if (graphRootEl.querySelector('.hover-card--pinned')) return;
      // Set the selected build
      setSelectedBuildId(d.buildId);
      updateGraphHoverElements(rootEl, graphWidth, margin.left, margin.right, xScale, i);
    })
    .on('mouseout', () => {
      if (graphRootEl.querySelector('.hover-card--pinned')) return;
      debouncedClearBuildId();
    });
}

/**
 * @param {HTMLElement} rootEl
 * @param {number} graphWidth
 * @param {number} graphLeftMargin
 * @param {number} graphRightMargin
 * @param {(n: number) => number} xScale
 * @param {number} selectedIndex
 */
export function updateGraphHoverElements(
  rootEl,
  graphWidth,
  graphLeftMargin,
{'fraction_non_alphanumeric': 0.14481185860889395, 'fraction_numerical': 0.005416191562143672, 'mean_word_length': 3.983664772727273, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3875868', 'n_tokens_mistral': 2174, 'n_tokens_neox': 2150, 'n_words': 581}