2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 11:36:44 +00:00

Update notification fetching (#9557)

- Reduce interval to 1 minute
- Only fetch for visible tab
This commit is contained in:
Oliver 2025-04-22 09:52:50 +10:00 committed by GitHub
parent 40ded29b35
commit 89b3f91ded
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 4 deletions

View File

@ -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
}); });

View File

@ -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: () =>