import { PageSkeleton, type SkeletonKind } from "@refs/design-system"; import { useEffect, useState, type ReactNode } from "react"; import { useLocation, useNavigation } from "react-router"; /** After this long a waiting page dims, so a fast navigation shows no loading state at all. */ const DIM_AFTER = 150; /** After this long the dimmed page gives way to the shape of the page to come. */ const SKELETON_AFTER = 450; export function under(path: string, base: string): boolean { return path === base || path.startsWith(`${base}/`); } interface Pending { /** The path the reader asked for. */ readonly to: string; /** The same page with other search parameters, such as a list filter. */ readonly samePage: boolean; } /** * The page navigation that is loading, or null. A submitted form is not one: its button shows the work, and the * page under it must stay. */ export function usePending(): Pending | null { const navigation = useNavigation(); const { pathname } = useLocation(); if (navigation.state !== "loading" || navigation.formMethod) return null; return { to: navigation.location.pathname, samePage: navigation.location.pathname === pathname }; } /** The path to mark as current: the one the reader just asked for, so the control answers at once. */ export function useIntendedPath(): string { const pending = usePending(); const { pathname } = useLocation(); return pending?.to ?? pathname; } function useStage(waiting: boolean): 0 | 1 | 2 { const [stage, setStage] = useState<0 | 1 | 2>(0); useEffect(() => { if (!waiting) return setStage(0); const dim = setTimeout(() => setStage(1), DIM_AFTER); const swap = setTimeout(() => setStage(2), SKELETON_AFTER); return () => { clearTimeout(dim); clearTimeout(swap); }; }, [waiting]); return waiting ? stage : 0; } /** The shape of the page at `path`, read from the address alone. */ export function skeletonKind(path: string): SkeletonKind { const [first, second, section, item] = path.split("/").filter(Boolean); if (first === undefined) return "overview"; if (first === "tasks") return second === "new" ? "form" : "list"; if (first === "admin") return "list"; if (first === "settings" || first === "new" || first === "organizations") return "form"; if (second === undefined) return "overview"; if (section === "tasks" || section === "pulls") return item ? "detail" : "list"; if (section === "settings" || section === "fork" || section === "compare") return "form"; return "code"; } /** * The part of a page that a navigation replaces. While the next page loads, the current content stays, then * dims, then gives way to a shimmer in the shape of the page to come. * * `covers` says whether this part is the one a navigation to `to` replaces. A layout that survives the * navigation leaves the work to the part inside it. */ export function PendingView({ covers, top = false, kind, children }: { covers: (to: string) => boolean; top?: boolean; kind?: SkeletonKind; children: ReactNode }) { const pending = usePending(); const mine = pending !== null && (pending.samePage ? top : covers(pending.to)); const stage = useStage(mine); const skeleton = stage === 2 && pending !== null && !pending.samePage; return ( <> {skeleton && } ); }