86 lines · 3.5 KBRaw
| 1 | import { PageSkeleton, type SkeletonKind } from "@refs/design-system"; |
| 2 | import { useEffect, useState, type ReactNode } from "react"; |
| 3 | import { useLocation, useNavigation } from "react-router"; |
| 4 | |
| 5 | /** After this long a waiting page dims, so a fast navigation shows no loading state at all. */ |
| 6 | const DIM_AFTER = 150; |
| 7 | /** After this long the dimmed page gives way to the shape of the page to come. */ |
| 8 | const SKELETON_AFTER = 450; |
| 9 | |
| 10 | export function under(path: string, base: string): boolean { |
| 11 | return path === base || path.startsWith(`${base}/`); |
| 12 | } |
| 13 | |
| 14 | interface Pending { |
| 15 | /** The path the reader asked for. */ |
| 16 | readonly to: string; |
| 17 | /** The same page with other search parameters, such as a list filter. */ |
| 18 | readonly samePage: boolean; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * The page navigation that is loading, or null. A submitted form is not one: its button shows the work, and the |
| 23 | * page under it must stay. |
| 24 | */ |
| 25 | export function usePending(): Pending | null { |
| 26 | const navigation = useNavigation(); |
| 27 | const { pathname } = useLocation(); |
| 28 | if (navigation.state !== "loading" || navigation.formMethod) return null; |
| 29 | return { to: navigation.location.pathname, samePage: navigation.location.pathname === pathname }; |
| 30 | } |
| 31 | |
| 32 | /** The path to mark as current: the one the reader just asked for, so the control answers at once. */ |
| 33 | export function useIntendedPath(): string { |
| 34 | const pending = usePending(); |
| 35 | const { pathname } = useLocation(); |
| 36 | return pending?.to ?? pathname; |
| 37 | } |
| 38 | |
| 39 | function useStage(waiting: boolean): 0 | 1 | 2 { |
| 40 | const [stage, setStage] = useState<0 | 1 | 2>(0); |
| 41 | useEffect(() => { |
| 42 | if (!waiting) return setStage(0); |
| 43 | const dim = setTimeout(() => setStage(1), DIM_AFTER); |
| 44 | const swap = setTimeout(() => setStage(2), SKELETON_AFTER); |
| 45 | return () => { |
| 46 | clearTimeout(dim); |
| 47 | clearTimeout(swap); |
| 48 | }; |
| 49 | }, [waiting]); |
| 50 | return waiting ? stage : 0; |
| 51 | } |
| 52 | |
| 53 | /** The shape of the page at `path`, read from the address alone. */ |
| 54 | export function skeletonKind(path: string): SkeletonKind { |
| 55 | const [first, second, section, item] = path.split("/").filter(Boolean); |
| 56 | if (first === undefined) return "overview"; |
| 57 | if (first === "tasks") return second === "new" ? "form" : "list"; |
| 58 | if (first === "admin") return "list"; |
| 59 | if (first === "settings" || first === "new" || first === "organizations") return "form"; |
| 60 | if (second === undefined) return "overview"; |
| 61 | if (section === "tasks" || section === "pulls") return item ? "detail" : "list"; |
| 62 | if (section === "settings" || section === "fork" || section === "compare") return "form"; |
| 63 | return "code"; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * The part of a page that a navigation replaces. While the next page loads, the current content stays, then |
| 68 | * dims, then gives way to a shimmer in the shape of the page to come. |
| 69 | * |
| 70 | * `covers` says whether this part is the one a navigation to `to` replaces. A layout that survives the |
| 71 | * navigation leaves the work to the part inside it. |
| 72 | */ |
| 73 | export function PendingView({ covers, top = false, kind, children }: { covers: (to: string) => boolean; top?: boolean; kind?: SkeletonKind; children: ReactNode }) { |
| 74 | const pending = usePending(); |
| 75 | const mine = pending !== null && (pending.samePage ? top : covers(pending.to)); |
| 76 | const stage = useStage(mine); |
| 77 | const skeleton = stage === 2 && pending !== null && !pending.samePage; |
| 78 | return ( |
| 79 | <> |
| 80 | <div hidden={skeleton} aria-busy={mine || undefined} className={`transition-opacity duration-200 ${stage === 1 ? "opacity-55" : ""}`}> |
| 81 | {children} |
| 82 | </div> |
| 83 | {skeleton && <PageSkeleton kind={kind ?? skeletonKind(pending.to)} title={top} />} |
| 84 | </> |
| 85 | ); |
| 86 | } |