Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Show one loading bar across nested Suspense boundaries (#3495)
Luke Vellaf31a2f27da36
diff --git a/apps/web/src/app/[locale]/layout.tsx b/apps/web/src/app/[locale]/layout.tsxindex 15d073872..42e15143e 100644--- a/apps/web/src/app/[locale]/layout.tsx+++ b/apps/web/src/app/[locale]/layout.tsx@@ -11,6 +11,7 @@ import type React from "react";import { Suspense } from "react";import { PostHogSessionInit } from "@/app/[locale]/components/posthog-init";import type { Params } from "@/app/[locale]/types";+import { RouterLoadingBar } from "@/components/router-loading-indicator";import { SkipNavLink } from "@/components/skip-nav-link";import { BrandingProvider } from "@/features/branding/client";import { getInstanceBrandingConfig } from "@/features/branding/data";@@ -83,6 +84,7 @@ export default async function Root({<InstancePolicyProvider value={instancePolicy}><BrandingProvider value={brandingConfig}><Toaster />+ <RouterLoadingBar /><I18nProvider locale={locale} resources={resources}><TRPCProvider><LazyMotion features={domAnimation}>diff --git a/apps/web/src/components/router-loading-indicator.tsx b/apps/web/src/components/router-loading-indicator.tsxindex f8707cc87..b39271a6c 100644--- a/apps/web/src/components/router-loading-indicator.tsx+++ b/apps/web/src/components/router-loading-indicator.tsx@@ -1,71 +1,116 @@"use client";-import { cn } from "@rallly/ui";import { Progress } from "@rallly/ui/progress";import * as React from "react";-export const RouterLoadingIndicator = () => {- const [isLoading, setIsLoading] = React.useState(false);- const [progress, setProgress] = React.useState(0);- const progressInterval = React.useRef<NodeJS.Timeout | null>(null);- const currentProgress = React.useRef(0);+// Nested Suspense fallbacks resolve one after another during a navigation.+// Each fallback registers here instead of drawing its own bar, so the single+// bar keeps moving until the last boundary resolves.+let pendingCount = 0;+const listeners = new Set<() => void>();- // Track route changes- React.useEffect(() => {- const startLoading = () => {- setIsLoading(true);- setProgress(0);- currentProgress.current = 0;+function setPendingCount(next: number) {+ pendingCount = next;+ for (const listener of listeners) {+ listener();+ }+}- // Use a smaller step for slower progress initially- let step = 0.5;+function subscribe(listener: () => void) {+ listeners.add(listener);+ return () => {+ listeners.delete(listener);+ };+}- progressInterval.current = setInterval(() => {- currentProgress.current += step;+const getSnapshot = () => pendingCount > 0;+const getServerSnapshot = () => false;- const calculatedProgress = Math.round(- (Math.atan(currentProgress.current) / (Math.PI / 2)) * 100,- );+export const RouterLoadingIndicator = () => {+ React.useEffect(() => {+ setPendingCount(pendingCount + 1);+ return () => {+ setPendingCount(pendingCount - 1);+ };+ }, []);- setProgress(calculatedProgress);+ return null;+};- if (calculatedProgress >= 70) {- step = 0.1;- }- }, 100);- };+// Fallbacks can hand off across separate commits, so completion waits this+// long for another fallback to register before finishing the bar.+const HANDOFF_GRACE_MS = 100;+const HIDE_DELAY_MS = 500;- const stopLoading = () => {- setProgress(100);+const toPercent = (value: number) =>+ Math.round((Math.atan(value) / (Math.PI / 2)) * 100);- // Clear the interval- if (progressInterval.current) {- clearInterval(progressInterval.current);- progressInterval.current = null;+export const RouterLoadingBar = () => {+ const isPending = React.useSyncExternalStore(+ subscribe,+ getSnapshot,+ getServerSnapshot,+ );+ const [isVisible, setIsVisible] = React.useState(false);+ const [progress, setProgress] = React.useState(0);+ const currentProgress = React.useRef(0);+ const finishTimeout = React.useRef<NodeJS.Timeout | null>(null);+ const hideTimeout = React.useRef<NodeJS.Timeout | null>(null);++ React.useEffect(() => {+ if (!isPending) {+ return;+ }++ if (finishTimeout.current) {+ // A fallback handed off to the next one; keep the bar's position+ clearTimeout(finishTimeout.current);+ finishTimeout.current = null;+ } else {+ if (hideTimeout.current) {+ clearTimeout(hideTimeout.current);+ hideTimeout.current = null;}+ currentProgress.current = 0;+ setProgress(0);+ setIsVisible(true);+ }- // Reset after animation completes- setTimeout(() => {- setIsLoading(false);- setProgress(0);- }, 500);- };+ const interval = setInterval(() => {+ // Use a smaller step for slower progress after 70%+ currentProgress.current +=+ toPercent(currentProgress.current) >= 70 ? 0.1 : 0.5;+ setProgress(toPercent(currentProgress.current));+ }, 100);- startLoading();+ return () => {+ clearInterval(interval);+ finishTimeout.current = setTimeout(() => {+ finishTimeout.current = null;+ setProgress(100);+ hideTimeout.current = setTimeout(() => {+ hideTimeout.current = null;+ setIsVisible(false);+ setProgress(0);+ }, HIDE_DELAY_MS);+ }, HANDOFF_GRACE_MS);+ };+ }, [isPending]);- // When route changes complete, stop loading+ React.useEffect(() => {return () => {- stopLoading();+ if (finishTimeout.current) clearTimeout(finishTimeout.current);+ if (hideTimeout.current) clearTimeout(hideTimeout.current);};}, []);+ if (!isVisible) {+ return null;+ }++ // Decorative: pages announce loading through their own skeletonsreturn (- <div- className={cn(- "fixed top-0 right-0 left-0 z-50 w-full",- isLoading ? "opacity-100" : "opacity-0",- )}- >+ <div aria-hidden className="fixed top-0 right-0 left-0 z-50 w-full"><Progress value={progress} className="h-1 rounded-none" /></div>);