From f2402c523ac089a26edcc985120946cd83a564d2 Mon Sep 17 00:00:00 2001 From: HendrikThePendric Date: Mon, 13 Jan 2025 16:42:01 +0100 Subject: [PATCH] fix: add hook dependencies and ensure fetchSuperSetBaseUrl is a stable reference --- src/api/superSetGateway.js | 23 +++++++++++++++-------- src/components/SystemSettingsProvider.js | 2 +- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/api/superSetGateway.js b/src/api/superSetGateway.js index 897107066..874c4d8fe 100644 --- a/src/api/superSetGateway.js +++ b/src/api/superSetGateway.js @@ -1,15 +1,20 @@ import { useConfig } from '@dhis2/app-service-config' +import { useCallback, useMemo } from 'react' export const useFetchSuperSetBaseUrl = () => { const { baseUrl } = useConfig() - const url = new URL( - 'superset-gateway/api/info', - baseUrl === '..' - ? window.location.href.split('dhis-web-dashboard/')[0] - : `${baseUrl}/` - )?.href + const url = useMemo( + () => + new URL( + 'superset-gateway/api/info', + baseUrl === '..' + ? window.location.href.split('dhis-web-dashboard/')[0] + : `${baseUrl}/` + )?.href, + [baseUrl] + ) - return async () => { + const fetchSuperSetBaseUrl = useCallback(async () => { const response = await fetch(url) if (!response.ok) { throw new Error(`Response status: ${response.status}`) @@ -17,5 +22,7 @@ export const useFetchSuperSetBaseUrl = () => { const data = await response.json() return data.supersetBaseUrl - } + }, [url]) + + return fetchSuperSetBaseUrl } diff --git a/src/components/SystemSettingsProvider.js b/src/components/SystemSettingsProvider.js index 6381bc935..051b27801 100644 --- a/src/components/SystemSettingsProvider.js +++ b/src/components/SystemSettingsProvider.js @@ -40,7 +40,7 @@ const SystemSettingsProvider = ({ children }) => { setSettings(resolvedSystemSettings) } fetchData() - }, []) + }, [engine, fetchSuperSetBaseUrl]) return (