♻️ Refactor torrent network traffic widget #616

This commit is contained in:
Manuel
2023-01-22 21:35:13 +01:00
parent 2c2f3ea5f4
commit 653f8c6fd8
12 changed files with 543 additions and 450 deletions

View File

@@ -0,0 +1,11 @@
import { useQuery } from '@tanstack/react-query';
import { NormalizedDownloadQueueResponse } from '../../../types/api/downloads/queue/NormalizedDownloadQueueResponse';
export const useGetDownloadClientsQueue = () => useQuery({
queryKey: ['network-speed'],
queryFn: async (): Promise<NormalizedDownloadQueueResponse> => {
const response = await fetch('/api/modules/downloads');
return response.json();
},
refetchInterval: 3000,
});

View File

@@ -1,27 +0,0 @@
import { Query, useQuery } from '@tanstack/react-query';
import axios from 'axios';
import { NormalizedTorrentListResponse } from '../../../types/api/NormalizedTorrentListResponse';
interface TorrentsDataRequestParams {
appId: string;
refreshInterval: number;
}
export const useGetTorrentData = (params: TorrentsDataRequestParams) =>
useQuery({
queryKey: ['torrentsData', params.appId],
queryFn: fetchData,
refetchOnWindowFocus: true,
refetchInterval(_: any, query: Query) {
if (query.state.fetchFailureCount < 3) {
return params.refreshInterval;
}
return false;
},
enabled: !!params.appId,
});
const fetchData = async (): Promise<NormalizedTorrentListResponse> => {
const response = await axios.post('/api/modules/torrents');
return response.data as NormalizedTorrentListResponse;
};