mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 15:15:42 +00:00 
			
		
		
		
	Update notification fetching (#9557)
- Reduce interval to 1 minute - Only fetch for visible tab
This commit is contained in:
		@@ -11,7 +11,7 @@ import {
 | 
				
			|||||||
  Text,
 | 
					  Text,
 | 
				
			||||||
  Tooltip
 | 
					  Tooltip
 | 
				
			||||||
} from '@mantine/core';
 | 
					} from '@mantine/core';
 | 
				
			||||||
import { useDisclosure } from '@mantine/hooks';
 | 
					import { useDisclosure, useDocumentVisibility } from '@mantine/hooks';
 | 
				
			||||||
import { IconBell, IconSearch } from '@tabler/icons-react';
 | 
					import { IconBell, IconSearch } from '@tabler/icons-react';
 | 
				
			||||||
import { useQuery } from '@tanstack/react-query';
 | 
					import { useQuery } from '@tanstack/react-query';
 | 
				
			||||||
import { type ReactNode, useEffect, useMemo, useState } from 'react';
 | 
					import { type ReactNode, useEffect, useMemo, useState } from 'react';
 | 
				
			||||||
@@ -63,10 +63,12 @@ export function Header() {
 | 
				
			|||||||
    return server.customize?.navbar_message;
 | 
					    return server.customize?.navbar_message;
 | 
				
			||||||
  }, [server.customize]);
 | 
					  }, [server.customize]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const visibility = useDocumentVisibility();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Fetch number of notifications for the current user
 | 
					  // Fetch number of notifications for the current user
 | 
				
			||||||
  const notifications = useQuery({
 | 
					  const notifications = useQuery({
 | 
				
			||||||
    queryKey: ['notification-count'],
 | 
					    queryKey: ['notification-count'],
 | 
				
			||||||
    enabled: isLoggedIn(),
 | 
					    enabled: isLoggedIn() && visibility === 'visible',
 | 
				
			||||||
    queryFn: async () => {
 | 
					    queryFn: async () => {
 | 
				
			||||||
      if (!isLoggedIn()) {
 | 
					      if (!isLoggedIn()) {
 | 
				
			||||||
        return null;
 | 
					        return null;
 | 
				
			||||||
@@ -90,7 +92,8 @@ export function Header() {
 | 
				
			|||||||
        return null;
 | 
					        return null;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    refetchInterval: 5 * 60 * 1000,
 | 
					    // Refetch every minute, *if* the tab is visible
 | 
				
			||||||
 | 
					    refetchInterval: 60 * 1000,
 | 
				
			||||||
    refetchOnMount: true
 | 
					    refetchOnMount: true
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
import { ApiEndpoints } from '@lib/enums/ApiEndpoints';
 | 
					import { ApiEndpoints } from '@lib/enums/ApiEndpoints';
 | 
				
			||||||
import { apiUrl } from '@lib/functions/Api';
 | 
					import { apiUrl } from '@lib/functions/Api';
 | 
				
			||||||
import { t } from '@lingui/core/macro';
 | 
					import { t } from '@lingui/core/macro';
 | 
				
			||||||
 | 
					import { useDocumentVisibility } from '@mantine/hooks';
 | 
				
			||||||
import { notifications, showNotification } from '@mantine/notifications';
 | 
					import { notifications, showNotification } from '@mantine/notifications';
 | 
				
			||||||
import { IconCircleCheck } from '@tabler/icons-react';
 | 
					import { IconCircleCheck } from '@tabler/icons-react';
 | 
				
			||||||
import { useQuery } from '@tanstack/react-query';
 | 
					import { useQuery } from '@tanstack/react-query';
 | 
				
			||||||
@@ -21,6 +22,8 @@ export default function useDataOutput({
 | 
				
			|||||||
}) {
 | 
					}) {
 | 
				
			||||||
  const api = useApi();
 | 
					  const api = useApi();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const visibility = useDocumentVisibility();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const [loading, setLoading] = useState<boolean>(false);
 | 
					  const [loading, setLoading] = useState<boolean>(false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  useEffect(() => {
 | 
					  useEffect(() => {
 | 
				
			||||||
@@ -38,7 +41,7 @@ export default function useDataOutput({
 | 
				
			|||||||
  }, [id, title]);
 | 
					  }, [id, title]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const progress = useQuery({
 | 
					  const progress = useQuery({
 | 
				
			||||||
    enabled: !!id && loading,
 | 
					    enabled: !!id && loading && visibility === 'visible',
 | 
				
			||||||
    refetchInterval: 500,
 | 
					    refetchInterval: 500,
 | 
				
			||||||
    queryKey: ['data-output', id, title],
 | 
					    queryKey: ['data-output', id, title],
 | 
				
			||||||
    queryFn: () =>
 | 
					    queryFn: () =>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user