From 00f88bb69d09fb76db6a5695df24967a61ba50fe Mon Sep 17 00:00:00 2001 From: phamhieu Date: Tue, 19 Apr 2022 12:19:52 +0700 Subject: [PATCH] fix: useProjectSettings refreshInterval to wait for service_api_keys data --- studio/hooks/queries/useProjectSettings.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/studio/hooks/queries/useProjectSettings.ts b/studio/hooks/queries/useProjectSettings.ts index 833bb17f489..e97eba1a0c9 100644 --- a/studio/hooks/queries/useProjectSettings.ts +++ b/studio/hooks/queries/useProjectSettings.ts @@ -4,7 +4,18 @@ import useSWR from 'swr' export function useProjectSettings(ref?: string) { const url = `${API_URL}/props/project/${ref}/settings` - const { data, error } = useSWR(ref ? url : null, get) + const { data, error } = useSWR(ref ? url : null, get, { + /** + * on project creation, the service_api_keys will be populated with a delay + * check for data.services.service_api_keys, return a valid refresh interval if it's empty + */ + refreshInterval: function (data: any) { + const apiService = (data?.services ?? []).find((x: any) => x.app.id == DEFAULT_API_SERVICE_ID) + const apiKeys = apiService?.service_api_keys ?? [] + const interval = apiKeys.length === 0 ? 2000 : 0 + return interval + }, + }) const anyError = data?.error || error return { @@ -15,3 +26,5 @@ export function useProjectSettings(ref?: string) { isError: !!anyError, } } + +const DEFAULT_API_SERVICE_ID = 1