diff --git a/apps/dashboard/src/app/(app)/[organizationSlug]/[projectId]/realtime/realtime-reloader.tsx b/apps/dashboard/src/app/(app)/[organizationSlug]/[projectId]/realtime/realtime-reloader.tsx index 5a989666..27dbca2b 100644 --- a/apps/dashboard/src/app/(app)/[organizationSlug]/[projectId]/realtime/realtime-reloader.tsx +++ b/apps/dashboard/src/app/(app)/[organizationSlug]/[projectId]/realtime/realtime-reloader.tsx @@ -15,10 +15,12 @@ const RealtimeReloader = ({ projectId }: Props) => { useWS( `/live/events/${projectId}`, () => { - router.refresh(); - client.refetchQueries({ - type: 'active', - }); + if (!document.hidden) { + router.refresh(); + client.refetchQueries({ + type: 'active', + }); + } }, { debounce: { diff --git a/apps/dashboard/src/components/overview/live-counter/live-counter.tsx b/apps/dashboard/src/components/overview/live-counter/live-counter.tsx index dc4908d3..53540dbb 100644 --- a/apps/dashboard/src/components/overview/live-counter/live-counter.tsx +++ b/apps/dashboard/src/components/overview/live-counter/live-counter.tsx @@ -7,6 +7,7 @@ import { TooltipContent, TooltipTrigger, } from '@/components/ui/tooltip'; +import { useDebounceVal } from '@/hooks/useDebounceVal'; import useWS from '@/hooks/useWS'; import { cn } from '@/utils/cn'; import { useQueryClient } from '@tanstack/react-query'; @@ -23,40 +24,46 @@ const AnimatedNumbers = dynamic(() => import('react-animated-numbers'), { loading: () =>
0
, }); -const FIFTEEN_SECONDS = 1000 * 15; +const FIFTEEN_SECONDS = 1000 * 30; export default function LiveCounter({ data = 0, projectId }: LiveCounterProps) { const client = useQueryClient(); - const [counter, setCounter] = useState(data); + const counter = useDebounceVal(data, 1000, { + maxWait: 5000, + }); const lastRefresh = useRef(Date.now()); useWS(`/live/visitors/${projectId}`, (value) => { if (!isNaN(value)) { - setCounter(value); + counter.set(value); if (Date.now() - lastRefresh.current > FIFTEEN_SECONDS) { lastRefresh.current = Date.now(); - toast('Refreshed data'); - client.refetchQueries({ - type: 'active', - }); + if (!document.hidden) { + toast('Refreshed data'); + client.refetchQueries({ + type: 'active', + }); + } } } }); return ( - +
@@ -69,7 +76,7 @@ export default function LiveCounter({ data = 0, projectId }: LiveCounterProps) { damping: 10, stiffness: 200, })} - animateToNumber={counter} + animateToNumber={counter.debounced} locale="en" />