<commit_before>/**
 * @license Copyright 2019 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 {h} from 'preact';
import clsx from 'clsx';
import {useState} from 'preact/hooks';
import './lhr-viewer-link.css';

/** @typedef {LH.Result|(() => Promise<LH.Result>)} LHResolver */

/** @param {LH.Result} lhr */
export function openLhrInClassicViewer(lhr) {
  const VIEWER_ORIGIN = 'https://googlechrome.github.io';
  // Chrome doesn't allow us to immediately postMessage to a popup right
  // after it's created. Normally, we could also listen for the popup window's
  // load event, however it is cross-domain and won't fire. Instead, listen
  // for a message from the target app saying "I'm open".
  window.addEventListener('message', function msgHandler(messageEvent) {
    if (messageEvent.origin !== VIEWER_ORIGIN) {
      return;
    }
    if (popup && messageEvent.data.opened) {
      popup.postMessage({lhresults: lhr}, VIEWER_ORIGIN);
      window.removeEventListener('message', msgHandler);
    }
  });

  // The popup's window.name is keyed by version+url+fetchTime, so we reuse/select tabs correctly
  const fetchTime = lhr.fetchTime;
  const windowName = `${lhr.lighthouseVersion}-${lhr.requestedUrl}-${fetchTime}`;
  const popup = window.open(`${VIEWER_ORIGIN}/lighthouse/viewer`, windowName);
}

/** @param {{children: string|JSX.Element|JSX.Element[], lhr: LHResolver, className?: string}} props */
export const LhrViewerLink = props => {
  const {children, lhr: lhrOrResolver} = props;
  const [isLoading, setIsLoading] = useState(false);

  return (
    <span
      className={clsx('lhr-viewer-link', props.className, {
        'lhr-viewer-link--loading': isLoading,
      })}
      onClick={async evt => {
        evt.preventDefault();
        evt.stopImmediatePropagation();
        if (isLoading) return;

        if (typeof lhrOrResolver === 'object') return openLhrInClassicViewer(lhrOrResolver);
<commit_msg>chore: add comment for viewer's `opened` postMessage (#425)<commit_after>/**
 * @license Copyright 2019 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 {h} from 'preact';
import clsx from 'clsx';
import {useState} from 'preact/hooks';
import './lhr-viewer-link.css';

/** @typedef {LH.Result|(() => Promise<LH.Result>)} LHResolver */

/** @param {LH.Result} lhr */
export function openLhrInClassicViewer(lhr) {
  const VIEWER_ORIGIN = 'https://googlechrome.github.io';
  // Chrome doesn't allow us to immediately postMessage to a popup right
  // after it's created. Normally, we could also listen for the popup window's
  // load event, however it is cross-domain and won't fire. Instead, listen
  // for a message from the target app saying "I'm open".
  window.addEventListener('message', function msgHandler(messageEvent) {
    if (messageEvent.origin !== VIEWER_ORIGIN) {
      return;
    }
    // https://github.com/GoogleChrome/lighthouse/blob/35f1a6f90e9443bdf42179a4c9823e6d4848a9e6/lighthouse-viewer/app/src/lighthouse-report-viewer.js#L415-L418
    if (popup && messageEvent.data.opened) {
      popup.postMessage({lhresults: lhr}, VIEWER_ORIGIN);
      window.removeEventListener('message', msgHandler);
    }
  });

  // The popup's window.name is keyed by version+url+fetchTime, so we reuse/select tabs correctly
  const fetchTime = lhr.fetchTime;
  const windowName = `${lhr.lighthouseVersion}-${lhr.requestedUrl}-${fetchTime}`;
  const popup = window.open(`${VIEWER_ORIGIN}/lighthouse/viewer`, windowName);
}

/** @param {{children: string|JSX.Element|JSX.Element[], lhr: LHResolver, className?: string}} props */
export const LhrViewerLink = props => {
  const {children, lhr: lhrOrResolver} = props;
  const [isLoading, setIsLoading] = useState(false);

  return (
    <span
      className={clsx('lhr-viewer-link', props.className, {
        'lhr-viewer-link--loading': isLoading,
      })}
      onClick={async evt => {
        evt.preventDefault();
        evt.stopImmediatePropagation();
        if (isLoading) return;

        if (typeof lhrOrResolver === 'object') return openLhrInClassicViewer(lhrOrResolver);

{'fraction_non_alphanumeric': 0.12118245888801879, 'fraction_numerical': 0.010180109631949883, 'mean_word_length': 4.078528827037774, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 3, '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': '4818995', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1504, 'n_words': 478}