import { focusManager } from "@tanstack/react-query";
import { useSyncExternalStore } from "react";

export function useIsWindowInactive() {
	return useSyncExternalStore(
		(onChange) => {
			const unsubscribe = focusManager.subscribe(onChange);
			return unsubscribe;
		},
		() => !focusManager.isFocused(),
		// getServerSnapshot: Required for SSR compatibility
		// On the server, we assume the window is active (focused)
		() => false,
	);
}
