import { Fragment, useEffect, useState } from "react"; import { useNavigation } from "react-router"; import { useSpinDelay } from "spin-delay"; import { Transition } from "@headlessui/react"; import { useTranslation } from "react-i18next"; // Subframe migration (strangler shim): only the spinner glyph swaps to the Subframe // design-system Loader. The floating panel, spin-delay gating, navigation-state word // cycling, transitions and path display are all preserved unchanged. import { Loader } from "~/ui/components/Loader"; let firstRender = false; interface Props { loading?: boolean; } export default function FloatingLoader({ loading }: Props) { const { t } = useTranslation(); const navigation = useNavigation(); const [, setWords] = useState>([]); const [pendingPath, setPendingPath] = useState(""); const showLoader = useSpinDelay(Boolean(navigation.state !== "idle" || loading), { delay: 400, minDuration: 1000, }); useEffect(() => { if (navigation.location?.pathname === "/") return; if (navigation.state === "idle") return; if (navigation.state === "loading") setWords(LOADER_WORDS); if (navigation.state === "submitting") setWords(ACTION_WORDS); const interval = setInterval(() => { setWords(([first, ...rest]) => [...rest, first] as Array); }, 2000); return () => clearInterval(interval); // eslint-disable-next-line react-hooks/exhaustive-deps }, [pendingPath, navigation.state]); useEffect(() => { if (firstRender) return; if (navigation.state === "idle") return; setPendingPath(navigation.location.pathname); }, [navigation]); useEffect(() => { firstRender = false; }, []); const action = navigation.state === "loading" ? t("shared.loading") : t("shared.processing"); return (
{/* Notification panel, dynamically insert this into the live region when it needs to be displayed */}
{action}...
{navigation.location?.pathname && (
{t("shared.path")}: {navigation.location?.pathname}
)}
); } const LOADER_WORDS = ["loading", "checking cdn", "checking cache", "fetching from db", "compiling mdx", "updating cache", "transfer"]; const ACTION_WORDS = ["packaging", "zapping", "validating", "processing", "calculating", "computing", "computering"];