From 3f623458b34533af704ed8f3ee564c5b8673fc36 Mon Sep 17 00:00:00 2001 From: yuri-sakharov <10849682+yuri-sakharov@users.noreply.github.com> Date: Thu, 27 Nov 2025 11:44:50 +0200 Subject: [PATCH] Fixed queries, mutations and JSON parse (#626) * Fixed queries ond mutations * Fixed JSON.parse in analytics --- frontend/public/analytics.js | 21 ++++- frontend/src/API/k8s.ts | 34 +++---- frontend/src/API/other.ts | 18 ++-- frontend/src/API/releases.ts | 94 +++++++++---------- frontend/src/API/repositories.ts | 25 +++-- frontend/src/API/scanners.ts | 20 ++-- frontend/src/API/shared.ts | 11 +-- .../InstallReleaseChartModal.tsx | 5 +- .../InstallRepoChartModal.tsx | 6 +- .../components/revision/RevisionDetails.tsx | 5 +- 10 files changed, 124 insertions(+), 115 deletions(-) diff --git a/frontend/public/analytics.js b/frontend/public/analytics.js index f4826d3..ebbf2b2 100644 --- a/frontend/public/analytics.js +++ b/frontend/public/analytics.js @@ -13,8 +13,24 @@ const BASE_ANALYTIC_MSG = { referrerPolicy: "no-referrer" }; xhr.onload = function() { - if (xhr.readyState === XMLHttpRequest.DONE) { - const status = JSON.parse(xhr.responseText); + if (xhr.readyState !== XMLHttpRequest.DONE) { + return; + } + + const responseTxt = xhr.responseText?.trim(); + if (!responseTxt) { + console.warn("Analytics response is empty"); + return; + } + + let status; + try { + status = JSON.parse(responseTxt); + } catch (e) { + console.error("Failed to parse JSON: ", xhr.responseText, e); + return; + } + const version = status.CurVer; if (status.Analytics) { enableDD(version); @@ -23,7 +39,6 @@ xhr.onload = function() { } else { console.log("Analytics is disabled in this session"); } - } }; xhr.open("GET", "/status", true); xhr.send(null); diff --git a/frontend/src/API/k8s.ts b/frontend/src/API/k8s.ts index 1fb92f9..c292fd7 100644 --- a/frontend/src/API/k8s.ts +++ b/frontend/src/API/k8s.ts @@ -5,11 +5,11 @@ import apiService from "./apiService"; // Get list of kubectl contexts configured locally function useGetKubectlContexts(options?: UseQueryOptions) { - return useQuery( - ["k8s", "contexts"], - () => apiService.fetchWithDefaults("/api/k8s/contexts"), - options - ); + return useQuery({ + queryKey:["k8s", "contexts"], + queryFn:() => apiService.fetchWithDefaults("/api/k8s/contexts"), +...(options ?? {}) +}); } // Get resources information @@ -19,13 +19,13 @@ function useGetK8sResource( namespace: string, options?: UseQueryOptions ) { - return useQuery( - ["k8s", kind, "get", name, namespace], - () => + return useQuery({ + queryKey: ["k8s", kind, "get", name, namespace], + queryFn:() => apiService.fetchWithDefaults( `/api/k8s/${kind}/get?name=${name}&namespace=${namespace}` ), - options + ...(options ?? {})} ); } @@ -34,11 +34,11 @@ function useGetK8sResourceList( kind: string, options?: UseQueryOptions ) { - return useQuery( - ["k8s", kind, "list"], - () => + return useQuery({ + queryKey: ["k8s", kind, "list"], + queryFn:() => apiService.fetchWithDefaults(`/api/k8s/${kind}/list`), - options + ...(options ?? {})} ); } @@ -49,9 +49,9 @@ function useGetK8sResourceDescribe( namespace: string, options?: UseQueryOptions ) { - return useQuery( - ["k8s", kind, "describe", name, namespace], - () => + return useQuery({ + queryKey:["k8s", kind, "describe", name, namespace], + queryFn:() => apiService.fetchWithDefaults( `/api/k8s/${kind}/describe?name=${name}&namespace=${namespace}`, { @@ -60,6 +60,6 @@ function useGetK8sResourceDescribe( }, } ), - options + ...(options ?? {})} ); } diff --git a/frontend/src/API/other.ts b/frontend/src/API/other.ts index bdc35b2..add491a 100644 --- a/frontend/src/API/other.ts +++ b/frontend/src/API/other.ts @@ -11,12 +11,12 @@ import apiService from "./apiService"; export function useShutdownHelmDashboard( options?: UseMutationOptions ) { - return useMutation( - () => + return useMutation({ + mutationFn:() => apiService.fetchWithDefaults("/", { method: "DELETE", }), - options +...(options ?? {})} ); } @@ -24,11 +24,9 @@ export function useShutdownHelmDashboard( export function useGetApplicationStatus( options?: UseQueryOptions ) { - return useQuery( - ["status"], - () => apiService.fetchWithDefaults("/status"), - { - ...options, - } - ); + return useQuery({ + queryKey: ["status"], + queryFn: () => apiService.fetchWithDefaults("/status"), + ...(options ?? {}), + }); } diff --git a/frontend/src/API/releases.ts b/frontend/src/API/releases.ts index 9160ec7..e316d2c 100644 --- a/frontend/src/API/releases.ts +++ b/frontend/src/API/releases.ts @@ -14,10 +14,10 @@ export function useGetInstalledReleases( context: string, options?: UseQueryOptions ) { - return useQuery( - ["installedReleases", context], - () => apiService.fetchWithDefaults("/api/helm/releases"), - options + return useQuery({ + queryKey:["installedReleases", context], + queryFn: () => apiService.fetchWithDefaults("/api/helm/releases"), + ...(options ?? {})} ); } @@ -62,13 +62,13 @@ export function useGetReleaseManifest({ chartName: string; options?: UseQueryOptions; }) { - return useQuery( - ["manifest", namespace, chartName], - () => + return useQuery({ + queryKey:["manifest", namespace, chartName], + queryFn:() => apiService.fetchWithDefaults( `/api/helm/releases/${namespace}/${chartName}/manifests` ), - options + ...(options ?? {})} ); } @@ -78,13 +78,13 @@ export function useGetResources( name: string, options?: UseQueryOptions ) { - const { data, ...rest } = useQuery( - ["resources", ns, name], - () => + const { data, ...rest } = useQuery({ + queryKey:["resources", ns, name], + queryFn:() => apiService.fetchWithDefaults( `/api/helm/releases/${ns}/${name}/resources?health=true` ), - options + ...(options ?? {})} ); return { @@ -115,42 +115,42 @@ export function useGetResourceDescription( name: string, options?: UseQueryOptions ) { - return useQuery( - ["describe", type, ns, name], - () => + return useQuery({ + queryKey:["describe", type, ns, name], + queryFn:() => apiService.fetchWithDefaults( `/api/k8s/${type}/describe?name=${name}&namespace=${ns}`, { headers: { "Content-Type": "text/plain; charset=utf-8" }, } ), - options + ...(options ?? {})} ); } export function useGetLatestVersion( chartName: string, options?: UseQueryOptions ) { - return useQuery( - ["latestver", chartName], - () => + return useQuery({ + queryKey:["latestver", chartName], + queryFn:() => apiService.fetchWithDefaults( `/api/helm/repositories/latestver?name=${chartName}` ), - options +...(options ?? {})} ); } export function useGetVersions( chartName: string, options?: UseQueryOptions ) { - return useQuery( - ["versions", chartName], - () => + return useQuery({ + queryKey: ["versions", chartName], + queryFn: () => apiService.fetchWithDefaults( `/api/helm/repositories/versions?name=${chartName}` ), - options + ...(options ?? {})} ); } @@ -160,16 +160,16 @@ export function useGetReleaseInfoByType( options?: UseQueryOptions ) { const { chart, namespace, tab, revision } = params; - return useQuery( - [tab, namespace, chart, revision, additionalParams], - () => + return useQuery({ + queryKey:[tab, namespace, chart, revision, additionalParams], + queryFn:() => apiService.fetchWithDefaults( `/api/helm/releases/${namespace}/${chart}/${tab}?revision=${revision}${additionalParams}`, { headers: { "Content-Type": "text/plain; charset=utf-8" }, } ), - options + ...(options ?? {})} ); } @@ -177,16 +177,16 @@ export function useGetDiff( formData: FormData, options?: UseQueryOptions ) { - return useQuery( - ["diff", formData], - () => { + return useQuery({ + queryKey:["diff", formData], + queryFn:() => { return apiService.fetchWithDefaults("/diff", { body: formData, method: "POST", }); }, - options + ...(options ?? {})} ); } @@ -202,7 +202,7 @@ export function useRollbackRelease( void, unknown, { ns: string; name: string; revision: number } - >(({ ns, name, revision }) => { + >({mutationFn:({ ns, name, revision }) => { const formData = new FormData(); formData.append("revision", revision.toString()); @@ -213,15 +213,15 @@ export function useRollbackRelease( body: formData, } ); - }, options); + }, ...(options ?? {})}); } // Run the tests on a release export function useTestRelease( options?: UseMutationOptions ) { - return useMutation( - ({ ns, name }) => { + return useMutation({ + mutationFn:({ ns, name }) => { return apiService.fetchWithDefaults( `/api/helm/releases/${ns}/${name}/test`, { @@ -229,7 +229,7 @@ export function useTestRelease( } ); }, - options +...(options ?? {})} ); } @@ -248,9 +248,9 @@ export function useChartReleaseValues({ version?: string; options?: UseQueryOptions; }) { - return useQuery( - ["values", namespace, release, userDefinedValue, version], - () => + return useQuery({ + queryKey:["values", namespace, release, userDefinedValue, version], + queryFn:() => apiService.fetchWithDefaults( `/api/helm/releases/${namespace}/${release}/values?${"userDefined=true"}${ revision ? `&revision=${revision}` : "" @@ -259,7 +259,7 @@ export function useChartReleaseValues({ headers: { "Content-Type": "text/plain; charset=utf-8" }, } ), - options + ...(options ?? {})} ); } @@ -282,8 +282,8 @@ export const useVersionData = ({ isInstallRepoChart?: boolean; options?: UseQueryOptions; }) => { - return useQuery( - [ + return useQuery({ + queryKey: [ version, userValues, chartAddress, @@ -292,7 +292,7 @@ export const useVersionData = ({ releaseName, isInstallRepoChart, ], - async () => { + queryFn: async () => { const formData = getVersionManifestFormData({ version, userValues, @@ -314,9 +314,9 @@ export const useVersionData = ({ return data; }, - // @ts-ignore - options - ); + + ...(options ?? {}) + }); }; // Request objects diff --git a/frontend/src/API/repositories.ts b/frontend/src/API/repositories.ts index c06fd5f..f39abd8 100644 --- a/frontend/src/API/repositories.ts +++ b/frontend/src/API/repositories.ts @@ -11,12 +11,12 @@ import apiService from "./apiService"; export function useGetRepositories( options?: UseQueryOptions ) { - return useQuery( - ["helm", "repositories"], - () => + return useQuery({ + queryKey:["helm", "repositories"], + queryFn: () => apiService.fetchWithDefaults("/api/helm/repositories"), - options - ); + ...(options ?? {}) + }); } // Update repository from remote @@ -24,14 +24,14 @@ export function useUpdateRepo( repo: string, options?: UseMutationOptions ) { - return useMutation(() => { + return useMutation({ mutationFn:() => { return apiService.fetchWithDefaults( `/api/helm/repositories/${repo}`, { method: "POST", } ); - }, options); + }, ...(options ?? {})}); } // Remove repository @@ -39,14 +39,14 @@ export function useDeleteRepo( repo: string, options?: UseMutationOptions ) { - return useMutation(() => { + return useMutation({mutationFn:() => { return apiService.fetchWithDefaults( `/api/helm/repositories/${repo}`, { method: "DELETE", } ); - }, options); + }, ...(options ?? {})}); } export function useChartRepoValues({ @@ -56,16 +56,15 @@ export function useChartRepoValues({ version: string; chart: string; }) { - return useQuery( - ["helm", "repositories", "values", chart, version], - () => + return useQuery({ + queryKey:["helm", "repositories", "values", chart, version], + queryFn:() => apiService.fetchWithDefaults( `/api/helm/repositories/values?chart=${chart}&version=${version}`, { headers: { "Content-Type": "text/plain; charset=utf-8" }, } ), - { enabled: Boolean(version) && Boolean(chart), } ); diff --git a/frontend/src/API/scanners.ts b/frontend/src/API/scanners.ts index eece8fd..046bc3a 100644 --- a/frontend/src/API/scanners.ts +++ b/frontend/src/API/scanners.ts @@ -12,10 +12,10 @@ import apiService from "./apiService"; // Get list of discovered scanners function useGetDiscoveredScanners(options?: UseQueryOptions) { - return useQuery( - ["scanners"], - () => apiService.fetchWithDefaults("/api/scanners"), - options + return useQuery({ + queryKey: ["scanners"], + queryFn:() => apiService.fetchWithDefaults("/api/scanners"), + ...(options ?? {})} ); } @@ -26,13 +26,13 @@ function useScanManifests( ) { const formData = new FormData(); formData.append("manifest", manifest); - return useMutation( - () => + return useMutation({ + mutationFn:() => apiService.fetchWithDefaults("/api/scanners/manifests", { method: "POST", body: formData, }), - options + ...(options ?? {})} ); } @@ -43,12 +43,12 @@ function useScanK8sResource( name: string, options?: UseQueryOptions ) { - return useQuery( + return useQuery({queryKey: ["scanners", "resource", kind, namespace, name], - () => + queryFn:() => apiService.fetchWithDefaults( `/api/scanners/resource/${kind}?namespace=${namespace}&name=${name}` ), - options + ...(options ?? {})} ); } diff --git a/frontend/src/API/shared.ts b/frontend/src/API/shared.ts index 3c5ee85..75ec43a 100644 --- a/frontend/src/API/shared.ts +++ b/frontend/src/API/shared.ts @@ -43,9 +43,9 @@ export const useDiffData = ({ selectedVerData: { [key: string]: string }; chart: string; }) => { - return useQuery( - [selectedRepo, versionsError, chart, currentVerManifest, selectedVerData], - async () => { + return useQuery({ + queryKey: [selectedRepo, versionsError, chart, currentVerManifest, selectedVerData], + queryFn: async () => { const formData = new FormData(); formData.append("a", currentVerManifest); formData.append("b", selectedVerData.manifest); @@ -57,8 +57,7 @@ export const useDiffData = ({ return diff; }, - { - enabled: Boolean(selectedVerData), - } + enabled: Boolean(selectedVerData), +} ); }; diff --git a/frontend/src/components/modal/InstallChartModal/InstallReleaseChartModal.tsx b/frontend/src/components/modal/InstallChartModal/InstallReleaseChartModal.tsx index c718afe..cc31efc 100644 --- a/frontend/src/components/modal/InstallChartModal/InstallReleaseChartModal.tsx +++ b/frontend/src/components/modal/InstallChartModal/InstallReleaseChartModal.tsx @@ -131,7 +131,7 @@ export const InstallReleaseChartModal = ({ // Confirm method (install) const setReleaseVersionMutation = useMutation( - [ + {mutationKey:[ "setVersion", namespace, releaseName, @@ -140,7 +140,7 @@ export const InstallReleaseChartModal = ({ selectedCluster, chartAddress, ], - async () => { + mutationFn:async () => { setInstallError(""); const formData = new FormData(); formData.append("preview", "false"); @@ -161,7 +161,6 @@ export const InstallReleaseChartModal = ({ ); return data; }, - { onSuccess: async (response) => { onClose(); setSelectedVersionData({ version: "", urls: [] }); //cleanup diff --git a/frontend/src/components/modal/InstallChartModal/InstallRepoChartModal.tsx b/frontend/src/components/modal/InstallChartModal/InstallRepoChartModal.tsx index 76a7289..beb0328 100644 --- a/frontend/src/components/modal/InstallChartModal/InstallRepoChartModal.tsx +++ b/frontend/src/components/modal/InstallChartModal/InstallRepoChartModal.tsx @@ -113,7 +113,7 @@ export const InstallRepoChartModal = ({ // Confirm method (install) const setReleaseVersionMutation = useMutation( - [ + {mutationKey:[ "setVersion", namespace, releaseName, @@ -122,7 +122,7 @@ export const InstallRepoChartModal = ({ selectedCluster, chartAddress, ], - async () => { + mutationFn:async () => { setInstallError(""); const formData = new FormData(); formData.append("preview", "false"); @@ -139,7 +139,7 @@ export const InstallRepoChartModal = ({ ); return data; }, - { + onSuccess: async (response) => { onClose(); navigate( diff --git a/frontend/src/components/revision/RevisionDetails.tsx b/frontend/src/components/revision/RevisionDetails.tsx index f861e37..83ca841 100644 --- a/frontend/src/components/revision/RevisionDetails.tsx +++ b/frontend/src/components/revision/RevisionDetails.tsx @@ -459,15 +459,14 @@ const Uninstall = () => { }); const uninstallMutation = useMutation( - ["uninstall", namespace, chart], - () => + {mutationKey:["uninstall", namespace, chart], + mutationFn:() => apiService.fetchWithDefaults( "/api/helm/releases/" + namespace + "/" + chart, { method: "delete", } ), - { onSuccess: () => { window.location.href = "/"; },