<commit_before>/* eslint-disable react/destructuring-assignment */
import React from 'react';
import { LinkProps } from 'next/link';
import Router from 'next/router';
import { format, resolve, parse, UrlObject } from 'url';

interface Props extends LinkProps {
  withData?: boolean;
}

export const prefetch = async (href: string | UrlObject) => {
  // if  we're running server side do nothing
  if (typeof window === 'undefined') return;

  const url = typeof href !== 'string' ? format(href) : href;

  const { pathname } = window.location;

  const parsedHref = resolve(pathname, url);

  const { query } = typeof href !== 'string' ? href : parse(url, true);

  const Component = await Router.prefetch(parsedHref);

  // if Component exists and has getInitialProps
  // fetch the component props (the component should save it in cache)
  // @ts-ignore
  if (Component && Component.getInitialProps) {
    const ctx = { pathname: href, query, isVirtualCall: true };
    // @ts-ignore
    await Component.getInitialProps(ctx);
  }
};

// extend default next/link to customize the prefetch behaviour
export default class LinkWithData extends React.Component<Props> {
  // our custom prefetch method
  async prefetch() {
    // if the prefetch prop is not defined do nothing
    if (!this.props.prefetch) return;

    // if withData prop is defined
    // prefetch with data
    // otherwise just prefetch the page
    if (this.props.withData) {
      prefetch(this.props.href);
    } else {
      // @ts-ignore
      super.prefetch();
    }
  }
}<commit_msg>get LinkWithData to work with custom props, fix rendering<commit_after>import Link, { LinkProps } from 'next/link';
import Router from 'next/router';
import { format, resolve, parse, UrlObject } from 'url';

interface Props extends LinkProps {
  withData?: boolean;
}

const prefetchPage = async (href: string | UrlObject) => {
  // if  we're running server side do nothing
  if (typeof window === 'undefined') return;

  const url = typeof href !== 'string' ? format(href) : href;

  const { pathname } = window.location;

  const parsedHref = resolve(pathname, url);

  const { query } = typeof href !== 'string' ? href : parse(url, true);

  const Component = await Router.prefetch(parsedHref);

  // if Component exists and has getInitialProps
  // fetch the component props (the component should save it in cache)
  // @ts-ignore
  if (Component && Component.getInitialProps) {
    const ctx = { pathname: href, query, isVirtualCall: true };
    // @ts-ignore
    await Component.getInitialProps(ctx);
  }
};

// extend default next/link to customize the prefetch behaviour
class LinkWithData extends Link {
  // our custom prefetch method
  async prefetch() {
    const { withData, prefetch, href }: Props = this.props;
    // if the prefetch prop is not defined do nothing
    if (!prefetch) return;
    // if withData prop is defined
    // prefetch with data
    // otherwise just prefetch the page
    if (withData) {
      prefetchPage(href);
    } else {
      // @ts-ignore
      super.prefetch();
    }
  }
}

export { prefetchPage as prefetch };
export default LinkWithData;
{'fraction_non_alphanumeric': 0.11220597584233948, 'fraction_numerical': 0.0, 'mean_word_length': 3.3109589041095893, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '635311', 'n_tokens_mistral': 921, 'n_tokens_neox': 905, 'n_words': 322}