Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add global wm enable/disable #106

Merged
merged 6 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/background/Background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
isMonetizationReadyHandler,
runPaymentHandler,
setIncomingPointerHandler,
setStorageData,
setStorageKey,
} from '../messageHandlers'
import { tabChangeHandler, tabUpdateHandler } from './tabHandlers'
Expand All @@ -25,6 +26,7 @@ class Background {
getStorageData,
getStorageKey,
setStorageKey,
setStorageData,
]
private subscriptions: any = []
// TO DO: remove these from background into storage or state & use injection
Expand All @@ -39,7 +41,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)
}
Expand Down
2 changes: 1 addition & 1 deletion src/messageHandlers/getStorageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/messageHandlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -13,5 +14,6 @@ export {
isMonetizationReadyHandler,
runPaymentHandler,
setIncomingPointerHandler,
setStorageData,
setStorageKey,
}
17 changes: 17 additions & 0 deletions src/messageHandlers/setStorageData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { storageApi } from '@/utils/storage'

const setStorageData = async (data: any) => {
try {
await storageApi.set({ data })
return {
type: 'SUCCESS',
}
} catch (error) {
return {
type: 'ERROR',
error,
}
}
}

export default { callback: setStorageData, type: 'SET_STORAGE_DATA' }
1 change: 1 addition & 0 deletions src/providers/__tests__/popup-context.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jest.mock('webextension-polyfill', () => ({
addListener: jest.fn(),
removeListener: jest.fn(),
},
sendMessage: jest.fn(),
},
}))

Expand Down
12 changes: 12 additions & 0 deletions src/providers/popup.provider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { createContext, useEffect, useState } from 'react'

import { sendMessage } from '@/utils/sendMessages'
import { defaultData, getStorageData } from '@/utils/storage'

import { PopupContextValue, TPopupContext } from './providers.interface'
Expand All @@ -16,6 +17,10 @@ export const PopupContext = createContext<PopupContextValue>({
export const PopupProvider: React.FC<IProps> = ({ children }) => {
const [data, setData] = useState<TPopupContext>({ ...defaultData })

const updateStorageData = async () => {
await sendMessage({ type: 'SET_STORAGE_DATA', data })
}

useEffect(() => {
const fetchData = async () => {
try {
Expand All @@ -32,6 +37,13 @@ export const PopupProvider: React.FC<IProps> = ({ children }) => {
fetchData()
}, [])

useEffect(() => {
if (JSON.stringify(data) !== JSON.stringify(defaultData)) {
updateStorageData()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [data])

return <PopupContext.Provider value={{ data, setData }}>{children}</PopupContext.Provider>
}

Expand Down
Loading