|
1 | 1 | import { keepPreviousData, useQuery } from '@tanstack/react-query';
|
| 2 | +import { useWatch } from 'react-hook-form'; |
2 | 3 | import { fetchLogs } from '../queries/logs';
|
3 | 4 | import { useAuthProperties } from './useAuthProperties';
|
| 5 | +import { useLiveRefresh } from './useLiveRefresh'; |
4 | 6 | import { useSearchForm } from './useSearchForm';
|
5 |
| -import { useWatch } from 'react-hook-form'; |
6 | 7 |
|
7 | 8 | const useQueryLogs = () => {
|
8 | 9 | const { fetchInfo, isHeaderReady } = useAuthProperties();
|
9 | 10 | const { getValues } = useSearchForm();
|
| 11 | + const { |
| 12 | + isLiveRefreshRunning, |
| 13 | + liveRefreshLabel, |
| 14 | + refetchInterval, |
| 15 | + startLiveRefresh, |
| 16 | + stopLiveRefresh, |
| 17 | + } = useLiveRefresh(); |
10 | 18 |
|
11 |
| - const currentDbKey = useWatch({ name: 'table' }) |
12 |
| - const entriesPerPage = useWatch({ name: 'entriesPerPage' }) |
13 |
| - const page = useWatch({ name: 'page' }) |
14 |
| - const sortBy = useWatch({ name: 'sortBy' }) |
15 |
| - const sortOn = useWatch({ name: 'sortOn' }) |
| 19 | + const currentDbKey = useWatch({ name: 'table' }); |
| 20 | + const entriesPerPage = useWatch({ name: 'entriesPerPage' }); |
| 21 | + const page = useWatch({ name: 'page' }); |
| 22 | + const sortBy = useWatch({ name: 'sortBy' }); |
| 23 | + const sortOn = useWatch({ name: 'sortOn' }); |
16 | 24 |
|
17 |
| - return useQuery({ |
18 |
| - enabled: true, |
19 |
| - queryKey: ['get-logs', entriesPerPage, page, sortBy, sortOn, currentDbKey], |
20 |
| - queryFn: async () => { |
21 |
| - if (!isHeaderReady) return null; |
| 25 | + return { |
| 26 | + ...useQuery({ |
| 27 | + enabled: true, |
| 28 | + queryKey: ['get-logs', entriesPerPage, page, sortBy, sortOn, currentDbKey], |
| 29 | + queryFn: async () => { |
| 30 | + if (!isHeaderReady) return null; |
| 31 | + const values = getValues(); |
22 | 32 |
|
23 |
| - return currentDbKey |
24 |
| - ? await fetchLogs(getValues(), fetchInfo.headers, fetchInfo.routePrefix) |
25 |
| - : null; |
26 |
| - }, |
27 |
| - placeholderData: keepPreviousData, |
28 |
| - refetchOnMount: false, |
29 |
| - refetchOnWindowFocus: false, |
30 |
| - retry: false, |
31 |
| - }); |
| 33 | + return currentDbKey |
| 34 | + ? await fetchLogs(values, fetchInfo.headers, fetchInfo.routePrefix) |
| 35 | + : null; |
| 36 | + }, |
| 37 | + placeholderData: keepPreviousData, |
| 38 | + refetchOnMount: false, |
| 39 | + refetchOnWindowFocus: false, |
| 40 | + retry: false, |
| 41 | + refetchInterval, |
| 42 | + }), |
| 43 | + isLiveRefreshRunning, |
| 44 | + liveRefreshLabel, |
| 45 | + startLiveRefresh, |
| 46 | + stopLiveRefresh, |
| 47 | + }; |
32 | 48 | };
|
33 | 49 |
|
34 | 50 | export default useQueryLogs;
|
0 commit comments