From 33d11461cea9c10ef9432eadbcab1c1dc3afb75f Mon Sep 17 00:00:00 2001 From: Vincevdb1 Date: Thu, 26 Dec 2024 01:02:07 +0100 Subject: [PATCH] refactor: Separate hostname in settings from sync storage into local storage; - Modify the way settings are retrieved from sync and local storages. - Make sure the hostname doesn't get in the sync storage but in the local storage. --- src/core/WakaTimeCore.ts | 20 ++++++++++++----- src/utils/settings.ts | 47 +++++++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/src/core/WakaTimeCore.ts b/src/core/WakaTimeCore.ts index a3cb97f..eaeafb4 100644 --- a/src/core/WakaTimeCore.ts +++ b/src/core/WakaTimeCore.ts @@ -165,11 +165,21 @@ class WakaTimeCore { } async sendHeartbeats(): Promise { - const settings = await browser.storage.sync.get({ - apiKey: config.apiKey, - heartbeatApiEndPoint: config.heartbeatApiEndPoint, - hostname: '', - }); + const [syncSettings, localSettings] = await Promise.all([ + browser.storage.sync.get({ + apiKey: config.apiKey, + heartbeatApiEndPoint: config.heartbeatApiEndPoint, + }) as Promise<{ apiKey: string; heartbeatApiEndPoint: string }>, + browser.storage.local.get({ + hostname: '', + }) as Promise<{ hostname: string }>, + ]); + + const settings = { + ...syncSettings, + hostname: localSettings.hostname, + }; + if (!settings.apiKey) { await changeExtensionStatus('notSignedIn'); return; diff --git a/src/utils/settings.ts b/src/utils/settings.ts index e835057..72e598c 100644 --- a/src/utils/settings.ts +++ b/src/utils/settings.ts @@ -23,22 +23,31 @@ export interface Settings { } export const getSettings = async (): Promise => { - const settings = (await browser.storage.sync.get({ - allowList: [], - apiKey: config.apiKey, - apiUrl: config.apiUrl, - blacklist: null, - customProjectNames: [], - denyList: [], - hostname: config.hostname, - loggingEnabled: config.loggingEnabled, - loggingStyle: config.loggingStyle, - loggingType: config.loggingType, - socialMediaSites: config.socialMediaSites, - theme: config.theme, - trackSocialMedia: true, - whitelist: null, - })) as Omit & { + const [syncSettings, localSettings] = await Promise.all([ + browser.storage.sync.get({ + allowList: [], + apiKey: config.apiKey, + apiUrl: config.apiUrl, + blacklist: null, + customProjectNames: [], + denyList: [], + loggingEnabled: config.loggingEnabled, + loggingStyle: config.loggingStyle, + loggingType: config.loggingType, + socialMediaSites: config.socialMediaSites, + theme: config.theme, + trackSocialMedia: true, + whitelist: null, + }), + browser.storage.local.get({ + hostname: config.hostname, + }), + ]); + + const settings = { + ...syncSettings, + hostname: localSettings.hostname, + } as Omit & { blacklist?: string; socialMediaSites: string[] | string; whitelist?: string; @@ -81,7 +90,11 @@ export const getSettings = async (): Promise => { }; export const saveSettings = async (settings: Settings): Promise => { - return browser.storage.sync.set(settings); + const { hostname, ...syncSettings } = settings; + await Promise.all([ + browser.storage.sync.set(syncSettings), + browser.storage.local.set({ hostname }), + ]); }; export const getApiUrl = async () => {