<commit_before>import { useEffect, useState } from "react";
import useSWR from "swr";

const pinger = async (url) => {
  try {
    const res = await fetch(url, { method: "HEAD", credentials: "omit" });
    return res.ok;
  } catch (e) {
    return false;
  }
};

export const useIsOffline = () => {
  const { data: isOnline, isValidating } = useSWR("/api/ping/", pinger, {
    refreshInterval: 10000,
    dedupingInterval: 10000,
    focusThrottleInterval: 10000,
    refreshWhenOffline: true,
    shouldRetryOnError: false,
    revalidateIfStale: false,
  });

  const [isUnloading, setIsUnloading] = useState(false);
  useEffect(() => {
    let handleBeforeUnload = () => setIsUnloading(true);
    window.addEventListener("beforeunload", handleBeforeUnload);
    return () => {
      window.removeEventListener("beforeunload", handleBeforeUnload);
    };
  });

  return !isUnloading && typeof isOnline === "boolean" ? !isOnline : null;<commit_msg>fix: useIsOffline forced re-rendering<commit_after>import { useEffect, useState } from "react";
import useSWR from "swr";

const pinger = async (url) => {
  try {
    const res = await fetch(url, { method: "HEAD", credentials: "omit" });
    return res.ok;
  } catch (e) {
    return false;
  }
};

const pingerConfig = {
  refreshInterval: 10000,
  dedupingInterval: 10000,
  focusThrottleInterval: 10000,
  refreshWhenOffline: true,
  shouldRetryOnError: false,
  revalidateIfStale: false,
};

export const useIsOffline = () => {
  const { data: isOnline, isValidating } = useSWR(
    "/api/ping/",
    pinger,
    pingerConfig
  );

  const [isUnloading, setIsUnloading] = useState(false);

  useEffect(() => {
    let handleBeforeUnload = () => setIsUnloading(true);
    window.addEventListener("beforeunload", handleBeforeUnload);
    return () => {
      window.removeEventListener("beforeunload", handleBeforeUnload);
    };
  }, []);

  return !isUnloading && typeof isOnline === "boolean" ? !isOnline : null;
{'fraction_non_alphanumeric': 0.14082358922216573, 'fraction_numerical': 0.015251652262328419, 'mean_word_length': 3.9696969696969697, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2633722', 'n_tokens_mistral': 665, 'n_tokens_neox': 628, 'n_words': 144}