44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
|
|
import { useQuery } from '@tanstack/react-query';
|
||
|
|
import { config } from '../../config';
|
||
|
|
import { useAuthStore } from '../../auth/auth-store';
|
||
|
|
|
||
|
|
export function useRouteCatalog() {
|
||
|
|
return useQuery({
|
||
|
|
queryKey: ['routes', 'catalog'],
|
||
|
|
queryFn: async () => {
|
||
|
|
const token = useAuthStore.getState().accessToken;
|
||
|
|
const res = await fetch(`${config.apiBaseUrl}/routes/catalog`, {
|
||
|
|
headers: {
|
||
|
|
Authorization: `Bearer ${token}`,
|
||
|
|
'X-Cameleer-Protocol-Version': '1',
|
||
|
|
},
|
||
|
|
});
|
||
|
|
if (!res.ok) throw new Error('Failed to load route catalog');
|
||
|
|
return res.json();
|
||
|
|
},
|
||
|
|
refetchInterval: 15_000,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useRouteMetrics(from?: string, to?: string, appId?: string) {
|
||
|
|
return useQuery({
|
||
|
|
queryKey: ['routes', 'metrics', from, to, appId],
|
||
|
|
queryFn: async () => {
|
||
|
|
const token = useAuthStore.getState().accessToken;
|
||
|
|
const params = new URLSearchParams();
|
||
|
|
if (from) params.set('from', from);
|
||
|
|
if (to) params.set('to', to);
|
||
|
|
if (appId) params.set('appId', appId);
|
||
|
|
const res = await fetch(`${config.apiBaseUrl}/routes/metrics?${params}`, {
|
||
|
|
headers: {
|
||
|
|
Authorization: `Bearer ${token}`,
|
||
|
|
'X-Cameleer-Protocol-Version': '1',
|
||
|
|
},
|
||
|
|
});
|
||
|
|
if (!res.ok) throw new Error('Failed to load route metrics');
|
||
|
|
return res.json();
|
||
|
|
},
|
||
|
|
refetchInterval: 30_000,
|
||
|
|
});
|
||
|
|
}
|