From 51737da64399566f20be955c5ed638f7d3fe8b22 Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Thu, 26 Oct 2023 20:39:25 -0500 Subject: [PATCH] fix(sdk): use settings from SDK when there is no preexisting persisted data --- src/Bootstrap.tsx | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/src/Bootstrap.tsx b/src/Bootstrap.tsx index 252fdd2bf..508f7ab0a 100644 --- a/src/Bootstrap.tsx +++ b/src/Bootstrap.tsx @@ -125,26 +125,40 @@ function Bootstrap({ PersistedStorageKeys.USER_SETTINGS ) - let configFromSdk = {} - - try { + const computeUserSettings = async (): Promise => { if (queryParams.has(QueryParamKeys.GET_SDK_CONFIG)) { - configFromSdk = await getConfigFromSdk() + try { + const configFromSdk = await getConfigFromSdk() + + return { + ...userSettings, + ...persistedUserSettings, + ...configFromSdk, + } + } catch (e) { + console.error( + 'Chitchatter configuration from parent frame could not be loaded' + ) + } + + return userSettings } - } catch (e) { - console.error( - 'Chitchatter configuration from parent frame could not be loaded' - ) + + if (persistedUserSettings) { + return { + ...userSettings, + ...persistedUserSettings, + } + } + + return userSettings } - if (persistedUserSettings) { - setUserSettings({ - ...userSettings, - ...persistedUserSettings, - ...configFromSdk, - }) - } else { - await persistUserSettings(userSettings) + const computedUserSettings = await computeUserSettings() + setUserSettings(computedUserSettings) + + if (persistedUserSettings === null) { + await persistUserSettings(computedUserSettings) } setHasLoadedSettings(true)