SurfSense/surfsense_web/hooks/use-global-loading.ts

96 lines
2.9 KiB
TypeScript
Raw Normal View History

"use client";
import { useAtom } from "jotai";
import { useCallback, useEffect, useRef } from "react";
import { globalLoadingAtom } from "@/atoms/ui/loading.atoms";
// Global counter to generate unique IDs for each loading request
let loadingIdCounter = 0;
// Track the current active loading ID globally
let currentLoadingId: number | null = null;
// Pending hide timeout - allows new loading requests to take over before hiding
let pendingHideTimeout: ReturnType<typeof setTimeout> | null = null;
/**
* Hook to control the global loading screen.
* The spinner is always mounted in the DOM to prevent animation reset.
*/
export function useGlobalLoading() {
const [loading, setLoading] = useAtom(globalLoadingAtom);
2026-01-27 15:28:30 +05:30
const show = useCallback(() => {
// Cancel any pending hide - new loading request takes over
if (pendingHideTimeout) {
clearTimeout(pendingHideTimeout);
pendingHideTimeout = null;
}
2026-01-27 15:28:30 +05:30
const id = ++loadingIdCounter;
currentLoadingId = id;
setLoading({ isLoading: true });
return id;
}, [setLoading]);
const hide = useCallback(
(id?: number) => {
// Only hide if this is the current loading, or if no ID provided (force hide)
if (id === undefined || id === currentLoadingId) {
// Use a small delay to allow React to flush pending mounts
// This prevents flash when transitioning between loading states
if (pendingHideTimeout) {
clearTimeout(pendingHideTimeout);
}
pendingHideTimeout = setTimeout(() => {
// Double-check we're still the current loading after the delay
if (id === undefined || id === currentLoadingId) {
currentLoadingId = null;
2026-01-27 15:28:30 +05:30
setLoading({ isLoading: false });
}
pendingHideTimeout = null;
}, 50); // Small delay to allow next component to mount and show loading
}
},
[setLoading]
);
return { show, hide, isLoading: loading.isLoading };
}
/**
* Hook that automatically shows/hides the global loading screen based on a condition.
* Useful for components that show loading on mount and hide on unmount.
*
* Uses ownership tracking to prevent flashes when multiple components
* transition loading states (e.g., layout page).
*
* @param shouldShow - Whether the loading screen should be visible
*/
2026-01-27 15:28:30 +05:30
export function useGlobalLoadingEffect(shouldShow: boolean) {
const { show, hide } = useGlobalLoading();
const loadingIdRef = useRef<number | null>(null);
useEffect(() => {
if (shouldShow) {
// Show loading and store the ID
2026-01-27 15:28:30 +05:30
loadingIdRef.current = show();
} else if (loadingIdRef.current !== null) {
// Only hide if we were the ones showing loading
hide(loadingIdRef.current);
loadingIdRef.current = null;
}
2026-01-27 15:28:30 +05:30
}, [shouldShow, show, hide]);
// Cleanup on unmount - only hide if we're still the active loading
useEffect(() => {
return () => {
if (loadingIdRef.current !== null) {
hide(loadingIdRef.current);
loadingIdRef.current = null;
}
};
}, [hide]);
}