From c991289366eb122d244233992da6e079c5507c73 Mon Sep 17 00:00:00 2001 From: Diana Fulga Date: Thu, 22 Feb 2024 10:50:41 +0200 Subject: [PATCH 1/5] Add global wm enable/disable --- src/background/Background.ts | 2 ++ src/messageHandlers/setStorageData.ts | 13 +++++++++++++ src/providers/popup.provider.tsx | 8 ++++++++ 3 files changed, 23 insertions(+) create mode 100644 src/messageHandlers/setStorageData.ts diff --git a/src/background/Background.ts b/src/background/Background.ts index e45edb12..fedca9f7 100644 --- a/src/background/Background.ts +++ b/src/background/Background.ts @@ -10,6 +10,7 @@ import getStorageData from '../messageHandlers/getStorageData' import isMonetizationReadyHandler from '../messageHandlers/isMonetizationReadyHandler' import setIncomingPointerHandler from '../messageHandlers/setIncomingPointerHandler' import { tabChangeHandler, tabUpdateHandler } from './tabHandlers' +import setStorageData from '../messageHandlers/setStorageData' const storage = browser.storage.local @@ -19,6 +20,7 @@ class Background { setIncomingPointerHandler, getSendingPaymentPointerHandler, getStorageData, + setStorageData, ] private subscriptions: any = [] // TO DO: remove these from background into storage or state & use injection diff --git a/src/messageHandlers/setStorageData.ts b/src/messageHandlers/setStorageData.ts new file mode 100644 index 00000000..b6d3c984 --- /dev/null +++ b/src/messageHandlers/setStorageData.ts @@ -0,0 +1,13 @@ +import browser from 'webextension-polyfill' + +const storage = browser.storage.local + +const setStorageData = async (data: any) => { + await storage.set([{ data: data }]) + + return { + type: 'SUCCESS', + } +} + +export default { callback: setStorageData, type: 'SET_STORAGE_DATA' } diff --git a/src/providers/popup.provider.tsx b/src/providers/popup.provider.tsx index 4f2e10b6..6c03b988 100644 --- a/src/providers/popup.provider.tsx +++ b/src/providers/popup.provider.tsx @@ -3,6 +3,8 @@ import React, { createContext, useEffect, useState } from 'react' import { getStorageData } from '@/utils/storage' import { PopupContextValue, TPopupContext } from './providers.interface' +import setStorageData from '../messageHandlers/setStorageData' +import { sendMessage } from '@/utils/sendMessages' export const defaultData = { connected: false, @@ -40,6 +42,12 @@ export const PopupProvider: React.FC = ({ children }) => { // eslint-disable-next-line react-hooks/exhaustive-deps }, []) + useEffect(() => { + if (!data) return + + sendMessage({ type: 'SET_STORAGE_DATA', data }) + }, [data]) + return {children} } From db4c84c3999b2f35180144e22e0e0a5fb194e40f Mon Sep 17 00:00:00 2001 From: Ionut Anin Date: Tue, 27 Feb 2024 19:04:27 +0200 Subject: [PATCH 2/5] fix storage update with new data --- src/background/Background.ts | 3 ++- src/messageHandlers/getStorageData.ts | 2 +- src/messageHandlers/index.ts | 2 ++ src/messageHandlers/setStorageData.ts | 18 +++++++++++------- src/providers/popup.provider.tsx | 14 +++++++++----- src/utils/storage.ts | 2 +- 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/background/Background.ts b/src/background/Background.ts index 4adc6203..42a89621 100644 --- a/src/background/Background.ts +++ b/src/background/Background.ts @@ -12,6 +12,7 @@ import { isMonetizationReadyHandler, runPaymentHandler, setIncomingPointerHandler, + setStorageData, setStorageKey, } from '../messageHandlers' import { tabChangeHandler, tabUpdateHandler } from './tabHandlers' @@ -41,7 +42,7 @@ class Background { // TODO: to be moved to a service async setStorageDefaultData() { try { - await storageApi.set({ ...defaultData }) + await storageApi.set({ data: { ...defaultData } }) } catch (error) { console.error('Error storing data:', error) } diff --git a/src/messageHandlers/getStorageData.ts b/src/messageHandlers/getStorageData.ts index d1ae9648..66845d94 100644 --- a/src/messageHandlers/getStorageData.ts +++ b/src/messageHandlers/getStorageData.ts @@ -2,7 +2,7 @@ import { storageApi } from '@/utils/storage' const getStorageData = async () => { try { - const data = await storageApi.get('data') + const { data } = await storageApi.get('data') return { type: 'SUCCESS', data, diff --git a/src/messageHandlers/index.ts b/src/messageHandlers/index.ts index 83c0a511..52826a98 100644 --- a/src/messageHandlers/index.ts +++ b/src/messageHandlers/index.ts @@ -4,6 +4,7 @@ import getStorageKey from './getStorageKey' import isMonetizationReadyHandler from './isMonetizationReadyHandler' import runPaymentHandler from './runPaymentHandler' import setIncomingPointerHandler from './setIncomingPointerHandler' +import setStorageData from './setStorageData' import setStorageKey from './setStorageKey' export { @@ -13,5 +14,6 @@ export { isMonetizationReadyHandler, runPaymentHandler, setIncomingPointerHandler, + setStorageData, setStorageKey, } diff --git a/src/messageHandlers/setStorageData.ts b/src/messageHandlers/setStorageData.ts index b6d3c984..e424f76d 100644 --- a/src/messageHandlers/setStorageData.ts +++ b/src/messageHandlers/setStorageData.ts @@ -1,12 +1,16 @@ -import browser from 'webextension-polyfill' - -const storage = browser.storage.local +import { storageApi } from '@/utils/storage' const setStorageData = async (data: any) => { - await storage.set([{ data: data }]) - - return { - type: 'SUCCESS', + try { + await storageApi.set({ data }) + return { + type: 'SUCCESS', + } + } catch (error) { + return { + type: 'ERROR', + error, + } } } diff --git a/src/providers/popup.provider.tsx b/src/providers/popup.provider.tsx index 05941939..76500270 100644 --- a/src/providers/popup.provider.tsx +++ b/src/providers/popup.provider.tsx @@ -1,10 +1,9 @@ import React, { createContext, useEffect, useState } from 'react' +import { sendMessage } from '@/utils/sendMessages' import { defaultData, getStorageData } from '@/utils/storage' import { PopupContextValue, TPopupContext } from './providers.interface' -import setStorageData from '../messageHandlers/setStorageData' -import { sendMessage } from '@/utils/sendMessages' interface IProps { children: React.ReactNode @@ -18,6 +17,10 @@ export const PopupContext = createContext({ export const PopupProvider: React.FC = ({ children }) => { const [data, setData] = useState({ ...defaultData }) + const updateStorageData = async () => { + await sendMessage({ type: 'SET_STORAGE_DATA', data }) + } + useEffect(() => { const fetchData = async () => { try { @@ -35,9 +38,10 @@ export const PopupProvider: React.FC = ({ children }) => { }, []) useEffect(() => { - if (!data) return - - sendMessage({ type: 'SET_STORAGE_DATA', data }) + if (JSON.stringify(data) !== JSON.stringify(defaultData)) { + updateStorageData() + } + // eslint-disable-next-line react-hooks/exhaustive-deps }, [data]) return {children} diff --git a/src/utils/storage.ts b/src/utils/storage.ts index 569df536..7bea278c 100755 --- a/src/utils/storage.ts +++ b/src/utils/storage.ts @@ -21,7 +21,7 @@ export const defaultData: TPopupContext = { export const getStorageData = async () => { try { const response = await sendMessage({ type: 'GET_STORAGE_DATA' }) - return response?.data as TPopupContext + return response.data as TPopupContext } catch (error) { console.error('Error fetching storage data:', error) return null From 6040c918267f945b76f58b7de53361325fd500d3 Mon Sep 17 00:00:00 2001 From: Ionut Anin Date: Tue, 27 Feb 2024 19:07:16 +0200 Subject: [PATCH 3/5] remove duplicate import --- src/background/Background.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/background/Background.ts b/src/background/Background.ts index 42a89621..2453bca9 100644 --- a/src/background/Background.ts +++ b/src/background/Background.ts @@ -16,7 +16,6 @@ import { setStorageKey, } from '../messageHandlers' import { tabChangeHandler, tabUpdateHandler } from './tabHandlers' -import setStorageData from '../messageHandlers/setStorageData' class Background { private messageHandlers: any = [ From 695f5eee02fdaba03c86a6d4e25c21abdc068515 Mon Sep 17 00:00:00 2001 From: Ionut Anin Date: Tue, 27 Feb 2024 19:12:24 +0200 Subject: [PATCH 4/5] fix tests by mocking sendMessage --- src/providers/__tests__/popup-context.test.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/providers/__tests__/popup-context.test.tsx b/src/providers/__tests__/popup-context.test.tsx index d5b80640..60213a4f 100644 --- a/src/providers/__tests__/popup-context.test.tsx +++ b/src/providers/__tests__/popup-context.test.tsx @@ -11,6 +11,7 @@ jest.mock('webextension-polyfill', () => ({ addListener: jest.fn(), removeListener: jest.fn(), }, + sendMessage: jest.fn(), }, })) From 754aec664a8e1a65ee515fbfa758298ecf8b060e Mon Sep 17 00:00:00 2001 From: Ionut Anin Date: Tue, 27 Feb 2024 19:13:26 +0200 Subject: [PATCH 5/5] fix test undefined error --- src/utils/storage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/storage.ts b/src/utils/storage.ts index 7bea278c..569df536 100755 --- a/src/utils/storage.ts +++ b/src/utils/storage.ts @@ -21,7 +21,7 @@ export const defaultData: TPopupContext = { export const getStorageData = async () => { try { const response = await sendMessage({ type: 'GET_STORAGE_DATA' }) - return response.data as TPopupContext + return response?.data as TPopupContext } catch (error) { console.error('Error fetching storage data:', error) return null