mirror of https://github.com/buster-so/buster.git
try to simplify the metric latest version
This commit is contained in:
parent
de77bc15e3
commit
c6f1222cf7
|
@ -10,7 +10,7 @@ import { Query, useQueryClient } from '@tanstack/react-query';
|
||||||
import { prefetchGetMetricDataClient } from '../metrics/queryRequests';
|
import { prefetchGetMetricDataClient } from '../metrics/queryRequests';
|
||||||
import { dashboardsGetDashboard } from './requests';
|
import { dashboardsGetDashboard } from './requests';
|
||||||
import { useParams, useSearchParams } from 'next/navigation';
|
import { useParams, useSearchParams } from 'next/navigation';
|
||||||
import { useMemo } from 'react';
|
import { useMemo, useState, useEffect } from 'react';
|
||||||
import { RustApiError } from '../errors';
|
import { RustApiError } from '../errors';
|
||||||
import last from 'lodash/last';
|
import last from 'lodash/last';
|
||||||
|
|
||||||
|
@ -149,19 +149,38 @@ const getLatestVersionNumber = (
|
||||||
|
|
||||||
const useGetLatestDashboardVersion = ({ dashboardId }: { dashboardId: string }) => {
|
const useGetLatestDashboardVersion = ({ dashboardId }: { dashboardId: string }) => {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
const [latestVersion, setLatestVersion] = useState<number | null>(null);
|
||||||
|
|
||||||
const memoizedKey = useMemo(() => {
|
const memoizedKey = useMemo(() => {
|
||||||
return dashboardQueryKeys.dashboardGetDashboard(dashboardId, null).queryKey.slice(0, -1);
|
return dashboardQueryKeys.dashboardGetDashboard(dashboardId, null).queryKey.slice(0, -1);
|
||||||
}, [dashboardId]);
|
}, [dashboardId]);
|
||||||
|
|
||||||
const queries = queryClient.getQueriesData<BusterDashboardResponse, any>({
|
const updateLatestVersion = useMemoizedFn(() => {
|
||||||
queryKey: memoizedKey,
|
const queries = queryClient.getQueriesData<BusterDashboardResponse, any>({
|
||||||
predicate: filterMetricPredicate
|
queryKey: memoizedKey,
|
||||||
|
predicate: filterMetricPredicate
|
||||||
|
});
|
||||||
|
const newVersion = getLatestVersionNumber(queries);
|
||||||
|
setLatestVersion(newVersion);
|
||||||
});
|
});
|
||||||
|
|
||||||
const latestVersion = useMemo(() => {
|
useEffect(() => {
|
||||||
return getLatestVersionNumber(queries);
|
// Initial computation
|
||||||
}, [queries.length]);
|
updateLatestVersion();
|
||||||
|
|
||||||
|
// Subscribe to cache updates
|
||||||
|
const unsubscribe = queryClient.getQueryCache().subscribe((event) => {
|
||||||
|
if (
|
||||||
|
event.type === 'updated' &&
|
||||||
|
event.query.queryKey[2] === last(memoizedKey) &&
|
||||||
|
event.query.queryKey[1] === memoizedKey[1]
|
||||||
|
) {
|
||||||
|
updateLatestVersion();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => unsubscribe();
|
||||||
|
}, [memoizedKey, updateLatestVersion]);
|
||||||
|
|
||||||
return latestVersion;
|
return latestVersion;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue