shared handler

This commit is contained in:
Nate Kelley 2025-08-18 19:25:14 -06:00
parent 1025371a69
commit 1ee690b0eb
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
10 changed files with 118 additions and 79 deletions

View File

@ -158,8 +158,14 @@ export const prefetchGetDashboard = async (
shouldInitializeMetrics: true, shouldInitializeMetrics: true,
}); });
}; };
await queryClient.prefetchQuery({
...dashboardQueryKeys.dashboardGetDashboard(id, chosenVersionNumber), const queryKey = dashboardQueryKeys.dashboardGetDashboard(id, chosenVersionNumber)?.queryKey;
queryFn, const existingData = queryClient.getQueryData(queryKey);
}); if (!existingData) {
await queryClient.prefetchQuery({
...dashboardQueryKeys.dashboardGetDashboard(id, chosenVersionNumber),
queryFn,
});
}
return existingData || queryClient.getQueryData(queryKey);
}; };

View File

@ -169,21 +169,6 @@ export const useGetMetricData = <TData = BusterMetricDataExtended>(
}); });
}; };
export const prefetchGetMetric = async (
{ id, version_number }: { id: string; version_number: number | undefined },
queryClient: QueryClient
) => {
const options = metricsQueryKeys.metricsGetMetric(id, version_number || 'LATEST');
const existingData = queryClient.getQueryData(options.queryKey);
if (!existingData) {
await queryClient.prefetchQuery({
...options,
queryFn: () => getMetric({ id, version_number }),
});
}
return existingData;
};
export const prefetchGetMetricDataClient = async ( export const prefetchGetMetricDataClient = async (
{ id, version_number }: { id: string; version_number: number }, { id, version_number }: { id: string; version_number: number },
queryClient: QueryClient queryClient: QueryClient

View File

@ -8,14 +8,21 @@ export const prefetchGetMetric = async (
queryClientProp?: QueryClient queryClientProp?: QueryClient
) => { ) => {
const queryClient = queryClientProp || new QueryClient(); const queryClient = queryClientProp || new QueryClient();
const queryKey = metricsQueryKeys.metricsGetMetric(
params.id,
params.version_number || 'LATEST'
)?.queryKey;
const existingData = queryClient.getQueryData(queryKey);
await queryClient.prefetchQuery({ if (!existingData) {
...metricsQueryKeys.metricsGetMetric(params.id, params.version_number || 'LATEST'), await queryClient.prefetchQuery({
queryFn: async () => { ...metricsQueryKeys.metricsGetMetric(params.id, params.version_number || 'LATEST'),
const result = await getMetric(params); queryFn: async () => {
return upgradeMetricToIMetric(result, null); const result = await getMetric(params);
}, return upgradeMetricToIMetric(result, null);
}); },
});
}
return queryClient; return existingData || queryClient.getQueryData(queryKey);
}; };

View File

@ -25,13 +25,12 @@ import type {
import { mainApi, mainApiV2 } from '../instances'; import { mainApi, mainApiV2 } from '../instances';
export const getMetric = async (params: GetMetricRequest): Promise<GetMetricResponse> => { export const getMetric = async (params: GetMetricRequest): Promise<GetMetricResponse> => {
console.log('getMetric1!!!!');
return mainApi return mainApi
.get<GetMetricResponse>(`/metric_files/${params.id}`, { .get<GetMetricResponse>(`/metric_files/${params.id}`, {
params, params,
}) })
.then((res) => { .then((res) => {
console.log('getMetric res'); console.log('getMetric res', typeof window !== 'undefined');
return res.data; return res.data;
}); });
}; };

View File

@ -75,17 +75,21 @@ export const prefetchGetReportsListClient = async (
export const prefetchGetReport = async ( export const prefetchGetReport = async (
reportId: string, reportId: string,
version_number: number | undefined, report_version_number: number | undefined,
queryClientProp?: QueryClient queryClient: QueryClient
) => { ) => {
const queryClient = queryClientProp || new QueryClient(); const version_number = report_version_number || 'LATEST';
await queryClient.prefetchQuery({ const queryKey = reportsQueryKeys.reportsGetReport(reportId, version_number)?.queryKey;
...reportsQueryKeys.reportsGetReport(reportId, version_number || 'LATEST'), const existingData = queryClient.getQueryData(queryKey);
queryFn: () => getReportById(reportId), if (!existingData) {
}); await queryClient.prefetchQuery({
...reportsQueryKeys.reportsGetReport(reportId, version_number || 'LATEST'),
queryFn: () => getReportById(reportId),
});
}
return queryClient; return existingData || queryClient.getQueryData(queryKey);
}; };
/** /**

View File

@ -0,0 +1,39 @@
import type { AssetType } from '@buster/server-shared/assets';
import type { QueryClient } from '@tanstack/react-query';
import { z } from 'zod';
import { prefetchGetMetric } from '@/api/buster_rest/metrics';
export const validateSearch = z.object({
metric_version_number: z.coerce.number().optional(),
});
export const loader = async ({
params: { metricId },
context: { queryClient },
deps: { metric_version_number },
}: {
params: { metricId: string };
deps: { metric_version_number?: number };
context: { queryClient: QueryClient };
}): Promise<{ title: string | undefined }> => {
const data = await prefetchGetMetric(
{ id: metricId, version_number: metric_version_number },
queryClient
);
return {
title: data?.name,
};
};
export const staticData = {
assetType: 'metric' as AssetType,
};
export const head = ({ loaderData }: { loaderData?: { title: string | undefined } } = {}) => ({
meta: [
{ title: loaderData?.title || 'Metric' },
{ name: 'description', content: 'View detailed metric analysis and insights' },
{ name: 'og:title', content: 'Metric' },
{ name: 'og:description', content: 'View detailed metric analysis and insights' },
],
});

View File

@ -0,0 +1,34 @@
import type { QueryClient } from '@tanstack/react-query';
import { prefetchGetDashboard } from '@/api/buster_rest/dashboards';
import { prefetchGetMetric } from '@/api/buster_rest/metrics';
import { prefetchGetReport } from '@/api/buster_rest/reports';
export const createDashboardLoader = async ({
params: { dashboardId },
context: { queryClient },
deps: { dashboard_version_number },
}: {
params: { dashboardId: string };
deps: { dashboard_version_number?: number };
context: { queryClient: QueryClient };
}): Promise<{ title: string | undefined }> => {
const data = await prefetchGetDashboard(dashboardId, dashboard_version_number, queryClient);
return {
title: data?.dashboard?.name,
};
};
export const createReportLoader = async ({
params: { reportId },
context: { queryClient },
deps: { report_version_number },
}: {
params: { reportId: string };
deps: { report_version_number?: number };
context: { queryClient: QueryClient };
}): Promise<{ title: string | undefined }> => {
const data = await prefetchGetReport(reportId, report_version_number, queryClient);
return {
title: data?.name,
};
};

View File

@ -6,12 +6,16 @@ import {
useParams, useParams,
useSearch, useSearch,
} from '@tanstack/react-router'; } from '@tanstack/react-router';
import { z } from 'zod';
import { getTitle as getAssetTitle } from '@/api/buster_rest/title'; import { getTitle as getAssetTitle } from '@/api/buster_rest/title';
import { AppAssetCheckLayout, type AppAssetCheckLayoutProps } from '@/layouts/AppAssetCheckLayout'; import { AppAssetCheckLayout, type AppAssetCheckLayoutProps } from '@/layouts/AppAssetCheckLayout';
import { getAssetIdAndVersionNumber } from '@/layouts/AppAssetCheckLayout/preloadAsset'; import { getAssetIdAndVersionNumber } from '@/layouts/AppAssetCheckLayout/preloadAsset';
export const Route = createFileRoute('/app/_app/_asset')({ export const Route = createFileRoute('/app/_app/_asset')({
component: RouteComponent, component: RouteComponent,
loaderDeps: ({ search }) => {
return search;
},
context: () => ({ context: () => ({
getAssetTitle, getAssetTitle,
}), }),

View File

@ -1,36 +1,9 @@
import type { AssetType } from '@buster/server-shared/assets'; import { createFileRoute } from '@tanstack/react-router';
import { createFileRoute, Link } from '@tanstack/react-router'; import * as metricServerContext from '@/context/BusterAssets/metricServerAssetContext';
import { z } from 'zod';
import { prefetchGetMetric } from '@/api/buster_rest/metrics/getMetricQueryRequests';
const searchParamsSchema = z.object({
metric_version_number: z.coerce.number().optional(),
});
export const Route = createFileRoute('/app/_app/_asset/metrics/$metricId')({ export const Route = createFileRoute('/app/_app/_asset/metrics/$metricId')({
loader: async ({ params, context }) => {
const title = await context.getAssetTitle({
assetId: params.metricId,
assetType: 'metric',
});
await prefetchGetMetric({ id: params.metricId, version_number: 1 }, context.queryClient);
return {
title,
};
},
staticData: {
assetType: 'metric' as Extract<AssetType, 'metric'>,
},
validateSearch: searchParamsSchema,
head: ({ loaderData }) => ({
meta: [
{ title: loaderData?.title || 'Metric' },
{ name: 'description', content: 'View detailed metric analysis and insights' },
{ name: 'og:title', content: 'Metric' },
{ name: 'og:description', content: 'View detailed metric analysis and insights' },
],
}),
component: RouteComponent, component: RouteComponent,
...metricServerContext,
}); });
function RouteComponent() { function RouteComponent() {

View File

@ -11,16 +11,4 @@ export const Route = createFileRoute('/app/_app/metrics/')({
], ],
}), }),
component: MetricListContainer, component: MetricListContainer,
loader: async ({ context }) => {
console.log('metrics index loader');
return {
swag: true,
};
},
beforeLoad: ({ context }) => {
console.log('metrics index beforeLoad');
return {
swagger: true,
};
},
}); });