From 1ee690b0eb38a554aedafab37a8036ca69e31b9e Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Mon, 18 Aug 2025 19:25:14 -0600 Subject: [PATCH] shared handler --- .../dashboards/queries/useGetDashboard.ts | 14 +++++-- .../metrics/getMetricQueryRequests.ts | 15 ------- .../buster_rest/metrics/queryReqestsServer.ts | 23 +++++++---- .../src/api/buster_rest/metrics/requests.ts | 3 +- .../api/buster_rest/reports/queryRequests.ts | 20 ++++++---- .../BusterAssets/metricServerAssetContext.ts | 39 +++++++++++++++++++ .../BusterAssets/serverAssetContext.ts | 34 ++++++++++++++++ apps/web-tss/src/routes/app/_app/_asset.tsx | 4 ++ .../app/_app/_asset/metrics.$metricId.tsx | 33 ++-------------- .../src/routes/app/_app/metrics.index.tsx | 12 ------ 10 files changed, 118 insertions(+), 79 deletions(-) create mode 100644 apps/web-tss/src/context/BusterAssets/metricServerAssetContext.ts create mode 100644 apps/web-tss/src/context/BusterAssets/serverAssetContext.ts diff --git a/apps/web-tss/src/api/buster_rest/dashboards/queries/useGetDashboard.ts b/apps/web-tss/src/api/buster_rest/dashboards/queries/useGetDashboard.ts index b09c3e7f7..0e8052c09 100644 --- a/apps/web-tss/src/api/buster_rest/dashboards/queries/useGetDashboard.ts +++ b/apps/web-tss/src/api/buster_rest/dashboards/queries/useGetDashboard.ts @@ -158,8 +158,14 @@ export const prefetchGetDashboard = async ( shouldInitializeMetrics: true, }); }; - await queryClient.prefetchQuery({ - ...dashboardQueryKeys.dashboardGetDashboard(id, chosenVersionNumber), - queryFn, - }); + + const queryKey = dashboardQueryKeys.dashboardGetDashboard(id, chosenVersionNumber)?.queryKey; + const existingData = queryClient.getQueryData(queryKey); + if (!existingData) { + await queryClient.prefetchQuery({ + ...dashboardQueryKeys.dashboardGetDashboard(id, chosenVersionNumber), + queryFn, + }); + } + return existingData || queryClient.getQueryData(queryKey); }; diff --git a/apps/web-tss/src/api/buster_rest/metrics/getMetricQueryRequests.ts b/apps/web-tss/src/api/buster_rest/metrics/getMetricQueryRequests.ts index 08486af2a..4c4414ed6 100644 --- a/apps/web-tss/src/api/buster_rest/metrics/getMetricQueryRequests.ts +++ b/apps/web-tss/src/api/buster_rest/metrics/getMetricQueryRequests.ts @@ -169,21 +169,6 @@ export const useGetMetricData = ( }); }; -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 ( { id, version_number }: { id: string; version_number: number }, queryClient: QueryClient diff --git a/apps/web-tss/src/api/buster_rest/metrics/queryReqestsServer.ts b/apps/web-tss/src/api/buster_rest/metrics/queryReqestsServer.ts index f083eee1b..77391d3ed 100644 --- a/apps/web-tss/src/api/buster_rest/metrics/queryReqestsServer.ts +++ b/apps/web-tss/src/api/buster_rest/metrics/queryReqestsServer.ts @@ -8,14 +8,21 @@ export const prefetchGetMetric = async ( queryClientProp?: 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({ - ...metricsQueryKeys.metricsGetMetric(params.id, params.version_number || 'LATEST'), - queryFn: async () => { - const result = await getMetric(params); - return upgradeMetricToIMetric(result, null); - }, - }); + if (!existingData) { + await queryClient.prefetchQuery({ + ...metricsQueryKeys.metricsGetMetric(params.id, params.version_number || 'LATEST'), + queryFn: async () => { + const result = await getMetric(params); + return upgradeMetricToIMetric(result, null); + }, + }); + } - return queryClient; + return existingData || queryClient.getQueryData(queryKey); }; diff --git a/apps/web-tss/src/api/buster_rest/metrics/requests.ts b/apps/web-tss/src/api/buster_rest/metrics/requests.ts index fe09a179a..8b73ed23c 100644 --- a/apps/web-tss/src/api/buster_rest/metrics/requests.ts +++ b/apps/web-tss/src/api/buster_rest/metrics/requests.ts @@ -25,13 +25,12 @@ import type { import { mainApi, mainApiV2 } from '../instances'; export const getMetric = async (params: GetMetricRequest): Promise => { - console.log('getMetric1!!!!'); return mainApi .get(`/metric_files/${params.id}`, { params, }) .then((res) => { - console.log('getMetric res'); + console.log('getMetric res', typeof window !== 'undefined'); return res.data; }); }; diff --git a/apps/web-tss/src/api/buster_rest/reports/queryRequests.ts b/apps/web-tss/src/api/buster_rest/reports/queryRequests.ts index 2202664ef..455185c2d 100644 --- a/apps/web-tss/src/api/buster_rest/reports/queryRequests.ts +++ b/apps/web-tss/src/api/buster_rest/reports/queryRequests.ts @@ -75,17 +75,21 @@ export const prefetchGetReportsListClient = async ( export const prefetchGetReport = async ( reportId: string, - version_number: number | undefined, - queryClientProp?: QueryClient + report_version_number: number | undefined, + queryClient: QueryClient ) => { - const queryClient = queryClientProp || new QueryClient(); + const version_number = report_version_number || 'LATEST'; - await queryClient.prefetchQuery({ - ...reportsQueryKeys.reportsGetReport(reportId, version_number || 'LATEST'), - queryFn: () => getReportById(reportId), - }); + const queryKey = reportsQueryKeys.reportsGetReport(reportId, version_number)?.queryKey; + const existingData = queryClient.getQueryData(queryKey); + if (!existingData) { + await queryClient.prefetchQuery({ + ...reportsQueryKeys.reportsGetReport(reportId, version_number || 'LATEST'), + queryFn: () => getReportById(reportId), + }); + } - return queryClient; + return existingData || queryClient.getQueryData(queryKey); }; /** diff --git a/apps/web-tss/src/context/BusterAssets/metricServerAssetContext.ts b/apps/web-tss/src/context/BusterAssets/metricServerAssetContext.ts new file mode 100644 index 000000000..c2de0f7e3 --- /dev/null +++ b/apps/web-tss/src/context/BusterAssets/metricServerAssetContext.ts @@ -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' }, + ], +}); diff --git a/apps/web-tss/src/context/BusterAssets/serverAssetContext.ts b/apps/web-tss/src/context/BusterAssets/serverAssetContext.ts new file mode 100644 index 000000000..34627d5f1 --- /dev/null +++ b/apps/web-tss/src/context/BusterAssets/serverAssetContext.ts @@ -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, + }; +}; diff --git a/apps/web-tss/src/routes/app/_app/_asset.tsx b/apps/web-tss/src/routes/app/_app/_asset.tsx index 40bf97279..9a3a68f9a 100644 --- a/apps/web-tss/src/routes/app/_app/_asset.tsx +++ b/apps/web-tss/src/routes/app/_app/_asset.tsx @@ -6,12 +6,16 @@ import { useParams, useSearch, } from '@tanstack/react-router'; +import { z } from 'zod'; import { getTitle as getAssetTitle } from '@/api/buster_rest/title'; import { AppAssetCheckLayout, type AppAssetCheckLayoutProps } from '@/layouts/AppAssetCheckLayout'; import { getAssetIdAndVersionNumber } from '@/layouts/AppAssetCheckLayout/preloadAsset'; export const Route = createFileRoute('/app/_app/_asset')({ component: RouteComponent, + loaderDeps: ({ search }) => { + return search; + }, context: () => ({ getAssetTitle, }), diff --git a/apps/web-tss/src/routes/app/_app/_asset/metrics.$metricId.tsx b/apps/web-tss/src/routes/app/_app/_asset/metrics.$metricId.tsx index 47a9e66c0..e2b10c47a 100644 --- a/apps/web-tss/src/routes/app/_app/_asset/metrics.$metricId.tsx +++ b/apps/web-tss/src/routes/app/_app/_asset/metrics.$metricId.tsx @@ -1,36 +1,9 @@ -import type { AssetType } from '@buster/server-shared/assets'; -import { createFileRoute, Link } from '@tanstack/react-router'; -import { z } from 'zod'; -import { prefetchGetMetric } from '@/api/buster_rest/metrics/getMetricQueryRequests'; - -const searchParamsSchema = z.object({ - metric_version_number: z.coerce.number().optional(), -}); +import { createFileRoute } from '@tanstack/react-router'; +import * as metricServerContext from '@/context/BusterAssets/metricServerAssetContext'; 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, - }, - 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, + ...metricServerContext, }); function RouteComponent() { diff --git a/apps/web-tss/src/routes/app/_app/metrics.index.tsx b/apps/web-tss/src/routes/app/_app/metrics.index.tsx index 22026d261..29941cbcb 100644 --- a/apps/web-tss/src/routes/app/_app/metrics.index.tsx +++ b/apps/web-tss/src/routes/app/_app/metrics.index.tsx @@ -11,16 +11,4 @@ export const Route = createFileRoute('/app/_app/metrics/')({ ], }), component: MetricListContainer, - loader: async ({ context }) => { - console.log('metrics index loader'); - return { - swag: true, - }; - }, - beforeLoad: ({ context }) => { - console.log('metrics index beforeLoad'); - return { - swagger: true, - }; - }, });